1

LoginViewModel.js

function login(){
       var username='worklight';
      var password = 'worklight';
    var invocationData = {
            adapter : "AuthenticationAdapter",
            procedure : "submitAuthentication",
            parameters : [ username, password ]
        };

        adapterAuthRealmChallengeHandler.submitAdapterAuthentication(invocationData, {});

AdapterAuthRealmChallengeProcessor.js

var adapterAuthRealmChallengeHandler = WL.Client.createChallengeHandler("AdapterAuthRealm");

adapterAuthRealmChallengeHandler.isCustomResponse = function(response) {
if (!response || !response.responseJSON || response.responseText === null) {
    return false;
}
if (typeof(response.responseJSON.authRequired) !== 'undefined'){
    return true;
} else {
    return false;
}
};

adapterAuthRealmChallengeHandler.handleChallenge = function(response){

var authRequired = response.responseJSON.authRequired;

if (authRequired == true){
    window.location = "#login";
    alert(response.responseJSON.errorMessage);

} else if (authRequired == false){
    adapterAuthRealmChallengeHandler.submitSuccess();
    window.location = "#Home";

}
};

AuthenticationAdapter-impl.js

function onAuthRequired(headers, errorMessage){
    errorMessage = errorMessage ? errorMessage : null;

    return {
    authRequired: true,
    errorMessage: errorMessage
    };
}

function submitAuthentication(username, password){

    var userIdentity = {
            userId: username,
            displayName: username, 
            attributes: {
                foo: "bar"
            }
    };
    WL.Server.setActiveUser("AdapterAuthRealm", userIdentity);

    return { 
        authRequired: false 
    };

}

I am not able to set setActiveUser. How to set it and get it. Above wat I have to done for maintaining session. Is it right way to do it. Sometimes I got error Illegal State: Cannot change identity of an already logged in user in realm 'Adapter AuthRealm'. The application must logout first. If I tried to set it null first then it causes my application to enter an infinite loop of attempting to authenticate. Any idea why I would see this behavior? Anything I am doing wrong? please help mi out.

user3747168
  • 43
  • 1
  • 9

1 Answers1

0

Check the SO answer for Worklight logout does not clear active user

Try reloading app onSuccess of Logout.

WL.Client.logout("AdapterAuthRealm",{
                onSuccess: function(){ WL.Client.reloadApp(); },
                onFailure: function(){ WL.Logger.debug("Error on logout");}
            });    

And you can check realm is authenticated or not by using wl client Java script API

WL.Client.isUserAuthenticated("AdapterAuthRealm");
Community
  • 1
  • 1
Bluewings
  • 3,438
  • 3
  • 18
  • 31
  • Hey thanx for helping.. but its still not working I think variable is not getting set. Just refer my code above. I have used this on logout. But at start its not setting up. – user3747168 Jul 23 '14 at 04:41
  • Please share your project in Dropbox. I'll take a look into it – Bluewings Jul 23 '14 at 04:47
  • I have shared my code above. That's what I done. else Authenticationconfig.xml is changed. I called login function then auth adapter and set into it. and afterwards AdapterAuthRealmChallengeProcessor.js tells me true or false. – user3747168 Jul 23 '14 at 09:35
  • plz check it and let mi know as early as possible plz – user3747168 Jul 23 '14 at 10:35