SocketCluster / socketcluster-client

JavaScript client for SocketCluster
MIT License
290 stars 92 forks source link

Can I set the jwtToken? #130

Closed mzupek closed 4 years ago

mzupek commented 4 years ago

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

jondubois commented 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.

mzupek commented 4 years ago

@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

mzupek commented 4 years ago

Closing

kishor82 commented 1 year ago

@mzupek , Would have been helpful if you had commented on solutions before closing this thread.

jondubois commented 1 year ago

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