2025-07-22 07:37:51 +02:00
|
|
|
// src/contexts/AuthContext.tsx
|
|
|
|
|
import React, { createContext, useContext, useEffect, useState, useCallback } from 'react';
|
2025-07-22 08:50:18 +02:00
|
|
|
import { authService, UserProfile } from '../api/services/authService';
|
2025-07-22 07:37:51 +02:00
|
|
|
import { tokenManager } from '../api/auth/tokenManager';
|
2025-07-17 13:54:51 +02:00
|
|
|
|
|
|
|
|
interface AuthContextType {
|
2025-07-22 07:37:51 +02:00
|
|
|
user: UserProfile | null;
|
|
|
|
|
isAuthenticated: boolean;
|
|
|
|
|
isLoading: boolean;
|
2025-07-17 13:54:51 +02:00
|
|
|
login: (email: string, password: string) => Promise<void>;
|
2025-07-22 07:37:51 +02:00
|
|
|
register: (data: any) => Promise<void>;
|
|
|
|
|
logout: () => Promise<void>;
|
|
|
|
|
updateProfile: (updates: Partial<UserProfile>) => Promise<void>;
|
|
|
|
|
refreshUser: () => Promise<void>;
|
2025-07-17 13:54:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-22 07:37:51 +02:00
|
|
|
const AuthContext = createContext<AuthContextType | null>(null);
|
2025-07-17 13:54:51 +02:00
|
|
|
|
|
|
|
|
export const useAuth = () => {
|
|
|
|
|
const context = useContext(AuthContext);
|
|
|
|
|
if (!context) {
|
|
|
|
|
throw new Error('useAuth must be used within an AuthProvider');
|
|
|
|
|
}
|
|
|
|
|
return context;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
2025-07-22 07:37:51 +02:00
|
|
|
const [user, setUser] = useState<UserProfile | null>(null);
|
|
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
2025-07-17 13:54:51 +02:00
|
|
|
|
2025-07-22 07:37:51 +02:00
|
|
|
// Initialize auth state
|
2025-07-17 13:54:51 +02:00
|
|
|
useEffect(() => {
|
2025-07-22 07:37:51 +02:00
|
|
|
const initAuth = async () => {
|
|
|
|
|
try {
|
|
|
|
|
await tokenManager.initialize();
|
|
|
|
|
|
|
|
|
|
if (authService.isAuthenticated()) {
|
|
|
|
|
const profile = await authService.getCurrentUser();
|
|
|
|
|
setUser(profile);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Auth initialization failed:', error);
|
|
|
|
|
} finally {
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
initAuth();
|
2025-07-17 13:54:51 +02:00
|
|
|
}, []);
|
|
|
|
|
|
2025-07-22 07:37:51 +02:00
|
|
|
const login = useCallback(async (email: string, password: string) => {
|
|
|
|
|
const profile = await authService.login({ email, password });
|
|
|
|
|
setUser(profile);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const register = useCallback(async (data: any) => {
|
|
|
|
|
const profile = await authService.register(data);
|
|
|
|
|
setUser(profile);
|
|
|
|
|
}, []);
|
2025-07-17 13:54:51 +02:00
|
|
|
|
2025-07-22 07:37:51 +02:00
|
|
|
const logout = useCallback(async () => {
|
|
|
|
|
await authService.logout();
|
|
|
|
|
setUser(null);
|
|
|
|
|
}, []);
|
2025-07-17 13:54:51 +02:00
|
|
|
|
2025-07-22 07:37:51 +02:00
|
|
|
const updateProfile = useCallback(async (updates: Partial<UserProfile>) => {
|
|
|
|
|
const updated = await authService.updateProfile(updates);
|
|
|
|
|
setUser(updated);
|
|
|
|
|
}, [updateProfile]);
|
|
|
|
|
|
|
|
|
|
const refreshUser = useCallback(async () => {
|
|
|
|
|
if (authService.isAuthenticated()) {
|
|
|
|
|
const profile = await authService.getCurrentUser();
|
|
|
|
|
setUser(profile);
|
2025-07-17 13:54:51 +02:00
|
|
|
}
|
2025-07-22 07:37:51 +02:00
|
|
|
}, []);
|
2025-07-17 13:54:51 +02:00
|
|
|
|
2025-07-22 07:37:51 +02:00
|
|
|
// Set up token refresh interval
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!user) return;
|
|
|
|
|
|
|
|
|
|
// Check token expiry every minute
|
|
|
|
|
const interval = setInterval(async () => {
|
|
|
|
|
try {
|
|
|
|
|
await tokenManager.getAccessToken(); // This will refresh if needed
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Token refresh failed:', error);
|
|
|
|
|
await logout();
|
|
|
|
|
}
|
|
|
|
|
}, 60000); // 1 minute
|
|
|
|
|
|
|
|
|
|
return () => clearInterval(interval);
|
|
|
|
|
}, [user, logout]);
|
2025-07-17 13:54:51 +02:00
|
|
|
|
|
|
|
|
return (
|
2025-07-22 07:37:51 +02:00
|
|
|
<AuthContext.Provider
|
|
|
|
|
value={{
|
|
|
|
|
user,
|
|
|
|
|
isAuthenticated: !!user,
|
|
|
|
|
isLoading,
|
|
|
|
|
login,
|
|
|
|
|
register,
|
|
|
|
|
logout,
|
|
|
|
|
updateProfile,
|
|
|
|
|
refreshUser
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-07-17 13:54:51 +02:00
|
|
|
{children}
|
|
|
|
|
</AuthContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
};
|