feat(admin): implement Phase 3 Admin SPA Router and Dual-Token Auth integration
This commit is contained in:
1 parent
71e19bfe43
commit
eaa2621a17
11 files changed
+561
-153
No files matched your search
@@ -1,43 +1,75 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Navigate, Outlet } from 'react-router-dom';
|
||||
import api from '../services/api';
|
||||
import { useAdminAuthStore } from '../store/adminAuthStore';
|
||||
import type { ApiResponse, AuthResponseData, AdminUser } from '../types/admin';
|
||||
|
||||
export default function ProtectedRoute() {
|
||||
const token = localStorage.getItem('adminToken');
|
||||
const { isAuthenticated, accessToken, setAuth, clearAuth } = useAdminAuthStore();
|
||||
const [isValidating, setIsValidating] = useState(true);
|
||||
const [isValid, setIsValid] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
if (!token) {
|
||||
setIsValid(false);
|
||||
setIsValidating(false);
|
||||
return;
|
||||
}
|
||||
let isMounted = true;
|
||||
|
||||
api.get('/users/profile')
|
||||
.then(() => {
|
||||
setIsValid(true);
|
||||
})
|
||||
.catch(() => {
|
||||
localStorage.removeItem('adminToken');
|
||||
setIsValid(false);
|
||||
})
|
||||
.finally(() => {
|
||||
const validateSession = async () => {
|
||||
if (isAuthenticated && accessToken) {
|
||||
setIsValidating(false);
|
||||
});
|
||||
}, [token]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!token || !isValid) {
|
||||
return <Navigate to="/login" replace />;
|
||||
}
|
||||
try {
|
||||
const response = await api.post<ApiResponse<AuthResponseData>>('/auth/refresh');
|
||||
if (response.data?.success && response.data.data?.accessToken) {
|
||||
if (isMounted) {
|
||||
setAuth(response.data.data.accessToken, response.data.data.user);
|
||||
setIsValidating(false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
// Fallback profile check if token already in memory
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
try {
|
||||
const profileRes = await api.get<ApiResponse<AdminUser>>('/users/profile');
|
||||
if (profileRes.data?.data && isMounted) {
|
||||
setAuth(accessToken, profileRes.data.data);
|
||||
setIsValidating(false);
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
// Token invalid
|
||||
}
|
||||
}
|
||||
|
||||
if (isMounted) {
|
||||
clearAuth();
|
||||
setIsValidating(false);
|
||||
}
|
||||
};
|
||||
|
||||
validateSession();
|
||||
|
||||
return () => {
|
||||
isMounted = false;
|
||||
};
|
||||
}, [isAuthenticated, accessToken, setAuth, clearAuth]);
|
||||
|
||||
if (isValidating) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-gray-50">
|
||||
<div className="w-8 h-8 border-4 border-purple-600 border-t-transparent rounded-full animate-spin"></div>
|
||||
<div className="min-h-screen flex items-center justify-center bg-gray-50 font-vazir" dir="rtl">
|
||||
<div className="flex flex-col items-center gap-3">
|
||||
<div className="w-10 h-10 border-4 border-purple-600 border-t-transparent rounded-full animate-spin"></div>
|
||||
<span className="text-sm font-bold text-gray-500">در حال اعتبارسنجی نشست کاری ادمین...</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!useAdminAuthStore.getState().isAuthenticated) {
|
||||
return <Navigate to="/login" replace />;
|
||||
}
|
||||
|
||||
return <Outlet />;
|
||||
}
|
||||
Reference in new issue
Block a user