1

I am trying to authenticate my application users with a LDAP module with Worklight. This is working very well with this client code :

var ldapRealmChallengeHandler = WL.Client.createChallengeHandler("LDAPRealm");

function wlCommonInit(){
    WL.Client.login("LDAPRealm");
}

ldapRealmChallengeHandler.isCustomResponse = function(response) {
if (!response || response.responseText === null) {
    return false;
}
var indicatorIdx = response.responseText.search('j_security_check');

if (indicatorIdx >= 0){
    return true;
}  
return false;
};


ldapRealmChallengeHandler.handleChallenge = function(response){
};

ldapRealmChallengeHandler.submitLoginFormCallback = function(response) {
    var isLoginFormResponse = ldapRealmChallengeHandler.isCustomResponse(response);
    if (isLoginFormResponse){
        ldapRealmChallengeHandler.handleChallenge(response);
    } 
    else {
        ldapRealmChallengeHandler.submitSuccess();
        window.location.hash = "classes";
    }
};

submitLoginForm = function(username, password){
        var reqURL = '/j_security_check';
        var options = {};
        options.parameters = {
            j_username : username,
            j_password : password
        };
        options.headers = {};
        ldapRealmChallengeHandler.submitLoginForm(reqURL, options,   ldapRealmChallengeHandler.submitLoginFormCallback);
    }

logout = function(){
    WL.Client.logout('LDAPRealm',{});
    changePage(loginPage);
}

When I log in the first time it works well. However, if I logout, and I try to login a second time, an error appears : "File not found: /apps/services/j_security_check".

I tried several things :

  1. I took the code sample on the Worklight Getting Started website. They have the following code to logout : WL.Client.logout('LDAPRealm',{onSuccess: WL.Client.reloadApp}). If I delete the WL.Client.reloadApp part, the same problem than mine occurs : "/apps/services/j_security_check".

  2. I put the WL.Client.login("LDAPRealm") in the submitForm before calling the server but it's not working.

Why this problem is happening ? Is reload the entire application the only way to solve the problem ? Because it's not really time efficient ...

Thanks a lot for your help.

cmartet
  • 21
  • 4
  • 1
    just check whether user session is already validated or not? while logging in second time by using `WL.Client.isUserAuthenticated("LDAPRealm")` if it is authenticated after logging out also means your logout has some problem. Also you will receive error ` "File not found: /apps/services/j_security_check".` whenever you are submitting form without challenge – Bluewings May 22 '14 at 16:01
  • Actually, the value of `WL.Client.isUserAuthenticated("LDAPRealm")` is true after the logout. However, when I logout, my application received a success event from the server so I don't know what I can do ... All the solutions I have found is to reload the app entirely but I would prefer an other solution ... – cmartet May 23 '14 at 08:50
  • Are you clearing the active user by calling `WL.Server.setActiveUser("LDAPRealm", null);` – Bluewings May 23 '14 at 09:15
  • Where do I need to call this function ? If I call it in the client side, I have an error : `WL.Server is undefined` – cmartet May 23 '14 at 12:37
  • You have to use that in logout function adapter. ASAIK you need to use custom login module to use this. see this SO [question](http://stackoverflow.com/questions/23676890/worklight-logout-does-not-clear-active-user/23677525#23677525) for more info. – Bluewings May 23 '14 at 13:07

1 Answers1

0

You should do a WL.Client.connect instead of WL.Client.login. You are trying to login while you were not challenged yet.

Raanan Avidor
  • 3,533
  • 4
  • 25
  • 32
  • I have placed the `connectOnStartup` property to `true` in the initOptions.js file so normally I don't have to call `WL.Client.connect`. Is it true ? – cmartet May 23 '14 at 08:56