11// client/src/App.tsx
22
3- import React , { useEffect } from "react" ;
3+ import React , { useEffect , useState } from "react" ;
44import { Switch , Route } from "wouter" ;
55import { TooltipProvider } from "@/components/ui/tooltip" ;
66import { ThemeProvider } from "@/contexts/ThemeContext" ;
@@ -33,37 +33,104 @@ function Router() {
3333 ) ;
3434}
3535
36+ // Added debug component to show authentication state
37+ function AuthDebug ( { user, loading } : { user : any , loading : boolean } ) {
38+ return (
39+ < div className = "fixed bottom-4 right-4 bg-gray-800 text-white p-2 rounded text-xs opacity-80 z-50" >
40+ < div > Auth Status: { loading ? "Loading" : user ? "Authenticated" : "Not Authenticated" } </ div >
41+ { user && (
42+ < div >
43+ User: { user . email || "No email" } < br />
44+ ID: { user . id ?. substring ( 0 , 8 ) || "No ID" }
45+ </ div >
46+ ) }
47+ </ div >
48+ ) ;
49+ }
50+
3651export default function App ( ) {
3752 const { user, loading, signIn } = useAuthContext ( ) ;
53+ const [ showDebug , setShowDebug ] = useState ( false ) ;
54+
55+ // Add explicit debugging to track auth state
56+ useEffect ( ( ) => {
57+ console . log ( "[App] Auth state in App.tsx:" , {
58+ user : user ? { id : user . id , email : user . email } : null ,
59+ loading
60+ } ) ;
61+ console . log ( "[App] Is user null?" , user === null ) ;
62+ console . log ( "[App] Authentication state:" , loading ? "loading" : user ? "authenticated" : "not authenticated" ) ;
63+ } , [ user , loading ] ) ;
3864
3965 // Apply your theme class once on mount
4066 useEffect ( ( ) => {
4167 const prefersDark = window . matchMedia ( "(prefers-color-scheme: dark)" ) . matches ;
4268 const saved = localStorage . getItem ( "theme" ) ;
4369 const isDark = saved === "dark" || ( ! saved && prefersDark ) ;
4470 document . documentElement . classList . toggle ( "dark" , isDark ) ;
71+
72+ // Toggle debug panel with key press (Alt+D)
73+ const handleKeyDown = ( e : KeyboardEvent ) => {
74+ if ( e . altKey && e . key === 'd' ) {
75+ setShowDebug ( prev => ! prev ) ;
76+ }
77+ } ;
78+
79+ window . addEventListener ( 'keydown' , handleKeyDown ) ;
80+ return ( ) => window . removeEventListener ( 'keydown' , handleKeyDown ) ;
4581 } , [ ] ) ;
4682
83+ // Try to restore from localStorage if no user but we have a saved one
84+ useEffect ( ( ) => {
85+ if ( ! loading && ! user ) {
86+ try {
87+ const savedUser = localStorage . getItem ( 'auth_user' ) ;
88+ if ( savedUser ) {
89+ console . log ( "[App] Found saved user in localStorage, but not reflected in context" ) ;
90+ }
91+ } catch ( e ) {
92+ console . error ( "[App] localStorage error:" , e ) ;
93+ }
94+ }
95+ } , [ user , loading ] ) ;
96+
4797 // 1) Still waiting for Firebase → show spinner or placeholder
4898 if ( loading ) {
49- return < div className = "h-screen flex items-center justify-center" > Loading…</ div > ;
99+ console . log ( "[App] Currently loading, showing loading state" ) ;
100+ return (
101+ < div className = "h-screen flex flex-col items-center justify-center" >
102+ < div className = "mb-4" > Loading authentication...</ div >
103+ < div className = "animate-spin h-8 w-8 border-t-2 border-b-2 border-blue-500 rounded-full" > </ div >
104+ { showDebug && < AuthDebug user = { user } loading = { loading } /> }
105+ </ div >
106+ ) ;
50107 }
51108
52109 // 2) Not signed in → show Google button
53110 if ( ! user ) {
111+ console . log ( "[App] No user detected, showing login button" ) ;
54112 return (
55- < div className = "h-screen flex items-center justify-center" >
113+ < div className = "h-screen flex flex-col items-center justify-center" >
114+ < div className = "mb-4" >
115+ < h1 className = "text-xl font-bold mb-2" > CodePatchwork</ h1 >
116+ < p > Please sign in to access your snippets</ p >
117+ </ div >
56118 < button
57- onClick = { signIn }
119+ onClick = { ( ) => {
120+ console . log ( "[App] Sign in button clicked" ) ;
121+ signIn ( ) ;
122+ } }
58123 className = "px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700"
59124 >
60125 Sign in with Google
61126 </ button >
127+ { showDebug && < AuthDebug user = { user } loading = { loading } /> }
62128 </ div >
63129 ) ;
64130 }
65131
66132 // 3) Signed in → render the full app
133+ console . log ( "[App] User authenticated, rendering full app:" , user ) ;
67134 return (
68135 < ThemeProvider >
69136 < CodeThemeProvider >
@@ -72,6 +139,7 @@ export default function App() {
72139 < TooltipProvider >
73140 < Router />
74141 < DebugEnv />
142+ { showDebug && < AuthDebug user = { user } loading = { loading } /> }
75143 </ TooltipProvider >
76144 </ CollectionProvider >
77145 </ SnippetProvider >
0 commit comments