Keeps the user signed in across reloads and tabs, in one localStorage entry every tab shares. AuthProvider and useAuth are its only users.

Functions

f
forgetStoredSession(token: string | undefined): void

Forgets the stored session, but only if it still holds token. Another tab may have replaced it with fresh tokens from a password change, and a 401 for the old token here must not wipe those.

f
isTokenExpired(token: string): boolean

Checks whether token, a JWT, has expired, without asking the server. An unreadable one counts as expired.

f
readStoredSession(): Session | null

Returns the stored session, or null. A session that is unreadable, expired or from before roles existed is removed from storage.

f
storeSession(session: Session): void

Stores session, for every tab.

Type Aliases

T
Session =
Omit<AuthResponse, "mediaToken">
& { mediaToken?: string; settings?: UserSettings; }

Held by AuthProvider and stored here. It is the AuthResponse of a sign-in or sign-up, except that one stored before media tokens existed has none until AuthProvider fetches it. AuthProvider adds the user's settings once it has read them from the server, and useSettings keeps them current.

Variables

v
AUTH_STORAGE_KEY: "authResponse"

The localStorage key of the session. AuthProvider listens for changes to it from other tabs.