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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
import React, { createContext, useContext, useState, useEffect } from 'react';
import type { ReactNode } from 'react';
import { authApi } from '../utils/authApi';
import type { User, SessionResponse } from '../utils/authApi';
interface AuthContextType {
user: User | null;
isLoading: boolean;
isAuthenticated: boolean;
login: (username: string, password: string) => Promise<{ success: boolean; error?: string }>;
register: (userData: { username: string; email: string; password: string }) => Promise<{ success: boolean; error?: string }>;
logout: () => Promise<void>;
checkAuth: () => Promise<void>;
}
const AuthContext = createContext<AuthContextType | undefined>(undefined);
export const useAuth = () => {
const context = useContext(AuthContext);
if (context === undefined) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
};
interface AuthProviderProps {
children: ReactNode;
}
export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
const [user, setUser] = useState<User | null>(null);
const [isLoading, setIsLoading] = useState(true);
const isAuthenticated = user !== null;
const checkAuth = async () => {
try {
const response = await authApi.getSession();
if (response.error || !response.data) {
setUser(null);
return;
}
const sessionData = response.data as SessionResponse;
if (sessionData.authenticated && sessionData.user) {
setUser(sessionData.user);
} else {
setUser(null);
}
} catch (error) {
console.error('Auth check failed:', error);
setUser(null);
} finally {
setIsLoading(false);
}
};
const login = async (username: string, password: string) => {
try {
const response = await authApi.login({ username, password });
if (response.error) {
return { success: false, error: response.error };
}
if (response.data) {
setUser(response.data as User);
}
return { success: true };
} catch (error) {
console.error('Login failed:', error);
return { success: false, error: 'Network error. Please try again.' };
}
};
const register = async (userData: { username: string; email: string; password: string }) => {
try {
const response = await authApi.register(userData);
if (response.error) {
return { success: false, error: response.error };
}
if (response.data) {
setUser(response.data as User);
}
return { success: true };
} catch (error) {
console.error('Registration failed:', error);
return { success: false, error: 'Network error. Please try again.' };
}
};
const logout = async () => {
try {
await authApi.logout();
} catch (error) {
console.error('Logout error:', error);
} finally {
setUser(null);
}
};
useEffect(() => {
checkAuth();
}, []);
const value: AuthContextType = {
user,
isLoading,
isAuthenticated,
login,
register,
logout,
checkAuth,
};
return (
<AuthContext.Provider value={value}>
{children}
</AuthContext.Provider>
);
};
|