I have a question around authentication and sessions. When I do a successful login, I receive both token and refreshToken and store them on client side.
But when I restart the Nakama server, token is not valid anymore and I try to call sessionRefresh method, but the refreshToken is also invalid.
Hi @aminalaee, that is the expected behaviour to invalidate all auth tokens when the server restarts. Furthermore, even if tokens never got invalidated they’d still expire. We’d recommend catering for re-authenticating workflows in your game.
Hello Sean,
Thanks for the reply. I ununderstand that I would handle token expriy, but what if I want to update my Go plugins, I need to restart the server, right?
Why would all users need to authenticate again because some minor backend change in the plugins?
Am I missing something?
What you’re observing is not a production-readiness consideration, just a scenario that has to be handled by clients no matter what: tokens can expire, be invalidated by a logout or server-triggered ban operation, or become invalid for any other reason including the signing key on the server being changed.
Clients must always be prepared to handle server responses that say their session token and/or refresh token is invalid, and then re-authenticate as needed.
Sorry the last question I have is that what is the suggested way of doing that with Nakama?
Let’s say the authentication is with Email and Password, and after server restarts how can the client re-authenticate seamlessly without storing the email/password locally to do so?
To be more specific it’s not clear to me why the refresh token is invalidated after the restart, there’s no way to get the token again after that part.
What I said applies to all tokens, including refresh tokens. Having tokens that never expire or get invalidated is bad security practice.
As mentioned you should be prepared to handle token rejection at any point. If the session token is rejected, try to refresh it with the refresh token. If the refresh token is rejected, prompt the user to re-authenticate (or do it seamlessly via methods like device ID).
In your case I recommend linking both email/password and device ID to a Nakama user account. That way you can seamlessly re-authenticate using the device ID without prompting the user for their credentials again.