IBM Worklight : WL.Client.getUserName Fails to retrieve userIdentity immediately after authentication

▼魔方 西西 提交于 2019-12-18 07:23:19

问题


I have done adapter based authentication and there is no problem in authentication and it works fine. I have faced some issues in getting the active users useridentity.The code may explain you a bit more

adapterAuthRealmChallengeHandler.handleChallenge = function(response){
    var authRequired = response.responseJSON.authRequired;
    if (authRequired == true){
        if (response.responseJSON.errorMessage)
            alert(response.responseJSON.errorMessage);
    } else if (authRequired == false){
        adapterAuthRealmChallengeHandler.submitSuccess();
        setTimeout(function(){pageTransitionCall();},10000); //this code only works 
            pageTransitionCall(); //This throws null error in console
    }   
};
function pageTransitionCall(){
    console.log(WL.Client.getUserName("AdapterAuthRealm"));
}

As you can see i was trying to get the active userName of the realm. The WL.Client.getUserName("AdapterAuthRealm") only works after some time interval only and i am not sure about the time interval. By adapter code is as below

function submitAuthentication(username, password,userCred){
    if (username==="worklight" && password === "worklight"){
        WL.Logger.info("if");
            var userIdentity = {
                    userId: userCred,
                    displayName: userCred,
                    attributes: {
                        foo: "bar"
                    },
                    loginName : userCred,
                    userName : userCred
            };
            WL.Server.setActiveUser("AdapterAuthRealm", userIdentity);
            WL.Logger.info(JSON.stringify(userIdentity));
            return { 
                authRequired: false 
            };
        }
    else
    {
        WL.Logger.info("else");
        return onAuthRequired(null, "Invalid login credentials");
    }
}

My doubt is why does the client cant retrieve the activeuser. And i am sure that my code is correct and active user is set and i can see in the server log.After the setactvieruser is set only i have return false in the adpter and why cant the client retrieve the user at instant and why it needs delay to retrieve. i have verified in both Worklight V6.0 and also Worklight V6.1.i have created the Ipad environment.


回答1:


The info that contains logged in userId (basically any userIdentity data) is not returned immediately after adapter authentication but only when an original request succeeds. Consider this

  1. You're making request#1 to the server (let's say invoke procedure)
  2. You're getting response with authRequired:true
  3. You're submitting auth data
  4. You're getting authRequred:false
  5. You're calling submitSuccess()
  6. WL framework automatically re-invokes request#1
  7. You're getting response for request#1

userIdentity data will be returned in step7 and not in step4. Basically once you start authentication flow you're considered out of the original invocation context. You need to finish the flow and tell WL framework that auth has completed. Once you do - WL framework will reinvoke the original request. WL server add userIdentity data to the response and WL client will update userName, displayName etc properties.

In case you need user data before that, e.g. right away once auth is complete, you can add custom properties to your submitAuthentication function response, e.g.

        WL.Server.setActiveUser("AdapterAuthRealm", userIdentity);
        return { 
            authRequired: false,
            loginName: userIdentity.loginName
        };

this will make sure that loginName will be returned to your handleChallenge function. you can retrieve it there and do whatever you want with it.



来源:https://stackoverflow.com/questions/21770854/ibm-worklight-wl-client-getusername-fails-to-retrieve-useridentity-immediately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!