Topic
9 replies Latest Post - ‏2012-11-16T15:53:00Z by SystemAdmin
SystemAdmin
SystemAdmin
2327 Posts
ACCEPTED ANSWER

Pinned topic WL.Client.isUserAuthenticated returns false after logged in

‏2012-11-07T09:18:00Z |
Hi,

My apps requires push notification, and all scripts ready, at most cases, I can do subscribe correctly, now a case shows me a strange thing, my scripts looks attached below, it checks if onReadyToSubscribe called by WL client and if user already logged into system, if these 2 criteria OK, then call doSubscribe, now my problem is WL Client takes more time to register to GCM, before "Push notification device token has changed, updating server notification token id." got, user logged into system, and then onReadyToSubscribe called, but my logcat tells me current user haven't logged into system, why? how my script capture best chance to do subscribe?

1. onReadyToSubscribe:
WL.Client.Push.onReadyToSubscribe = function() {
WL.Client.Push.registerEventSourceCallback(PUSH_ALIAS, ADAPTER,
EVENT_SOURCE, pushNotificationReceived);

if (WL.Client.isUserAuthenticated(REALM)) {
_this.doSubscribe();
} else {
console.log('onReadyToSubscribe, but user haven't logged into system');
}

WL.Client.Push.readyToSubscribe = true;
};

2. After logged into system, it calls doSubscribe using following lines:
if (WL.Client.Push.isPushSupported && WL.Client.Push.isPushSupported()) {
if (WL.Client.Push.isSubscribed(PUSH_ALIAS) == false) {
if (WL.Client.Push.readyToSubscribe) {
_this.push.doSubscribe();
}
}
}
var _this = this;
this.doSubscribe = function() {
WL.Client.Push.subscribe(PUSH_ALIAS, {
onSuccess : pushSubscribe_Callback,
onFailure : pushSubscribe_Callback
});
};
thanks!
Daniel
  • SystemAdmin
    SystemAdmin
    2327 Posts
    ACCEPTED ANSWER

    Re: WL.Client.isUserAuthenticated returns false after logged in

    ‏2012-11-08T06:14:35Z  in response to SystemAdmin
    Oh, I found the statement at WL.Client.setUserPref() in document,
    "The local user preferences in the application are only updated when a successful response is received from the server."
    Can I guess that our WL.Client.isUserAuthenticated() use local preferences (just like getUserPref)?
    • SystemAdmin
      SystemAdmin
      2327 Posts
      ACCEPTED ANSWER

      Re: WL.Client.isUserAuthenticated returns false after logged in

      ‏2012-11-09T16:36:33Z  in response to SystemAdmin
      "Can I guess that our WL.Client.isUserAuthenticated() use local preferences (just like getUserPref)?"

      Yes.
      More specifically, when you successfully connect to the server as the client, you'll receive a json object informing you of all of the realms that you have successfully authenticated in. You can see this object in your browser log after starting an app in your browser. Each realm that your app interacts with will have its own object and each object will have an "isUserAuthenticated" property. This property will determine whether "WL.Client.isUserAuthenticated" will return true or false.
      • SystemAdmin
        SystemAdmin
        2327 Posts
        ACCEPTED ANSWER

        Re: WL.Client.isUserAuthenticated returns false after logged in

        ‏2012-11-12T03:12:57Z  in response to SystemAdmin
        thanks for your comments.
        now I can find JSON object after login procedure of my authentication adapter returned to client. I use the following statement to change 'isUserAuthenticated' indicator in onSuccess callback of my login procedure, now I can get WL.Client.isUserAuthenticated(REALM) == true.

        this looks like hacking, is my workaround feasible?
        and I'm wondering if my alternative statement has potential bug after worklight version upgrades and why worklight don't do this step automatically after a login procedure of authentication adapter called, naming it as "WL-Authentication-Success" and existence of 'authRequired: false' implies that worklight do know the procedure is for login purpose?

        result.invocationResultREALM.isUserAuthenticated = 1;
        • SystemAdmin
          SystemAdmin
          2327 Posts
          ACCEPTED ANSWER

          Re: WL.Client.isUserAuthenticated returns false after logged in

          ‏2012-11-12T03:16:45Z  in response to SystemAdmin
          why rectangle bracket removed in the post, how I use ""in IBM forum?

          result.invocationResult.WL-Authentication-Success.MyRealm.isUserAuthenticated=1
  • IdanAdar
    IdanAdar
    741 Posts
    ACCEPTED ANSWER

    Re: WL.Client.isUserAuthenticated returns false after logged in

    ‏2012-11-12T05:11:49Z  in response to SystemAdmin
    Daniel, you can use curly brackets to quote text.

    You have { quote } and { code } (remove spaces before and after the {}).
    quote is for regular text, and code is for code snippets.

    { quote } some text { quote } (no need for a closing /) will produce:
    some text
    Idan Adar
    QA Engineer
    IBM Worklight Mobile Platform
    • SystemAdmin
      SystemAdmin
      2327 Posts
      ACCEPTED ANSWER

      Re: WL.Client.isUserAuthenticated returns false after logged in

      ‏2012-11-13T01:38:27Z  in response to IdanAdar
      Oh, try my code snippet.
      then is my workaround in previous response feasible?
      
      result.invocationResult[
      "WL-Authentication-Success"].[REALM].isUserAuthenticated=1
      
  • SystemAdmin
    SystemAdmin
    2327 Posts
    ACCEPTED ANSWER

    Re: WL.Client.isUserAuthenticated returns false after logged in

    ‏2012-11-14T16:00:04Z  in response to SystemAdmin
    No, you shouldn't have to do that.

    How are you authenticating? Are you doing it through an adapter? On your adapter, do you have something that looks a bit like this:

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


    If so, then just add the "isUserAuthenticated" property to the json object:

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


    This is safe, since setting the userIdentity object and calling "setActiveUser" implies that the user is authenticated.
    • SystemAdmin
      SystemAdmin
      2327 Posts
      ACCEPTED ANSWER

      Re: WL.Client.isUserAuthenticated returns false after logged in

      ‏2012-11-16T02:08:39Z  in response to SystemAdmin
      Hi,

      Thanks for your good idea, now I changed my authentication adapter to push 'isUserAuthenticated: 1' to client during login procedure, then authentication status is synchronized. It's safe because all things are done at server-side.

      I read training module Module_21_-_Adapter_Based_Authentication.pdf again, it only suggest us as below, I suggest that all system fields should be listed here , just like userId/displayName.

      var userIdentity = {
      userId: username,
      displayName: username,
      attributes : {
      foo: "bar"
      }
      }
      Daniel.
      • SystemAdmin
        SystemAdmin
        2327 Posts
        ACCEPTED ANSWER

        Re: WL.Client.isUserAuthenticated returns false after logged in

        ‏2012-11-16T15:53:00Z  in response to SystemAdmin
        Thank you for your input. In future releases, this will not be an issue. The necessary fields will either be added automatically to the userIdentity object created in the adapter or the remaining fields will be described in documentation.