di usage in website
This commit is contained in:
@@ -12,7 +12,9 @@ import {
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
import type { SessionViewModel } from '@/lib/view-models/SessionViewModel';
|
||||
import { useServices } from '@/lib/services/ServiceProvider';
|
||||
import { useCurrentSession } from '@/hooks/auth/useCurrentSession';
|
||||
import { useLogin } from '@/hooks/auth/useLogin';
|
||||
import { useLogout } from '@/hooks/auth/useLogout';
|
||||
|
||||
export type AuthContextValue = {
|
||||
session: SessionViewModel | null;
|
||||
@@ -31,29 +33,15 @@ interface AuthProviderProps {
|
||||
|
||||
export function AuthProvider({ initialSession = null, children }: AuthProviderProps) {
|
||||
const router = useRouter();
|
||||
const { sessionService, authService } = useServices();
|
||||
const [session, setSession] = useState<SessionViewModel | null>(initialSession);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
// Use React-Query hooks for session management
|
||||
const { data: session, isLoading, refetch: refreshSession } = useCurrentSession({
|
||||
initialData: initialSession,
|
||||
});
|
||||
|
||||
const fetchSession = useCallback(async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const current = await sessionService.getSession();
|
||||
setSession(current);
|
||||
} catch {
|
||||
setSession(null);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [sessionService]);
|
||||
|
||||
const refreshSession = useCallback(async () => {
|
||||
await fetchSession();
|
||||
}, [fetchSession]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchSession();
|
||||
}, [fetchSession]);
|
||||
// Use mutation hooks for login/logout
|
||||
const loginMutation = useLogin();
|
||||
const logoutMutation = useLogout();
|
||||
|
||||
const login = useCallback(
|
||||
(returnTo?: string) => {
|
||||
@@ -72,26 +60,29 @@ export function AuthProvider({ initialSession = null, children }: AuthProviderPr
|
||||
);
|
||||
|
||||
const logout = useCallback(async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
await authService.logout();
|
||||
setSession(null);
|
||||
await logoutMutation.mutateAsync();
|
||||
router.push('/');
|
||||
router.refresh();
|
||||
} finally {
|
||||
setLoading(false);
|
||||
} catch (error) {
|
||||
console.error('Logout failed:', error);
|
||||
router.push('/');
|
||||
}
|
||||
}, [authService, router]);
|
||||
}, [logoutMutation, router]);
|
||||
|
||||
const handleRefreshSession = useCallback(async () => {
|
||||
await refreshSession();
|
||||
}, [refreshSession]);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
session,
|
||||
loading,
|
||||
session: session ?? null,
|
||||
loading: isLoading,
|
||||
login,
|
||||
logout,
|
||||
refreshSession,
|
||||
refreshSession: handleRefreshSession,
|
||||
}),
|
||||
[session, loading, login, logout, refreshSession],
|
||||
[session, isLoading, login, logout, handleRefreshSession],
|
||||
);
|
||||
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
||||
|
||||
Reference in New Issue
Block a user