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