Files
lcbp3/frontend/components/auth/auth-sync.tsx
admin 863a727756
Some checks failed
Spec Validation / validate-markdown (push) Has been cancelled
Spec Validation / validate-diagrams (push) Has been cancelled
Spec Validation / check-todos (push) Has been cancelled
251208:1625 Frontend: to be complete admin panel, Backend: tobe recheck all task
2025-12-08 16:25:56 +07:00

40 lines
1.3 KiB
TypeScript

'use client';
import { useSession, signOut } from 'next-auth/react';
import { useEffect } from 'react';
import { useAuthStore } from '@/lib/stores/auth-store';
export function AuthSync() {
const { data: session, status } = useSession();
const { setAuth, logout } = useAuthStore();
useEffect(() => {
if (session?.error === 'RefreshAccessTokenError') {
signOut({ callbackUrl: '/login' });
} else if (status === 'authenticated' && session?.user) {
// Map NextAuth session to AuthStore user
// Assuming session.user has the fields we need based on types/next-auth.d.ts
// cast to any or specific type if needed, as NextAuth types might need assertion
const user = session.user as any;
setAuth(
{
id: user.id || user.user_id,
username: user.username,
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
role: user.role,
permissions: user.permissions // If backend/auth.ts provides this
},
session.accessToken || '' // If we store token in session
);
} else if (status === 'unauthenticated') {
logout();
}
}, [session, status, setAuth, logout]);
return null; // This component renders nothing
}