Move auth and timezone hooks into dedicated hook files
This commit is contained in:
@@ -10,7 +10,6 @@
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useMemo,
|
||||
useState,
|
||||
} from "react";
|
||||
@@ -25,7 +24,7 @@ interface AuthState {
|
||||
expiresAt: string | null;
|
||||
}
|
||||
|
||||
interface AuthContextValue {
|
||||
export interface AuthContextValue {
|
||||
/** `true` when a valid session token is held in state. */
|
||||
isAuthenticated: boolean;
|
||||
/**
|
||||
@@ -41,7 +40,7 @@ interface AuthContextValue {
|
||||
// Context
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const AuthContext = createContext<AuthContextValue | null>(null);
|
||||
export const AuthContext = createContext<AuthContextValue | null>(null);
|
||||
|
||||
const SESSION_KEY = "bangui_token";
|
||||
const SESSION_EXPIRES_KEY = "bangui_expires_at";
|
||||
@@ -97,22 +96,3 @@ export function AuthProvider({
|
||||
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Hook
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Access authentication state and actions.
|
||||
*
|
||||
* Must be called inside a component rendered within `<AuthProvider>`.
|
||||
*
|
||||
* @throws {Error} When called outside of `<AuthProvider>`.
|
||||
*/
|
||||
export function useAuth(): AuthContextValue {
|
||||
const ctx = useContext(AuthContext);
|
||||
if (ctx === null) {
|
||||
throw new Error("useAuth must be used within <AuthProvider>.");
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user