1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
import {
createContext,
type ReactNode,
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from "react";
import { ApiClientError, apiClient } from "../api/client";
import type { User } from "../api/types";
export interface AuthState {
user: User | null;
isAuthenticated: boolean;
isLoading: boolean;
}
export interface AuthActions {
login: (username: string, password: string) => Promise<void>;
register: (username: string, password: string) => Promise<void>;
logout: () => void;
}
export type AuthContextValue = AuthState & AuthActions;
const AuthContext = createContext<AuthContextValue | null>(null);
export interface AuthProviderProps {
children: ReactNode;
}
export function AuthProvider({ children }: AuthProviderProps) {
const [user, setUser] = useState<User | null>(null);
const [isLoading, setIsLoading] = useState(true);
// Check for existing auth on mount
useEffect(() => {
const tokens = apiClient.getTokens();
if (tokens) {
// We have tokens stored, but we don't have user info cached
// For now, just set authenticated state. User info will be fetched when needed.
// In a full implementation, we'd decode the JWT or call an API endpoint
setIsLoading(false);
} else {
setIsLoading(false);
}
}, []);
const login = useCallback(async (username: string, password: string) => {
const response = await apiClient.login(username, password);
setUser(response.user);
}, []);
const register = useCallback(
async (username: string, password: string) => {
await apiClient.register(username, password);
// After registration, log in automatically
await login(username, password);
},
[login],
);
const logout = useCallback(() => {
apiClient.logout();
setUser(null);
}, []);
const isAuthenticated = apiClient.isAuthenticated();
const value = useMemo<AuthContextValue>(
() => ({
user,
isAuthenticated,
isLoading,
login,
register,
logout,
}),
[user, isAuthenticated, isLoading, login, register, logout],
);
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
}
export function useAuth(): AuthContextValue {
const context = useContext(AuthContext);
if (!context) {
throw new Error("useAuth must be used within an AuthProvider");
}
return context;
}
export { ApiClientError };
|