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() {
const { needsFirstTimeSetup, checkingSetup, isAuthenticated } = useAuth()
// Debug logging
console.log('AppRoutes state:', { needsFirstTimeSetup, checkingSetup, isAuthenticated })
// Show loading while checking if setup is needed
if (checkingSetup) {
console.log('Showing loading screen...')
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="text-center">
@@ -37,9 +41,12 @@ function AppRoutes() {
// Show first-time setup if no admin users exist
if (needsFirstTimeSetup && !isAuthenticated) {
console.log('Showing FirstTimeAdminSetup component...')
return <FirstTimeAdminSetup />
}
console.log('Showing normal routes (Login/Dashboard)...')
return (
<Routes>
<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()
@@ -220,8 +220,9 @@ export const AuthProvider = ({ children }) => {
const canManageSettings = () => hasPermission('can_manage_settings')
// Check if any admin users exist (for first-time setup)
const checkAdminUsersExist = async () => {
const checkAdminUsersExist = useCallback(async () => {
try {
console.log('Making API call to check admin users...')
const response = await fetch('/api/v1/auth/check-admin-users', {
method: 'GET',
headers: {
@@ -245,16 +246,19 @@ export const AuthProvider = ({ children }) => {
} finally {
setCheckingSetup(false)
}
}
}, [])
// Check for admin users on initial load
useEffect(() => {
console.log('AuthContext useEffect triggered:', { token: !!token, user: !!user })
if (!token && !user) {
console.log('Calling checkAdminUsersExist...')
checkAdminUsersExist()
} else {
console.log('Skipping admin check - user already authenticated')
setCheckingSetup(false)
}
}, [token, user])
}, [token, user, checkAdminUsersExist])
const value = {
user,