Gives components the session, and the functions that sign in, sign out and make requests with its token.
Returns the session and the functions that act on it. It must be called
under AuthProvider.
Returns the session, for a page that RestrictedLoggedIn guards, such as
DumpEdit, so it needn't check for a user.
Returned by useAuth to any component that needs the session. Its functions are stable until the session's token changes.
-
authFetch: () => Promise<Response>input: RequestInfo,init?: RequestInit
Calls
fetchwith the session's token, and a JSON content type unless the body is FormData. An expired token, or a 401, ends the session. An expired token answers a 401 without calling the server. -
authUpload: () => Promise<Response>url: string,body: FormData,onProgress: (fraction: number | null) => void,method?: string
Works like
authFetchfor a multipart upload that shows progress, as the dump creation dialog's file upload does. It goes through XHR, sincefetchreports nothing until the whole body is sent. It still answers aResponse, so callers useexpectOkas usual. -
login: (response: AuthResponse) => void
Starts a session from a sign-in or sign-up response, in every tab.
-
logout: () => void
Ends the session this render holds. A 401 can land after the session has moved on to fresh tokens (a password change, here or in another tab), and must not sign those out.
-
mediaToken: string | null
The token for file and thumbnail URLs, where it shows in the query string. A session from before media tokens falls back to the session token until AuthProvider fetches one.
-
settings: UserSettings | null
The user's settings, once
AuthProviderhas read them.useSettingsis the way to read and change them. - token: string | null
-
updateSession: (patch: Partial<Session>) => void
Merges
patchinto the session, in memory and in storage. The profile page uses it for a new avatar or email, the password dialog for fresh tokens. - user: SelfUser | null
Returned by useRequiredAuth. It is UseAuthResult with a user that can't be null.
Usage
import * as mod from "src/hooks/auth/useAuth.ts";