How do you handle usergrid access token expiry in node.js?

I need to use user authentication with API BaaS before obtaining access to a collection. I followed the instructions on how to perform user authentication and obtaining access tokens, from the documentation link: http://apigee.com/docs/app-services/content/authen...

I have been able to successfully authenticate with baas and access the collection. Since the default time to live as per docs is 7days, i get the access token expired message in the node.js logs and hence was not able to access the collection. I had to undeploy and deploy my proxy again after which it started working. Is there a way to set ttl in the node.js or handle this error and obtain a new access token?

Below is my code.

//Retrieve user credentials from config
var username=config.clientUser;
var password=config.clientPassword;

var loggedIn;

function initializeUsergridClient() {
if (loggedIn == null || loggedIn == undefined) {
//initialize the SDK
var client = new usergrid.client({
'orgName' : config.organization,
'appName' : config.application,
logging   : 'true',
URI       : config.uri,
buildCurl : 'true'
});
    client.login(username, password,
        function (err) {
            if (err) {
                //error - could not log user in
              token = null;
                console.log("error: Could not login."+err);
            } else {
                //success - user has been logged in
    console.log(client);
                //the login call will return an OAuth token, which is saved
                //in the client object for later use.  Access it this way:
                token = client.token;
    console.log(token);
                loggedIn = new usergrid.client({
                    orgName  : config.organization,
                    appName  : config.application,
                    logging  : 'true',
                    URI      : config.uri,
                    buildCurl: 'true',
                    token    : client.token,
                  authType : usergrid.AUTH_APP_USER
                });
            }
        });
}
}
Solved Solved
0 2 1,282
1 ACCEPTED SOLUTION

@Nagashree B ,

Great finding !!

I think yes, this issue is reproducible. Here is what i can think about same,

  • Restart of Node App should fix the issue, Because tokens are cached. Ideally we cannot restart server every time token expires. That's the reason when you redeployed it worked.
  • Usergrid defines a way to logout user & re-login user. Re-Login should generate a new access token . Unfortunately, I don't think there is a way to find out token expired or not in application. Reason, SDK doesn't return error details. It's a implementation bug in SDK i believe. It currently returns just error true/false.

I suggest using client credentials for time being instead of password grant type. You should not get into the issue of tokens if you use same.

As per documentation of Node.JS SDK here , You will use client credentials generally. But, When you need to authenticate an user you will use password grant type to generate token. Once you do same, You will logout user & set the authentication mechanism back to client credentials.

Else, I suggest using BaaS APIs for time being to do the job instead of depending on NodeJS SDK. You can regenerate token using an API call when you get to know token expired. It needs little bit of effort to build app using direct APIs.

Let me try to find a easy hack to get around this problem till fix is available in SDK. I will keep you posted.

View solution in original post

2 REPLIES 2

@Nagashree B ,

Great finding !!

I think yes, this issue is reproducible. Here is what i can think about same,

  • Restart of Node App should fix the issue, Because tokens are cached. Ideally we cannot restart server every time token expires. That's the reason when you redeployed it worked.
  • Usergrid defines a way to logout user & re-login user. Re-Login should generate a new access token . Unfortunately, I don't think there is a way to find out token expired or not in application. Reason, SDK doesn't return error details. It's a implementation bug in SDK i believe. It currently returns just error true/false.

I suggest using client credentials for time being instead of password grant type. You should not get into the issue of tokens if you use same.

As per documentation of Node.JS SDK here , You will use client credentials generally. But, When you need to authenticate an user you will use password grant type to generate token. Once you do same, You will logout user & set the authentication mechanism back to client credentials.

Else, I suggest using BaaS APIs for time being to do the job instead of depending on NodeJS SDK. You can regenerate token using an API call when you get to know token expired. It needs little bit of effort to build app using direct APIs.

Let me try to find a easy hack to get around this problem till fix is available in SDK. I will keep you posted.

@Anil Sagar Thanks for the detailed reply.