Files
PacketStation/frontend/src/auth/context.ts
T

28 lines
1.1 KiB
TypeScript
Raw Normal View History

import { createContext } from "react";
import type { Department, Factor, Me, PresenceUser } from "./types";
export type AuthStatus = "loading" | "anonymous" | "ready" | "locked";
export interface AuthState {
status: AuthStatus;
/** Null while loading, when signed out, and when a 423 arrived before /me answered. */
me: Me | null;
departments: Department[];
/** Shown on the sign-in page once, for example after a session ended. */
notice: string | null;
presence: PresenceUser[];
refresh: () => Promise<void>;
signInPassword: (login: string, password: string) => Promise<Me>;
signInPin: (empNo: string, pin: string) => Promise<Me>;
signOut: () => Promise<void>;
/** Drops the session in the browser without calling the server, with a notice for the sign-in page. */
endSession: (notice: string | null) => void;
unlock: (factor: Factor, secret: string) => Promise<Me>;
lock: () => Promise<void>;
/** Replaces the cached Me, for example after a PIN change. */
setMe: (me: Me) => void;
roleLabel: (role: string) => string;
}
export const AuthCtx = createContext<AuthState | null>(null);