Sign In Register

How can we help you today?

Start a new topic

Keep user authentication using authToken or sessionId

Is there any way to keep user authentication or re-authenticate user using auth token or session id instead of login/password?
I know that you can use DeviceAuthenticationRequest but we need to authenticate existing user using his login and password first, and then keep the session.

We are building a custom dashboard using JavaScript SDK and we want to have "remember me" option. It seems that currently, once the WebSocket is disconnected, there is no other way to re-authenticate user besides asking for a login and password again, and this really annoying for the user (every time they refresh the page, they need to enter login and password again).

If there is currently no such option, can we expect to have it in the near future?

Thanks!

1 person likes this idea

Hi Leonid, I'll look into this for you and be back with an answer soon. 


Best Regards, Patrick. 

+1 for this


 Using with an angular app and if the app is reloaded by a refresh there needs to be way to use the token system to keep the user logged in

After some research, I discovered that it's possible to do it with some minor changes to the gamesparks.js.
Basically, during the handshake, after client recieves the AuthenticatedConnectResponse and does the handshake (calls OnNonce and sends AuthenticatedConnectRequest), you can send authToken and sessionId with the nonce. Current version of library supports it, it takes authToken and sessionId from the GameSparks object. That way, if websocket got disconnected it can safely reconnect and continue working.

However, there is no built-in way to set and get authToken from the outside of the GameSparks class. So here is what you need to do in the gamesparks.js:

After "this.getSessionId" add function:
this.getAuthToken = function(){
    return authToken;
}

 

In this.init function, add before cleanup() and connect() calls:
this.init = function(options) {
	...
	errorCallback = options.onError;
	nonceCallback = options.onNonce;

	cleanup();
	connect();
};

 

You can now now get authToken and sessionId from the cookies or whatever place you store them, and pass them to the options for the init function.

After user is logged in, you can get them by calling getSessionId() and getAuthToken().


In Angular.js, I have a service that wraps up the GameSparks SDK. In that service, I get saved authToken and sessionId using $cookies.get(), inject them into my options, init gamesparks, and then call AccountDetailsRequest. If that succeed, I get all necessary information and assume that user is logged in, otherwise I show the login page. In the login controller, after user signs in using AuthenticationRequest, I call getSessionId() and getAuthToken and save them into cookies using $cookies.put(). Hope that helps someone.


That would be really nice to have that changes in the official gamesparks.js. 





1 person likes this
Edit:
In this.init function, add before cleanup() and connect() calls:
1
2
3
4
5
6
7
8
9
this.init = function(options) {
    ...
 
    authToken = options.authToken;
    sessionId = options.sessionId;
 
    cleanup();
    connect();
};



1 person likes this

Leonid thanks for your post. It was something I was leaving to the end of my development to look into and your post has saved me having to look so thank you for taking the time.


Cheers 



Hi all, I'm trying to achieve the same, I just need to keep the user logged in in my JS app. I'm not familiar with Angular and I don't understand how to wraps the Gamesparks SDK in an Angular service, do you have maybe a sample for this? For now if the user is logged in and jumps to another page I call AccountDetailsRequest but of course the connection with the socket was lost and a can't catch user's data.

I was looking into the same topic. Update on that matter:

The functionality described by Leonid Umanskiy is included in the latest js sdk. The only thing you need to do is to store the authToken and set it before initializing. Works like a charm for me.

 

Use:

getAuthToken
setAuthToken

 

Does that apply for the CS version?

Login to post a comment