Skip to Content
defold-typescript libraries
heroiclabsnakama-defold

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
    1. Pick a release from heroiclabs/nakama-defold releases and add its Source code (zip) URL (or a packaged .zip asset, if the library ships one) to game.project under [project] dependencies, then Fetch Libraries in the Defold editor.
    2. Run bunx @defold-typescript/cli resolve to materialize its types.
    3. Import it under a namespace alias of your choice:
      import * as session from "nakama.session"

Functions

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

Returns

  • boolean

is_token_expired(session: Session): boolean

Whether the session's access token has already expired.

Parameters

  • session: Session — A session created with create.

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

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

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

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

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

Returns

  • Session | undefined — The stored session, or nil if 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.

SessionData

SessionData.token: string

SessionData.refresh_token?: string

On this page