From 9261272f43b6cce4cbde5fb204af5fbe7c010266 Mon Sep 17 00:00:00 2001 From: Kevyn Roberts Date: Fri, 12 Apr 2024 21:58:48 -0400 Subject: [PATCH 1/2] Use refresh_token in NakamaClient.restore_session and update README.md (#207) --- README.md | 15 ++++++++++----- .../com.heroiclabs.nakama/client/NakamaClient.gd | 7 ++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1fe2ca1..8d20b6a 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ There's a variety of ways to [authenticate](https://heroiclabs.com/docs/nakama/c ### Sessions -When authenticated the server responds with an auth token (JWT) which contains useful properties and gets deserialized into a `NakamaSession` object. +When authenticated the server responds with an auth token (JWT) which contains useful properties and gets deserialized into a `NakamaSession` object. A refresh token is included with the session which can be used to renew the session with a new set of tokens. ```gdscript print(session.token) # raw JWT token @@ -65,18 +65,23 @@ When authenticated the server responds with an auth token (JWT) which contains u print(session.username) print("Session has expired: %s" % session.expired) print("Session expires at: %s" % session.expire_time) + print(session.refresh_token) + print("Session refresh token expires at: %s" % session.refresh_expire_time) ``` -It is recommended to store the auth token from the session and check at startup if it has expired. If the token has expired you must reauthenticate. The expiry time of the token can be changed as a setting in the server. +It is recommended to store the auth token and refresh token from the session and check at startup if it has expired. If the token has expired you can use the refresh token to refresh the session, after which you can save the new tokens. If both tokens have expired, you must reauthenticate. The expiry time of the tokens can be changed as a setting in the server. ```gdscript var authtoken = "restored from somewhere" - var session2 = NakamaClient.restore_session(authtoken) + var refreshtoken = "also restored from somewhere" + var session2 = NakamaClient.restore_session(authtoken, refreshtoken) if session2.expired: - print("Session has expired. Must reauthenticate!") + session2 = await client.session_refresh_async(session2) + if session2.is_exception(): + print("Session has expired and cannot be refreshed. Must reauthenticate!") ``` -NOTE: The length of the lifetime of a session can be changed on the server with the `--session.token_expiry_sec` command flag argument. +NOTE: The length of the lifetime of a session can be changed on the server with the `--session.token_expiry_sec` command flag argument. The refresh token expiry can be changed with `--session.refresh_token_expiry_sec`. ### Requests diff --git a/addons/com.heroiclabs.nakama/client/NakamaClient.gd b/addons/com.heroiclabs.nakama/client/NakamaClient.gd index 84ff608..15cbfe3 100644 --- a/addons/com.heroiclabs.nakama/client/NakamaClient.gd +++ b/addons/com.heroiclabs.nakama/client/NakamaClient.gd @@ -133,10 +133,11 @@ func _init(p_adapter : NakamaHTTPAdapter, # Restore a session from the auth token. # A `null` or empty authentication token will return `null`. -# @param authToken - The authentication token to restore as a session. +# @param auth_token - The authentication token to restore as a session. +# @param refresh_token - The refresh token to refresh an expired session's tokens. # Returns a session. -static func restore_session(auth_token : String): - return NakamaSession.new(auth_token, false) +static func restore_session(auth_token : String, refresh_token: String): + return NakamaSession.new(auth_token, false, refresh_token) func _to_string(): return "Client(Host='%s', Port=%s, Scheme='%s', ServerKey='%s', Timeout=%s)" % [ From a9fa2e000cfc0f3759e42dd83da1fdb4c6336e90 Mon Sep 17 00:00:00 2001 From: Kevyn Roberts Date: Sat, 13 Apr 2024 14:02:30 -0400 Subject: [PATCH 2/2] Add default value for refresh_token argument in restore_session --- addons/com.heroiclabs.nakama/client/NakamaClient.gd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/com.heroiclabs.nakama/client/NakamaClient.gd b/addons/com.heroiclabs.nakama/client/NakamaClient.gd index 15cbfe3..4c2477b 100644 --- a/addons/com.heroiclabs.nakama/client/NakamaClient.gd +++ b/addons/com.heroiclabs.nakama/client/NakamaClient.gd @@ -136,7 +136,7 @@ func _init(p_adapter : NakamaHTTPAdapter, # @param auth_token - The authentication token to restore as a session. # @param refresh_token - The refresh token to refresh an expired session's tokens. # Returns a session. -static func restore_session(auth_token : String, refresh_token: String): +static func restore_session(auth_token : String, refresh_token: String = ""): return NakamaSession.new(auth_token, false, refresh_token) func _to_string():