From d3e001707b931e734145acd48ff956fc8a11766f Mon Sep 17 00:00:00 2001 From: DaX Date: Fri, 31 Oct 2025 02:45:47 +0100 Subject: [PATCH] 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) --- app/upadaj/colors/page.tsx | 9 ++++++--- app/upadaj/dashboard/page.tsx | 9 ++++++--- app/upadaj/page.tsx | 4 ++-- app/upadaj/requests/page.tsx | 4 ++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/upadaj/colors/page.tsx b/app/upadaj/colors/page.tsx index 2bde89a..bdcc19c 100644 --- a/app/upadaj/colors/page.tsx +++ b/app/upadaj/colors/page.tsx @@ -53,13 +53,16 @@ export default function ColorsManagement() { // Check authentication useEffect(() => { + // Wait for component to mount to avoid SSR issues with localStorage + if (!mounted) return; + const token = localStorage.getItem('authToken'); const expiry = localStorage.getItem('tokenExpiry'); - + if (!token || !expiry || Date.now() > parseInt(expiry)) { - router.push('/upadaj'); + window.location.href = '/upadaj'; } - }, [router]); + }, [mounted]); // Fetch colors const fetchColors = async () => { diff --git a/app/upadaj/dashboard/page.tsx b/app/upadaj/dashboard/page.tsx index 50af0c3..9122890 100644 --- a/app/upadaj/dashboard/page.tsx +++ b/app/upadaj/dashboard/page.tsx @@ -123,13 +123,16 @@ export default function AdminDashboard() { // Check authentication useEffect(() => { + // Wait for component to mount to avoid SSR issues with localStorage + if (!mounted) return; + const token = localStorage.getItem('authToken'); const expiry = localStorage.getItem('tokenExpiry'); - + if (!token || !expiry || Date.now() > parseInt(expiry)) { - router.push('/upadaj'); + window.location.href = '/upadaj'; } - }, [router]); + }, [mounted]); // Fetch filaments const fetchFilaments = async () => { diff --git a/app/upadaj/page.tsx b/app/upadaj/page.tsx index 941f27d..33a72eb 100644 --- a/app/upadaj/page.tsx +++ b/app/upadaj/page.tsx @@ -32,8 +32,8 @@ export default function AdminLogin() { // Track successful login trackEvent('Admin', 'Login', 'Success'); - // Redirect to admin dashboard - router.push('/upadaj/dashboard'); + // Redirect to admin dashboard using window.location for static export + window.location.href = '/upadaj/dashboard'; } catch (err: any) { setError('Neispravno korisničko ime ili lozinka'); console.error('Login error:', err); diff --git a/app/upadaj/requests/page.tsx b/app/upadaj/requests/page.tsx index 5be9f53..39530d7 100644 --- a/app/upadaj/requests/page.tsx +++ b/app/upadaj/requests/page.tsx @@ -40,9 +40,9 @@ export default function ColorRequestsAdmin() { const checkAuth = () => { const token = localStorage.getItem('authToken'); const expiry = localStorage.getItem('tokenExpiry'); - + if (!token || !expiry || new Date().getTime() > parseInt(expiry)) { - router.push('/upadaj'); + window.location.href = '/upadaj'; } };