diff --git a/client/lib/mock-auth.ts b/client/lib/mock-auth.ts index f8df6563..eef7e2e2 100644 --- a/client/lib/mock-auth.ts +++ b/client/lib/mock-auth.ts @@ -32,19 +32,19 @@ class MockAuthService { constructor() { // Load from localStorage if available - const savedUser = localStorage.getItem('mock_user'); - const savedProfile = localStorage.getItem('mock_profile'); - + const savedUser = localStorage.getItem("mock_user"); + const savedProfile = localStorage.getItem("mock_profile"); + if (savedUser) { this.currentUser = JSON.parse(savedUser); if (this.currentUser) { this.currentSession = { user: this.currentUser, - access_token: 'mock_token_' + Date.now() + access_token: "mock_token_" + Date.now(), }; } } - + if (savedProfile) { const profile = JSON.parse(savedProfile); this.profiles.set(profile.id, profile); @@ -54,33 +54,33 @@ class MockAuthService { async signInWithPassword(email: string, password: string) { // Mock validation - accept any email/password for demo if (!email || !password) { - throw new Error('Email and password are required'); + throw new Error("Email and password are required"); } const user: MockUser = { - id: 'mock_user_' + email.replace(/[^a-z0-9]/gi, '_'), + id: "mock_user_" + email.replace(/[^a-z0-9]/gi, "_"), email, - created_at: new Date().toISOString() + created_at: new Date().toISOString(), }; this.currentUser = user; this.currentSession = { user, - access_token: 'mock_token_' + Date.now() + access_token: "mock_token_" + Date.now(), }; // Save to localStorage - localStorage.setItem('mock_user', JSON.stringify(user)); + localStorage.setItem("mock_user", JSON.stringify(user)); // Notify auth state change - setTimeout(() => this.notifyAuthChange('SIGNED_IN'), 50); + setTimeout(() => this.notifyAuthChange("SIGNED_IN"), 50); return { data: { user, - session: this.currentSession + session: this.currentSession, }, - error: null + error: null, }; } @@ -95,14 +95,14 @@ class MockAuthService { this.currentUser = user; this.currentSession = { user, - access_token: 'mock_oauth_token_' + Date.now(), + access_token: "mock_oauth_token_" + Date.now(), }; // Save to localStorage - localStorage.setItem('mock_user', JSON.stringify(user)); + localStorage.setItem("mock_user", JSON.stringify(user)); // Notify auth state change after a short delay to simulate redirect - setTimeout(() => this.notifyAuthChange('SIGNED_IN'), 50); + setTimeout(() => this.notifyAuthChange("SIGNED_IN"), 50); return { data: { user, session: this.currentSession }, @@ -113,11 +113,11 @@ class MockAuthService { async signOut() { this.currentUser = null; this.currentSession = null; - localStorage.removeItem('mock_user'); - localStorage.removeItem('mock_profile'); + localStorage.removeItem("mock_user"); + localStorage.removeItem("mock_profile"); // Notify auth state change - setTimeout(() => this.notifyAuthChange('SIGNED_OUT'), 50); + setTimeout(() => this.notifyAuthChange("SIGNED_OUT"), 50); return { error: null }; } @@ -125,14 +125,14 @@ class MockAuthService { async getUser() { return { data: { user: this.currentUser }, - error: null + error: null, }; } async getSession() { return { data: { session: this.currentSession }, - error: null + error: null, }; } @@ -141,48 +141,54 @@ class MockAuthService { return this.profiles.get(userId) || null; } - async updateProfile(userId: string, updates: Partial): Promise { + async updateProfile( + userId: string, + updates: Partial, + ): Promise { let profile = this.profiles.get(userId); - + if (!profile) { // Create new profile profile = { id: userId, - username: updates.username || this.currentUser?.email?.split('@')[0] || 'user', - email: this.currentUser?.email || '', - role: 'member', + username: + updates.username || this.currentUser?.email?.split("@")[0] || "user", + email: this.currentUser?.email || "", + role: "member", onboarded: true, level: 1, total_xp: 0, created_at: new Date().toISOString(), updated_at: new Date().toISOString(), - ...updates + ...updates, }; } else { // Update existing profile = { ...profile, ...updates, - updated_at: new Date().toISOString() + updated_at: new Date().toISOString(), }; } this.profiles.set(userId, profile); - localStorage.setItem('mock_profile', JSON.stringify(profile)); - + localStorage.setItem("mock_profile", JSON.stringify(profile)); + return profile; } - onAuthStateChange(callback: (event: string, session: MockSession | null) => void) { + onAuthStateChange( + callback: (event: string, session: MockSession | null) => void, + ) { // Store callback for later use this.authCallback = callback; // Immediately call with current state setTimeout(() => { if (this.currentSession) { - callback('SIGNED_IN', this.currentSession); + callback("SIGNED_IN", this.currentSession); } else { - callback('SIGNED_OUT', null); + callback("SIGNED_OUT", null); } }, 100); @@ -192,13 +198,15 @@ class MockAuthService { subscription: { unsubscribe: () => { this.authCallback = null; - } - } - } + }, + }, + }, }; } - private authCallback: ((event: string, session: MockSession | null) => void) | null = null; + private authCallback: + | ((event: string, session: MockSession | null) => void) + | null = null; private notifyAuthChange(event: string) { if (this.authCallback) { diff --git a/client/lib/supabase.ts b/client/lib/supabase.ts index 755c76b4..445bcc2e 100644 --- a/client/lib/supabase.ts +++ b/client/lib/supabase.ts @@ -105,11 +105,19 @@ export const supabase = new Proxy(supabaseClient || {}, { // OAuth sign-in (GitHub/Google). Falls back to mock in development. signInWithOAuth: async (opts: any) => { const provider = opts?.provider; - if (isSupabaseConfigured && target && target.auth && typeof target.auth.signInWithOAuth === 'function') { + if ( + isSupabaseConfigured && + target && + target.auth && + typeof target.auth.signInWithOAuth === "function" + ) { try { return await target.auth.signInWithOAuth(opts); } catch (error: any) { - console.warn('Supabase signInWithOAuth failed:', error?.message || error); + console.warn( + "Supabase signInWithOAuth failed:", + error?.message || error, + ); try { return await mockAuth.signInWithOAuth(provider); } catch (e) { @@ -157,7 +165,9 @@ export const supabase = new Proxy(supabaseClient || {}, { try { realSub = target.auth.onAuthStateChange(callback); } catch (error) { - console.warn("Supabase onAuthStateChange failed, will use mock too"); + console.warn( + "Supabase onAuthStateChange failed, will use mock too", + ); } } // Always subscribe to mock as a safety net in case we fall back during auth