heroiclabs/nakama-defold/nakama.session
Create, check, store and restore Nakama sessions from their JWT tokens.
- GitHub: heroiclabs/nakama-defold — pinned to
v3.4.0- Pick a release from heroiclabs/nakama-defold releases and add its Source code (zip) URL (or a packaged
.zipasset, if the library ships one) togame.projectunder[project]dependencies, then Fetch Libraries in the Defold editor. - Run
bunx @defold-typescript/cli resolveto materialize its types. - Import it under a namespace alias of your choice:
import * as session from "nakama.session"
- Pick a release from heroiclabs/nakama-defold releases and add its Source code (zip) URL (or a packaged
Functions
is_token_expired_soon(session:Session): booleanis_token_expired(session:Session): booleanexpired(session:Session): booleanis_refresh_token_expired(session:Session): booleancreate(data:SessionData):Sessionstore(session:Session, id?: string): booleanrestore(id?: string):Session| undefined
is_token_expired_soon(session: Session): boolean
Whether the session's access token expires within the next twenty-four hours. Refresh the session while this is true rather than waiting for it to expire.
Parameters
session:Session— A session created withcreate.
Returns
boolean
is_token_expired(session: Session): boolean
Whether the session's access token has already expired.
Parameters
session:Session— A session created withcreate.
Returns
boolean
expired(session: Session): boolean
Upstream's backwards-compatible alias of is_token_expired, kept because older
code calls it. New code should call is_token_expired.
Parameters
session:Session— A session created withcreate.
Returns
boolean
is_refresh_token_expired(session: Session): boolean
Whether the session's refresh token has expired. A session created without a refresh token counts as expired, there being nothing left to refresh with.
Parameters
session:Session— A session created withcreate.
Returns
boolean
create(data: SessionData): Session
Builds a session from an authentication response, decoding the tokens it carries to read their expiry, username, user id and session variables.
Parameters
data:SessionData— The authentication response, which must carry atoken.
Returns
Session
store(session: Session, id?: string): boolean
Writes a session to disk under the project title, so it survives a restart.
Parameters
session:Session— The session to store.id?:string— Names the saved session. Defaults tonakama.
Returns
boolean— Whether the save succeeded.
restore(id?: string): Session | undefined
Reads back a session written by store.
Parameters
id?:string— The name the session was stored under. Defaults tonakama.
Returns
Session | undefined— The stored session, ornilif nothing usable was stored under that name.
Types
Session
Session.created: number
When create built this session, from os.time().
Session.token: string
The access token the server issued, as the JWT string it arrived as.
Session.expires: number
The access token's exp claim, in seconds since the epoch.
Session.username: string
The username the access token's usn claim carries.
Session.user_id: string
The user id the access token's uid claim carries.
Session.vars?: unknown
The session variables the access token's vrs claim carries, if any.
Session.refresh_token?: string
The refresh token the server issued, if the response carried one.
Session.refresh_token_expires?: number
The refresh token's exp claim, in seconds since the epoch.
Session.refresh_token_username?: string
The username the refresh token's usn claim carries.
Session.refresh_token_user_id?: string
The user id the refresh token's uid claim carries.
Session.refresh_token_vars?: unknown
The session variables the refresh token's vrs claim carries.