Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add refresh_token support to NakamaClient.restore_session #208

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,31 @@ 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
print(session.user_id)
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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small edit:"...check at startup if they have expired. If the auth token has expired..."


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

Expand Down
7 changes: 4 additions & 3 deletions addons/com.heroiclabs.nakama/client/NakamaClient.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)" % [
Expand Down