fix: infinite spinner – init isLoading from token presence and add home-page auth check
Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com> Agent-Logs-Url: https://github.com/christianlouis/pop_puller_to_gmail/sessions/b032c6bb-aa8c-491d-91f5-289c69e67b95
This commit is contained in:
@@ -4,17 +4,30 @@ import { useEffect } from 'react';
|
||||
import Link from 'next/link';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useAuthStore } from '@/store/authStore';
|
||||
import { userApi } from '@/lib/api';
|
||||
import { Mail, ArrowRight, Shield, Zap, Clock } from 'lucide-react';
|
||||
|
||||
export default function Home() {
|
||||
const router = useRouter();
|
||||
const { user, isLoading } = useAuthStore();
|
||||
const { isLoading, setUser, setLoading } = useAuthStore();
|
||||
|
||||
useEffect(() => {
|
||||
if (!isLoading && user) {
|
||||
router.push('/dashboard');
|
||||
}
|
||||
}, [user, isLoading, router]);
|
||||
const token =
|
||||
typeof window !== 'undefined' ? localStorage.getItem('access_token') : null;
|
||||
if (!token) return;
|
||||
|
||||
userApi
|
||||
.getCurrentUser()
|
||||
.then((userData) => {
|
||||
setUser(userData);
|
||||
router.push('/dashboard');
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('Auth check failed on home page:', err);
|
||||
localStorage.removeItem('access_token');
|
||||
setLoading(false);
|
||||
});
|
||||
}, [router, setUser, setLoading]);
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
|
||||
@@ -16,7 +16,7 @@ export const useAuthStore = create<AuthState>((set) => ({
|
||||
user: null,
|
||||
token: typeof window !== 'undefined' ? localStorage.getItem('access_token') : null,
|
||||
isAuthenticated: typeof window !== 'undefined' ? !!localStorage.getItem('access_token') : false,
|
||||
isLoading: true,
|
||||
isLoading: typeof window !== 'undefined' ? !!localStorage.getItem('access_token') : false,
|
||||
|
||||
setUser: (user) => set({
|
||||
user,
|
||||
|
||||
Reference in New Issue
Block a user