32 lines
825 B
TypeScript
32 lines
825 B
TypeScript
import { createContext, use } from "react";
|
|
|
|
import type { paths } from "@/shared/api";
|
|
|
|
type AuthContextValue = {
|
|
login: (email: string, password: string) => Promise<void>;
|
|
logout: () => Promise<void>;
|
|
ready: boolean;
|
|
register: (email: string, password: string) => Promise<void>;
|
|
retry: () => void;
|
|
signedOut: boolean;
|
|
/**
|
|
Refresh failed with a network error or 5xx; the refresh cookie may still be valid.
|
|
*/
|
|
unavailable: boolean;
|
|
user: undefined | User;
|
|
};
|
|
|
|
type User
|
|
= paths["/api/v1/auth/me"]["get"]["responses"][200]["content"]["application/json"];
|
|
|
|
const AuthContext = createContext<AuthContextValue | undefined>(undefined);
|
|
|
|
function useAuth() {
|
|
const context = use(AuthContext);
|
|
if (!context) throw new Error("AuthProvider is required");
|
|
|
|
return context;
|
|
}
|
|
|
|
export { AuthContext, useAuth, type User };
|