krishnasales_mobile/src/auth/context.ts

28 lines
813 B
TypeScript

import { createContext, useContext } from 'react';
import type { PasswordStatus } from '../api/types';
export interface PasswordChangeRequiredInfo {
message: string;
reason?: string;
rules?: string[];
}
export interface AuthValue {
authed: boolean;
isAdmin: boolean;
roles: string[];
userEmail: string | null;
passwordStatus: PasswordStatus | null;
login: (email: string, password: string, orgId?: string) => Promise<void>;
completePasswordChange: (email: string, currentPassword: string, newPassword: string, orgId?: string) => Promise<void>;
logout: () => void;
}
export const AuthCtx = createContext<AuthValue | null>(null);
export function useAuth(): AuthValue {
const v = useContext(AuthCtx);
if (!v) throw new Error('useAuth must be used within <AuthProvider>');
return v;
}