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..4c2477b 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)" % [