Fix AuthContext useEffect dependencies and add comprehensive debug logging for first-time setup flow

This commit is contained in:
Muhammad Ibrahim
2025-09-21 22:48:29 +01:00
parent 697da088d4
commit 523756cef2
2 changed files with 15 additions and 4 deletions

View File

@@ -23,8 +23,12 @@ import FirstTimeAdminSetup from './components/FirstTimeAdminSetup'
function AppRoutes() { function AppRoutes() {
const { needsFirstTimeSetup, checkingSetup, isAuthenticated } = useAuth() const { needsFirstTimeSetup, checkingSetup, isAuthenticated } = useAuth()
// Debug logging
console.log('AppRoutes state:', { needsFirstTimeSetup, checkingSetup, isAuthenticated })
// Show loading while checking if setup is needed // Show loading while checking if setup is needed
if (checkingSetup) { if (checkingSetup) {
console.log('Showing loading screen...')
return ( return (
<div className="min-h-screen bg-gradient-to-br from-primary-50 to-secondary-50 dark:from-secondary-900 dark:to-secondary-800 flex items-center justify-center"> <div className="min-h-screen bg-gradient-to-br from-primary-50 to-secondary-50 dark:from-secondary-900 dark:to-secondary-800 flex items-center justify-center">
<div className="text-center"> <div className="text-center">
@@ -37,9 +41,12 @@ function AppRoutes() {
// Show first-time setup if no admin users exist // Show first-time setup if no admin users exist
if (needsFirstTimeSetup && !isAuthenticated) { if (needsFirstTimeSetup && !isAuthenticated) {
console.log('Showing FirstTimeAdminSetup component...')
return <FirstTimeAdminSetup /> return <FirstTimeAdminSetup />
} }
console.log('Showing normal routes (Login/Dashboard)...')
return ( return (
<Routes> <Routes>
<Route path="/login" element={<Login />} /> <Route path="/login" element={<Login />} />

View File

@@ -1,4 +1,4 @@
import React, { createContext, useContext, useState, useEffect } from 'react' import React, { createContext, useContext, useState, useEffect, useCallback } from 'react'
const AuthContext = createContext() const AuthContext = createContext()
@@ -220,8 +220,9 @@ export const AuthProvider = ({ children }) => {
const canManageSettings = () => hasPermission('can_manage_settings') const canManageSettings = () => hasPermission('can_manage_settings')
// Check if any admin users exist (for first-time setup) // Check if any admin users exist (for first-time setup)
const checkAdminUsersExist = async () => { const checkAdminUsersExist = useCallback(async () => {
try { try {
console.log('Making API call to check admin users...')
const response = await fetch('/api/v1/auth/check-admin-users', { const response = await fetch('/api/v1/auth/check-admin-users', {
method: 'GET', method: 'GET',
headers: { headers: {
@@ -245,16 +246,19 @@ export const AuthProvider = ({ children }) => {
} finally { } finally {
setCheckingSetup(false) setCheckingSetup(false)
} }
} }, [])
// Check for admin users on initial load // Check for admin users on initial load
useEffect(() => { useEffect(() => {
console.log('AuthContext useEffect triggered:', { token: !!token, user: !!user })
if (!token && !user) { if (!token && !user) {
console.log('Calling checkAdminUsersExist...')
checkAdminUsersExist() checkAdminUsersExist()
} else { } else {
console.log('Skipping admin check - user already authenticated')
setCheckingSetup(false) setCheckingSetup(false)
} }
}, [token, user]) }, [token, user, checkAdminUsersExist])
const value = { const value = {
user, user,