- Session storage abstractions for different environments
- Session persistence and synchronization
- Automatic token refresh mechanisms
Classes
CookieStorage
Cookie-based storage implementation. This storage uses web browser cookies to store the session so it’s not available in server-side environments. It is useful though for synchronizing sessions between client and server environments.Implements
Constructors
Constructor
Parameters
Parameter | Type | Description |
---|---|---|
options? | { cookieName? : string ; expirationDays? : number ; sameSite? : "strict" | "lax" | "none" ; secure? : boolean ; } | Configuration options |
options.cookieName? | string | Name of the cookie to use (defaults to “nhostSession”) |
options.expirationDays? | number | Number of days until the cookie expires (defaults to 30) |
options.sameSite? | "strict" | "lax" | "none" | SameSite policy for the cookie (defaults to “lax”) |
options.secure? | boolean | Whether to set the Secure flag on the cookie (defaults to true) |
Returns
CookieStorage
Methods
get()
Returns
null
| Session
The stored session or null if not found
Implementation of
SessionStorageBackend
.get
remove()
Returns
void
Implementation of
SessionStorageBackend
.remove
set()
Parameters
Parameter | Type | Description |
---|---|---|
value | Session | The session to store |
Returns
void
Implementation of
SessionStorageBackend
.set
LocalStorage
Browser localStorage implementation of StorageInterface. Persists the session across page reloads and browser restarts.Implements
Constructors
Constructor
Parameters
Parameter | Type | Description |
---|---|---|
options? | { storageKey? : string ; } | Configuration options |
options.storageKey? | string | The key to use in localStorage (defaults to “nhostSession”) |
Returns
LocalStorage
Methods
get()
Returns
null
| Session
The stored session or null if not found
Implementation of
SessionStorageBackend
.get
remove()
Returns
void
Implementation of
SessionStorageBackend
.remove
set()
Parameters
Parameter | Type | Description |
---|---|---|
value | Session | The session to store |
Returns
void
Implementation of
SessionStorageBackend
.set
MemoryStorage
In-memory storage implementation for non-browser environments or when persistent storage is not available or desirable.Implements
Constructors
Constructor
Returns
MemoryStorage
Methods
get()
Returns
null
| Session
The stored session or null if not set
Implementation of
SessionStorageBackend
.get
remove()
Returns
void
Implementation of
SessionStorageBackend
.remove
set()
Parameters
Parameter | Type | Description |
---|---|---|
value | Session | The session to store |
Returns
void
Implementation of
SessionStorageBackend
.set
SessionStorage
A wrapper around any SessionStorageInterface implementation that adds the ability to subscribe to session changes.Constructors
Constructor
Parameters
Parameter | Type | Description |
---|---|---|
storage | SessionStorageBackend | The underlying storage implementation to use |
Returns
SessionStorage
Methods
get()
Returns
null
| Session
The stored session or null if not found
onChange()
Parameters
Parameter | Type | Description |
---|---|---|
callback | SessionChangeCallback | Function that will be called when the session changes |
Returns
An unsubscribe function to remove this subscriptionReturns
void
remove()
Returns
void
set()
Parameters
Parameter | Type | Description |
---|---|---|
value | Session | The session to store |
Returns
void
Interfaces
DecodedToken
Decoded JWT token payload with processed timestamps and Hasura claimsIndexable
Properties
exp?
https://hasura.io/jwt/claims?
iat?
iss?
sub?
Session
User authentication session containing tokens and user informationExtends
Properties
accessToken
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
Inherited from
Session
.accessToken
accessTokenExpiresIn
900
Format - int64
Inherited from
Session
.accessTokenExpiresIn
decodedToken
refreshToken
"2c35b6f3-c4b9-48e3-978a-d4d0f1d42e24"
Pattern - \b[0-9a-f]\b-[0-9a-f]-[0-9a-f]-[0-9a-f]-\b[0-9a-f]\b
Inherited from
Session
.refreshToken
refreshTokenId
"2c35b6f3-c4b9-48e3-978a-d4d0f1d42e24"
Pattern - \b[0-9a-f]\b-[0-9a-f]-[0-9a-f]-[0-9a-f]-\b[0-9a-f]\b
Inherited from
Session
.refreshTokenId
user?
Inherited from
Session
.user
SessionStorageBackend
Session storage interface for session persistence. This interface can be implemented to provide custom storage solutions.Methods
get()
Returns
null
| Session
The stored session or null if not found
remove()
Returns
void
set()
Parameters
Parameter | Type | Description |
---|---|---|
value | Session | The session to store |
Returns
void
Type Aliases
SessionChangeCallback()
Parameters
Parameter | Type |
---|---|
session | Session | null |
Returns
void
Variables
DEFAULT_SESSION_KEY
Functions
detectStorage()
- Try to use localStorage if we’re in a browser environment
- Fall back to in-memory storage if localStorage isn’t available
Returns
SessionStorageBackend
The best available storage implementation as a SessionStorageBackend
refreshSession()
Parameters
Parameter | Type | Default value | Description |
---|---|---|---|
auth | Client | undefined | The authentication client to use for token refresh |
storage | SessionStorage | undefined | The session storage implementation |
marginSeconds | number | 60 | The number of seconds before the token expiration to refresh the session. If the token is still valid for this duration, it will not be refreshed. Set to 0 to force the refresh. |
Returns
Promise
<null
| Session
>
A promise that resolves to the current session (refreshed if needed) or null if no session exists