87 lines
2.1 KiB
TypeScript
87 lines
2.1 KiB
TypeScript
|
|
import { createContext, useContext, useState, useEffect, useCallback, ReactNode } from 'react';
|
||
|
|
import { useNavigate } from 'react-router-dom';
|
||
|
|
import { api, User, ApiError } from '@/lib/api';
|
||
|
|
|
||
|
|
interface AuthContextType {
|
||
|
|
user: User | null;
|
||
|
|
isLoading: boolean;
|
||
|
|
isAuthenticated: boolean;
|
||
|
|
login: (email: string, password: string, rememberMe?: boolean) => Promise<void>;
|
||
|
|
logout: () => Promise<void>;
|
||
|
|
refreshUser: () => Promise<void>;
|
||
|
|
}
|
||
|
|
|
||
|
|
const AuthContext = createContext<AuthContextType | null>(null);
|
||
|
|
|
||
|
|
export function AuthProvider({ children }: { children: ReactNode }) {
|
||
|
|
const [user, setUser] = useState<User | null>(null);
|
||
|
|
const [isLoading, setIsLoading] = useState(true);
|
||
|
|
const navigate = useNavigate();
|
||
|
|
|
||
|
|
const refreshUser = useCallback(async () => {
|
||
|
|
try {
|
||
|
|
const response = await api.users.me();
|
||
|
|
setUser(response.user);
|
||
|
|
} catch (error) {
|
||
|
|
if (error instanceof ApiError && error.code === 401) {
|
||
|
|
setUser(null);
|
||
|
|
}
|
||
|
|
// Silently fail for other errors during refresh
|
||
|
|
}
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
// Check session on mount
|
||
|
|
useEffect(() => {
|
||
|
|
const checkSession = async () => {
|
||
|
|
try {
|
||
|
|
const response = await api.users.me();
|
||
|
|
setUser(response.user);
|
||
|
|
} catch {
|
||
|
|
setUser(null);
|
||
|
|
} finally {
|
||
|
|
setIsLoading(false);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
checkSession();
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
const login = useCallback(async (email: string, password: string, rememberMe = false) => {
|
||
|
|
const response = await api.auth.login(email, password, rememberMe);
|
||
|
|
setUser(response.user);
|
||
|
|
navigate('/profile');
|
||
|
|
}, [navigate]);
|
||
|
|
|
||
|
|
const logout = useCallback(async () => {
|
||
|
|
try {
|
||
|
|
await api.auth.logout();
|
||
|
|
} finally {
|
||
|
|
setUser(null);
|
||
|
|
navigate('/login');
|
||
|
|
}
|
||
|
|
}, [navigate]);
|
||
|
|
|
||
|
|
return (
|
||
|
|
<AuthContext.Provider
|
||
|
|
value={{
|
||
|
|
user,
|
||
|
|
isLoading,
|
||
|
|
isAuthenticated: !!user,
|
||
|
|
login,
|
||
|
|
logout,
|
||
|
|
refreshUser,
|
||
|
|
}}
|
||
|
|
>
|
||
|
|
{children}
|
||
|
|
</AuthContext.Provider>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useAuth() {
|
||
|
|
const context = useContext(AuthContext);
|
||
|
|
if (!context) {
|
||
|
|
throw new Error('useAuth must be used within an AuthProvider');
|
||
|
|
}
|
||
|
|
return context;
|
||
|
|
}
|