Closed mzupek closed 4 years ago
@mzupek From the server side you can create a new token using socket.setAuthToken(myTokenObject)
.
myTokenObject
should be an object; you can have any JSON property/value inside it.
If your token comes from a third-party auth service or a separate microservice, you can pass it to the client-side in any way you want and then add it to localStorage before you create the socket.
Alternatively, you can pass an authEngine
option when you create the socket which contains a custom client-side auth engine instance - This instance must implement a saveToken
, removeToken
and loadToken
method. This gives you the maximum control over where and how to save, remove and load the JWT on the client side.
@jondubois I am just getting the token back from a microserivce. Since I am using this in react native can I store it to the devices localStorage and will that work? my guess is not? Do you have an example of the authEngine option? thanks for your help in advance
Closing
@mzupek , Would have been helpful if you had commented on solutions before closing this thread.
For future reference, it's possible to use a JWT token generated from a different microservice for use by SC using localStorage.setItem('socketcluster.authToken', token);
.
See HTTP authentication flow here: https://socketcluster.io/docs/authentication/#http-flow
That said, for the JWT token to be considered valid by the SC server, you will need to know the secret key which was used to sign that token (by that microservice) and you will need to provide it when instantiating the SocketCluster server using the authKey
option in the constructor: https://socketcluster.io/docs/api-ag-server/#methods
I am using react native and just want to set the token rather than having it read from local storage? setAuthToken()? I tried passing through authToken on 'create'? can I emit it? Any help is much appreciated