interface UseAuthResult

Returned by useAuth to any component that needs the session. Its functions are stable until the session's token changes.

Properties

The user's settings, once AuthProvider has read them. useSettings is the way to read and change them.

token: string | null
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.

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.

updateSession: (patch: Partial<Session>) => void

Merges patch into the session, in memory and in storage. The profile page uses it for a new avatar or email, the password dialog for fresh tokens.

authFetch: (
input: RequestInfo,
) => Promise<Response>

Calls fetch with 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: (
url: string,
body: FormData,
onProgress: (fraction: number | null) => void,
method?: string
) => Promise<Response>

Works like authFetch for a multipart upload that shows progress, as the dump creation dialog's file upload does. It goes through XHR, since fetch reports nothing until the whole body is sent. It still answers a Response, so callers use expectOk as usual.

onProgress receives a fraction from 0 to 1, or null once the body is sent and the server is working, or when the length is unknown.

Usage

import { type UseAuthResult } from "src/hooks/auth/useAuth.ts";