@@ -13,31 +13,31 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
1313 const [ currentTenant , setCurrentTenant ] = useState < Tenant | null > ( null )
1414 const [ isLoading , setIsLoading ] = useState ( true )
1515
16- const getCurrentUser = async ( ) : Promise < User | null > => {
17- const {
18- data : { user } ,
19- } = await supabase . auth . getUser ( )
20- if ( ! user ) return null
16+ const buildUserFromSession = async ( authUser : any ) : Promise < User | null > => {
17+ console . log ( '🔍 buildUserFromSession for:' , authUser . email )
2118
19+ // Don't call supabase.auth.getUser() - we already have the authenticated user!
2220 const { data : profile , error : profileError } = await supabase
2321 . from ( 'profiles' )
2422 . select ( 'first_name, last_name, role, tenant_id' )
25- . eq ( 'id' , user . id )
23+ . eq ( 'id' , authUser . id )
2624 . single ( )
2725
26+ console . log ( '📋 Profile:' , profile , 'Error:' , profileError )
27+
2828 if ( profileError ) {
29- console . error ( 'Failed to fetch profile :' , profileError )
29+ console . error ( '❌ Profile fetch failed :' , profileError )
3030 return {
31- id : user . id ,
32- email : user . email ! ,
31+ id : authUser . id ,
32+ email : authUser . email ! ,
3333 first_name : '' ,
3434 last_name : '' ,
3535 tenant : null ,
3636 role : 'tenant' ,
3737 }
3838 }
3939
40- // Only fetch tenant if tenant_id exists
40+ // Fetch tenant if needed
4141 let tenant = null
4242 if ( profile ?. tenant_id ) {
4343 const { data : tenantData , error : tenantError } = await supabase
@@ -47,37 +47,26 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
4747 . single ( )
4848
4949 if ( tenantError ) {
50- console . error ( 'Failed to fetch tenant :' , tenantError )
50+ console . error ( '❌ Tenant fetch failed :' , tenantError )
5151 } else {
5252 tenant = tenantData
53+ console . log ( '🏢 Tenant:' , tenant . name )
5354 }
5455 }
5556
5657 const retrieved_user : User = {
57- id : user . id ,
58- email : user . email ! ,
58+ id : authUser . id ,
59+ email : authUser . email ! ,
5960 first_name : profile ?. first_name || '' ,
6061 last_name : profile ?. last_name || '' ,
6162 tenant : tenant ,
6263 role : profile ?. role || 'tenant' ,
6364 }
6465
65- console . log ( retrieved_user )
66-
66+ console . log ( '✅ User built:' , retrieved_user )
6767 return retrieved_user
6868 }
6969
70- const onAuthStateChange = ( callback : ( user : User | null ) => void ) => {
71- return supabase . auth . onAuthStateChange ( async ( _ , session ) => {
72- if ( session ?. user ) {
73- const user = await getCurrentUser ( )
74- callback ( user )
75- } else {
76- callback ( null )
77- }
78- } )
79- }
80-
8170 const login = async ( credentials : LoginForm ) => {
8271 const { error } = await supabase . auth . signInWithPassword ( {
8372 email : credentials . email ,
@@ -89,9 +78,6 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
8978 const logout = async ( ) => {
9079 const { error } = await supabase . auth . signOut ( )
9180 if ( error ) throw error
92-
93- setUser ( null )
94- setCurrentTenant ( null )
9581 }
9682
9783 const switchTenant = async ( tenantId : string ) => {
@@ -114,69 +100,54 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
114100 }
115101 }
116102
117- const loadTenantData = async ( tenant : Tenant | null ) => {
118- if ( tenant ) {
119- setCurrentTenant ( tenant )
120- } else {
121- setCurrentTenant ( null )
122- }
123- }
124-
125103 useEffect ( ( ) => {
104+ console . log ( '🟢 AuthProvider mounted' )
126105 let subscription : Subscription | null = null
127106
128- async function initializeAuth ( ) {
129- try {
130- const {
131- data : { session } ,
132- error,
133- } = await supabase . auth . getSession ( )
134- if ( error ) throw error
135-
136- if ( ! session ) {
137- setIsLoading ( false )
138- return
139- }
107+ const handleAuthChange = async ( event : string , session : any ) => {
108+ console . log (
109+ `🔔 Auth event: ${ event } ` ,
110+ session ?. user ?. email || 'no session'
111+ )
140112
141- const currentUser = await getCurrentUser ( )
113+ if ( session ?. user ) {
114+ console . log ( '🔄 Building user from session user...' )
115+ // Use session.user directly - it's already authenticated!
116+ const currentUser = await buildUserFromSession ( session . user )
142117
143- if ( ! currentUser ) {
144- setUser ( null )
118+ console . log ( '💾 Setting user state:' , currentUser )
119+ setUser ( currentUser )
120+
121+ if ( currentUser ?. tenant ) {
122+ setCurrentTenant ( currentUser . tenant )
123+ } else {
145124 setCurrentTenant ( null )
146- setIsLoading ( false )
147- return
148125 }
149-
150- setUser ( currentUser )
151- await loadTenantData ( currentUser . tenant )
152- setIsLoading ( false )
153- } catch ( error ) {
154- console . error ( 'Auth initialization error:' , error )
126+ } else {
127+ console . log ( '🚫 Clearing user state' )
155128 setUser ( null )
156129 setCurrentTenant ( null )
157- setIsLoading ( false )
158- } finally {
159- const {
160- data : { subscription : sub } ,
161- } = onAuthStateChange ( async user => {
162- setUser ( user )
163-
164- if ( user ?. tenant ) {
165- await loadTenantData ( user . tenant )
166- } else {
167- setCurrentTenant ( null )
168- }
169- } )
170- subscription = sub
171130 }
131+
132+ console . log ( '✋ Setting isLoading = false' )
133+ setIsLoading ( false )
172134 }
173135
174- initializeAuth ( )
136+ const {
137+ data : { subscription : sub } ,
138+ } = supabase . auth . onAuthStateChange ( handleAuthChange )
139+ subscription = sub
140+
141+ // Fallback timeout
142+ const timeout = setTimeout ( ( ) => {
143+ console . log ( '⏰ Timeout: setting isLoading = false' )
144+ setIsLoading ( false )
145+ } , 1000 )
175146
176147 return ( ) => {
177- if ( subscription ) {
178- subscription . unsubscribe ( )
179- }
148+ console . log ( '🧹 Cleanup' )
149+ clearTimeout ( timeout )
150+ subscription ?. unsubscribe ( )
180151 }
181152 } , [ ] )
182153
0 commit comments