AAhad
AAhad

Reputation: 2845

Worklight: Challenge-handler not working as expected

I used sample challenge handler that comes with Form Based Authentication module. I modified it as per my requirements. In my App, I have one Landing (home) page , where i have link to a login page. Now i want it to function when user click on the login button. I face various problems here:

  1. On first click on Login button, it does authenticate with WL Server but then it is unable to proceed further to execute login function logic.
  2. after first click, i have to 2nd time click on Login button. At 2nd login click it execute login function perfectly.
  3. When I log out,either it is NOT removing session from server or what? It only execute log-out function code (given below). But again after the specified session time out, it prompts 2nd/3rd time and shows "time out message". Although user is not loged in again. he is already log-out.

Purpose: I want App to only login when user click on Log-in button, but with just one click. and when i log-out Or time out, it should not keep session active OR shows "session timeout" message after the specified time again & again.

My Challenge Handler:

var aahadAppRealmChallengeHandler = WL.Client.createChallengeHandler("myAppRealm");
var isLandingPage=false , islogout=false;
aahadAppRealmChallengeHandler.isCustomResponse = function(response) {
    WL.Logger.debug("I am here >> 1");
    if (!response || response.responseText === null) {  return false;    }
    var indicatorIdx = response.responseText.search('j_security_check');
    if (indicatorIdx >= 0){ WL.Logger.debug("return true "); return true; }  
    else { 
        if(isLandingPage && $.trim($('#fldloginUserID').val()) !="" && $.trim($('#fldloginUserPassword').val()) !="" ) {
             WL.Logger.debug("WL.Client.isUserAuthenticated()=" + WL.Client.isUserAuthenticated("myAppRealm"));
             if(WL.Client.isUserAuthenticated("myAppRealm")) {       WL.Logger.debug("return false ");  return false;           }
             else {  WL.Logger.debug("return true "); return true;          }
         } 
        WL.Logger.debug("return false ");  return false;
    }
};
aahadAppRealmChallengeHandler.handleChallenge = function(response) {
     WL.Logger.debug("I am here >> 2");
     var indicatorIdx = response.responseText.search('j_security_check');
     var suc = response.responseText.search('success');
     WL.Logger.debug("I am here >> 3 - indicatorIdx =" + indicatorIdx + " Success =" + suc + "  - isLandingPage=" + isLandingPage +" islogout=" +islogout);
    if (isLandingPage){
            if (suc >= 0 ){
                WL.Logger.debug("I am here >> 4 - isLandingPage=" + isLandingPage +" suc="+suc);
                var reqURL = '/j_security_check';           var options = {};
                options.parameters = {
                    j_username : $.trim($('#fldloginUserID').val().toLowerCase()),
                    j_password : $.trim($('#fldloginUserPassword').val())
                };
                options.headers = {};
                aahadAppRealmChallengeHandler.submitLoginForm(reqURL, options, aahadAppRealmChallengeHandler.submitLoginFormCallback);
            }else {
                WL.Logger.debug("I am here >> 5");
                WL.SimpleDialog.show(DialogMessages_en.SessionExpired_Tile, DialogMessages_en.SessionExpired_Description ,
                    [ { text : 'Close', handler : function () {                 
                        if(busyIndicator.isVisible())   
                            busyIndicator.hide();
                         isLandingPage = false; userLogout();islogout=true;
                        $.mobile.changePage("#landingPage" , { transition: "slide"});   
                    } } ]);
            }
    }
     else {
         WL.Logger.debug("I am here >> 6 - isLandingPage=" + isLandingPage +" re-Login Again");
         if(indicatorIdx < 1) {
                var reqURL = '/j_security_check';       var options = {};
                options.parameters = {
                    j_username : $.trim($('#fldloginUserID').val().toLowerCase()),
                    j_password : $.trim($('#fldloginUserPassword').val())
                };
                options.headers = {};
                aahadAppRealmChallengeHandler.submitLoginForm(reqURL, options, aahadAppRealmChallengeHandler.submitLoginFormCallback); 
         }
     }
};
aahadAppRealmChallengeHandler.submitLoginFormCallback = function(response) {
    var isLoginFormResponse = aahadAppRealmChallengeHandler.isCustomResponse(response);
    if (isLoginFormResponse){  isLandingPage=false; aahadAppRealmChallengeHandler.handleChallenge(response);  } 
    else {isLandingPage=true;  aahadAppRealmChallengeHandler.submitSuccess(); WL.Logger.debug("aahadAppRealmChallengeHandler.submitSuccess()");       }
};
$('#logindone').bind('click', function () {
    WL.Logger.debug(" Button Clicked -Before isLandingPage=" +isLandingPage);  
    isLandingPage=true;  
            var reqURL = '/j_security_check';       var options = {};
            options.parameters = {
                j_username : $.trim($('#fldloginUserID').val().toLowerCase()),
                j_password : $.trim($('#fldloginUserPassword').val())
            };
            options.headers = {};
            aahadAppRealmChallengeHandler.submitLoginForm(reqURL, options, aahadAppRealmChallengeHandler.submitLoginFormCallback);
            loginAuthenticateUser();        
});

my Login Function

function loginAuthenticateUser() {
     WL.Logger.debug("Calling loginAuthenticateUser()....");
     busyIndicator.show();
     if ($.trim( $("#fldloginUserID").val()) !="" && $.trim( $("#fldloginUserPassword").val()) !="") {
            authenticateLDAPUsers( $.trim( $("#fldloginUserID").val().toLowerCase() ) , $.trim( $("#fldloginUserPassword").val() ));
     }else {
            if(busyIndicator.isVisible())   
                busyIndicator.hide();
       simpleDialogDemo(DialogMessages_en.LoginFailed_MsgTitle , DialogMessages_en.LoginFailed_MsgDescription);
     }
}

Log-out Function

function userLogout() {
      WL.Logger.debug("Logout....");
      WL.TabBar.setVisible(false);
      WL.Client.logout('myAppRealm', {onSuccess: function(){}  });  
      $.mobile.changePage("#landingPage" , { transition: "slide"});
      var options = {onSuccess: function() {WL.Logger.debug("collection closed");}, onFailure: function() { WL.Logger.debug("collection closing failed"); } };
      WL.JSONStore.closeAll(options);
}

authenticationConfig.xml (Realms)

<realm loginModule="Strongme" name="myAppRealm">
            <className>com.worklight.core.auth.ext.FormBasedAuthenticator</className>
            <parameter name="login-page" value="login.html" />
        </realm>

Worklight.properties

serverSessionTimeout=5

Any suggestion please.

Thanks

Upvotes: 0

Views: 2881

Answers (1)

Anton
Anton

Reputation: 3166

In case of FormBasedAuthentication you need to trigger authentication before actually submitting credentials. Therefore you need to call WL.Client.login("realm-name") in your app.

In case authentication requires immediately on app startup - call WL.Client.login(..) in your wlEnvInit or wlCommonInit function. In case it is requires on a later stage - call it once you need it.

Upvotes: 2

Related Questions