Fix admin panel authentication and navigation for static export
- Replace router.push with window.location.href for reliable redirects - Fix auth check to wait for component mount before accessing localStorage - Ensure proper redirect after successful login - Fix redirect behavior on all admin pages (dashboard, colors, requests)
This commit is contained in:
@@ -53,13 +53,16 @@ export default function ColorsManagement() {
|
|||||||
|
|
||||||
// Check authentication
|
// Check authentication
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
// Wait for component to mount to avoid SSR issues with localStorage
|
||||||
|
if (!mounted) return;
|
||||||
|
|
||||||
const token = localStorage.getItem('authToken');
|
const token = localStorage.getItem('authToken');
|
||||||
const expiry = localStorage.getItem('tokenExpiry');
|
const expiry = localStorage.getItem('tokenExpiry');
|
||||||
|
|
||||||
if (!token || !expiry || Date.now() > parseInt(expiry)) {
|
if (!token || !expiry || Date.now() > parseInt(expiry)) {
|
||||||
router.push('/upadaj');
|
window.location.href = '/upadaj';
|
||||||
}
|
}
|
||||||
}, [router]);
|
}, [mounted]);
|
||||||
|
|
||||||
// Fetch colors
|
// Fetch colors
|
||||||
const fetchColors = async () => {
|
const fetchColors = async () => {
|
||||||
|
|||||||
@@ -123,13 +123,16 @@ export default function AdminDashboard() {
|
|||||||
|
|
||||||
// Check authentication
|
// Check authentication
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
// Wait for component to mount to avoid SSR issues with localStorage
|
||||||
|
if (!mounted) return;
|
||||||
|
|
||||||
const token = localStorage.getItem('authToken');
|
const token = localStorage.getItem('authToken');
|
||||||
const expiry = localStorage.getItem('tokenExpiry');
|
const expiry = localStorage.getItem('tokenExpiry');
|
||||||
|
|
||||||
if (!token || !expiry || Date.now() > parseInt(expiry)) {
|
if (!token || !expiry || Date.now() > parseInt(expiry)) {
|
||||||
router.push('/upadaj');
|
window.location.href = '/upadaj';
|
||||||
}
|
}
|
||||||
}, [router]);
|
}, [mounted]);
|
||||||
|
|
||||||
// Fetch filaments
|
// Fetch filaments
|
||||||
const fetchFilaments = async () => {
|
const fetchFilaments = async () => {
|
||||||
|
|||||||
@@ -32,8 +32,8 @@ export default function AdminLogin() {
|
|||||||
// Track successful login
|
// Track successful login
|
||||||
trackEvent('Admin', 'Login', 'Success');
|
trackEvent('Admin', 'Login', 'Success');
|
||||||
|
|
||||||
// Redirect to admin dashboard
|
// Redirect to admin dashboard using window.location for static export
|
||||||
router.push('/upadaj/dashboard');
|
window.location.href = '/upadaj/dashboard';
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
setError('Neispravno korisničko ime ili lozinka');
|
setError('Neispravno korisničko ime ili lozinka');
|
||||||
console.error('Login error:', err);
|
console.error('Login error:', err);
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ export default function ColorRequestsAdmin() {
|
|||||||
const expiry = localStorage.getItem('tokenExpiry');
|
const expiry = localStorage.getItem('tokenExpiry');
|
||||||
|
|
||||||
if (!token || !expiry || new Date().getTime() > parseInt(expiry)) {
|
if (!token || !expiry || new Date().getTime() > parseInt(expiry)) {
|
||||||
router.push('/upadaj');
|
window.location.href = '/upadaj';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user