mirror of
https://github.com/9technologygroup/patchmon.net.git
synced 2025-11-03 13:33:30 +00:00
Fix AuthContext useEffect dependencies and add comprehensive debug logging for first-time setup flow
This commit is contained in:
@@ -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 />} />
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user