Locking
Restore a saved session as locked until the user unlocks it, for example with biometrics, in native apps.
A banking or health app often asks a returning user to unlock it, with Face ID, a fingerprint or a passcode, before it shows anything. The user is still signed in, but the app shouldn't use their tokens until they prove it's them. Locking gives you that: a saved session restores as locked, with the user known and the tokens sealed until your app unlocks them.
This page explains what locked means, what you provide, and how unlocking and locking again behave. For the Expo how-to, with a key behind the device's biometrics, see Unlock with biometrics.
Turn on locking
Pass a locking option to createAuth, with a seal function that encrypts
the tokens and an open function that decrypts them:
import { , type } from "@monarcode/session-kit";
import { } from "./backend.js";
// Your app's own: a native secure store, and encryption with a key it loads
// when the user unlocks.
declare const : ;
declare const : ;
declare function (: , : string): <string>;
declare function (: , : string): <string>;
export const = ({
: "my-native-app",
: ,
,
: {
// Runs whenever the tokens are saved.
: () => (, ),
// Runs only from `unlock()`.
: () => (, ),
},
});
/** After the app has its key: resolves the session, `authenticated`. */
export const = () => .();
/** `locked` again. The saved session stays, and the tokens leave memory. */
export const = () => .();Sealing is yours: Session kit depends on no crypto or biometrics library.
The user and the session ID are saved beside the sealed tokens, where they
can be read while locked. locking: true saves the tokens as they are, for
a lock screen in front of an otherwise ordinary session.
What locked means
A saved session restores as locked. user and sessionId are set, so a
"Welcome back" screen can greet the user by name, but the tokens stay sealed
until auth.unlock().
While the session is locked, nothing waits for an unlock that might never come:
getSession,retry,refresh,updateUser,credentials.getandcredentials.renewreject withLOCKED. So doesauthFetch.- Nothing refreshes, and the expiry timers are off.
- The router adapters treat a locked session as signed out.
useris still set, so checkstatus, notuser, to know whether the app has access. State shows how.
Two calls still work while locked:
signInreplaces the locked session, so a user who can't unlock can sign in again.signOutends it, without calling yourrevokeoption: the tokens aren't in memory to revoke. A session thatlock()is still locking, while a refresh finishes, is revoked as usual.
Seal with a key
With seal and open in locking, every save calls seal: a sign-in's, a
refresh's and a profile update's. If seal throws, as it does when your app
doesn't have the key, the save fails with PERSISTENCE_FAILED, and no one is
signed in. The one exception is a session saved before locking was turned
on: unlocking seals it, and if that seal fails, the user stays signed in
and the saved session stays as it was until the next save.
So your app needs the key whenever it saves, not only when it unlocks:
- On a password sign-in, make a new key before you call
signIn, aswithNewKeydoes in Unlock with biometrics. - While locking, keep the key until
lock()settles. A refresh already running when you calllock()still saves its tokens, and saving seals them.
Unlock
auth.unlock() reads the saved tokens, opens them with your open function,
and resolves the session, authenticated again. If the access token has
expired, it refreshes it first. Overlapping calls share one unlock, and when
the session isn't locked, unlock() acts like getSession().
A failed unlock doesn't always leave the session locked. Here's what each
outcome does, so you can check status after a rejection:
| When… | unlock()… | The session afterwards |
|---|---|---|
open throws, or doesn't return a string | Rejects with UNLOCK_FAILED | Still locked |
| Storage can't be read | Rejects with PERSISTENCE_FAILED | Still locked |
What open returns isn't this session's tokens | Rejects with INVALID_SESSION | Signed out |
The saved session is gone, or older than maxAge | Resolves null | Signed out |
| The backend rejects the refresh token | Resolves null | Signed out |
| The refresh fails for another reason | Rejects with REFRESH_FAILED | Unlocked, and unavailable until retry() |
| The user signs in or out while it runs | Rejects with SESSION_CHANGED | The new session, or signed out |
Lock again
auth.lock() locks a session that's in use, such as when the app goes to
the background. status turns locked at once, and the timers stop. The
saved session and user stay.
The tokens leave memory when lock() resolves. If a refresh is running,
lock() waits for it to save its tokens first: cancelling it could lose a
refresh token your backend has already replaced.
lock() does nothing when no one is signed in or the session is already
locked.
lock() and unlock() reject with INVALID_OPTIONS on a client created
without locking.
Limits
- Native apps only. Locking is built for one JavaScript context. With
locking, tabs don't follow each other, and the client doesn't check for changes that another context saved. - It needs a user schema. A user read from the access token can't be
shown without its token, so
createAuththrowsINVALID_OPTIONSforlockingwithfromAccessToken. - Not with server rendering. The browser's client for a session kept on
the server has no
lockorunlock. - Turning it on later works: a session saved before
lockingwas added restores aslockedtoo, and is sealed when it's unlocked. - Turning it off later makes sealed sessions unreadable. Those users are
signed out, with
INVALID_SESSIONinstate.error.
Next steps
- Unlock with biometrics: a key behind the device's biometrics, AES-GCM sealing, and a welcome-back screen in Expo.
- Expo Router: set up the app the lock screen sits in.
AuthLockingreference:sealandopen.