Nakama unable refresh token (SOLVED)

I tried the possibilities of “Nakama”, everything worked, but I deleted the user in the nakama console and the error.

I can no longer connect from this client…
Why?

DEBUG:SCRIPT: {"code":16, "message":"Refresh token invalid or expired."}
DEBUG:SCRIPT: Unable to refresh session

Nakama will store a session token if you call session.store(s). The session can later be restored by calling session.restore().

It is likely that you are restoring a stored but expired token somewhere in your code. If this is the case you should instead create a new session.

You can check if a session has expired by calling session.expired(s).

1 Like

I used the example you provided.

It’s just that when the client was deleted in the console itself, for some reason it stopped connecting to the server

local function login(client)
	-- restore a session
	local session = nakama_session.restore()

	local success = true

	if session and nakama_session.is_token_expired_soon(session) and not nakama_session.is_refresh_token_expired(session) then
		log("Session has expired or is about to expire. Refreshing.")
		success = refresh_session(client, session)
	elseif not session or nakama_session.is_refresh_token_expired(session) then
		log("Session does not exist or it has expired. Must reauthenticate.")
		success = email_login(client, "nana", "testPass", "testLog")
	else
		client.set_bearer_token(session.token)
	end
	return success
end

This is where the session is reloaded.

When exactly do you get “Refresh token invalid or expired”?

I ran nakama locally.
Installed dependencies in the engine. I took your example from github. Collected the project, the first time everything went fine. But when I deleted the user in the Nakama console and rebuilt the project. It started.

I was more asking which line of code that is causing the problem. Is it the reauthentication? Ie the line where you call login_email()?

I delete the user in Nakama, I rebuild the client, the token is not reloaded

DEBUG:SCRIPT: {"code":16, "message":"Refresh token invalid or expired."}
DEBUG:SCRIPT: Unable to refresh session

The token should reload thanks to this function, but it doesn’t work.

local function refresh_session(client, session)
	session = nakama.session_refresh(client, session.refresh_token)
	if session.token then
		nakama_session.store(session)
		client.set_bearer_token(session.token)
		return true
	end
	log("Unable to refresh session")
	return false
end

i use your git example

Ok, thanks. Well, I think my example is broken and doesn’t correctly handle this scenario. This is the relevant code from the example:

local function login(client)
	-- restore a session
	local session = nakama_session.restore()

	local success = true

	if session and nakama_session.is_token_expired_soon(session) and not nakama_session.is_refresh_token_expired(session) then
		log("Session has expired or is about to expire. Refreshing.")
		success = refresh_session(client, session)
	elseif not session or nakama_session.is_refresh_token_expired(session) then
		log("Session does not exist or it has expired. Must reauthenticate.")
		success = email_login(client, "bjorn@defold.se", "foobar123", "britzl")
	else
		client.set_bearer_token(session.token)
	end
	return success
end

And it seems like it fails here:

success = refresh_session(client, session)

This means that there is a stored session which has expired, but has a non-expired refresh token. The problem in this case is that the stored session is linked to a deleted user. The solution should be to change the login function to do a fresh login if the refresh fails. Like this (untested!):

local function login(client)
	-- restore a session
	local session = nakama_session.restore()

	-- use existing session token if it exists and doesn't expire soon
	if session and not nakama_session.is_token_expired_soon(session) then
		log("Session is good to use!")
		client.set_bearer_token(session.token)
		return true
	end

	-- if existing session token is about to expire we need to refresh it
	if session and nakama_session.is_token_expired_soon(session) and not nakama_session.is_refresh_token_expired(session) then
		log("Session has expired or is about to expire. Refreshing.")
		local success = refresh_session(client, session)
		-- session was refreshed, early-out!
		if success then
			log("Session was successfully refreshed!")
			return true
		end
	end

	-- if no session token exists or it has expired we need to log in again
	log("Session does not exist or it has expired. Must reauthenticate.")
	local success = email_login(client, "bjorn@defold.se", "foobar123", "britzl")
	return success
end
1 Like

Oh, It really works. Thank you.

Can you send me a couple of articles on how to organize the network architecture in nakama?

I want to try to implement a passive multiplayer strategy. So that many players get on a huge map during authorization by sending requests to the server to update the data.

Each action in my strategy will be like a call to the server and a check
user statistics.

I would like to know how the server processes commands?
Sorry, I’m just learning how to implement a backend

Check the Nakama documentation! They have really good documentation overall.

1 Like

okey, thank u