Skip to content

Commit 261e963

Browse files
committed
Fix auth: add session detection, better error handling, email confirmation flow
1 parent 3bf57cd commit 261e963

File tree

2 files changed

+83
-52
lines changed

2 files changed

+83
-52
lines changed

src/lib/supabase.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ if (!supabaseUrl || !supabaseAnonKey) {
77
throw new Error('Missing Supabase environment variables');
88
}
99

10-
export const supabase = createClient(supabaseUrl, supabaseAnonKey);
10+
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
11+
auth: {
12+
autoRefreshToken: true,
13+
persistSession: true,
14+
detectSessionInUrl: true,
15+
},
16+
});
1117

1218
// Types for our leaderboard
1319
export interface LeaderboardEntry {
@@ -22,15 +28,10 @@ export interface LeaderboardEntry {
2228

2329
// Auth helper functions
2430
export const signInWithGoogle = async () => {
25-
// Use Vercel URL in production, localhost in development
26-
const redirectUrl = window.location.hostname === 'localhost'
27-
? 'http://localhost:8080/#/quiz'
28-
: 'https://bb84-simulation.vercel.app/#/quiz';
29-
3031
const { data, error } = await supabase.auth.signInWithOAuth({
3132
provider: 'google',
3233
options: {
33-
redirectTo: redirectUrl,
34+
redirectTo: `${window.location.origin}/#/quiz`,
3435
},
3536
});
3637
return { data, error };
@@ -43,7 +44,9 @@ export const signUpWithEmail = async (email: string, password: string, username:
4344
options: {
4445
data: {
4546
username,
47+
display_name: username,
4648
},
49+
emailRedirectTo: `${window.location.origin}/#/quiz`,
4750
},
4851
});
4952
return { data, error };

src/pages/Auth.tsx

Lines changed: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -39,55 +39,83 @@ const Auth = () => {
3939
e.preventDefault();
4040
setLoading(true);
4141

42-
if (mode === 'signup') {
43-
if (!formData.username.trim()) {
44-
toast({
45-
title: "Username Required",
46-
description: "Please enter a username",
47-
variant: "destructive"
48-
});
49-
setLoading(false);
50-
return;
51-
}
42+
try {
43+
if (mode === 'signup') {
44+
if (!formData.username.trim()) {
45+
toast({
46+
title: "Username Required",
47+
description: "Please enter a username",
48+
variant: "destructive"
49+
});
50+
setLoading(false);
51+
return;
52+
}
5253

53-
const { error } = await signUpWithEmail(
54-
formData.email,
55-
formData.password,
56-
formData.username
57-
);
58-
59-
if (error) {
60-
toast({
61-
title: "Sign Up Failed",
62-
description: error.message,
63-
variant: "destructive"
64-
});
65-
} else {
66-
toast({
67-
title: "Check Your Email! 📧",
68-
description: "We sent you a confirmation link",
69-
});
70-
setMode('signin');
71-
}
72-
} else {
73-
const { error } = await signInWithEmail(formData.email, formData.password);
74-
75-
if (error) {
76-
toast({
77-
title: "Sign In Failed",
78-
description: error.message,
79-
variant: "destructive"
80-
});
54+
const { data, error } = await signUpWithEmail(
55+
formData.email,
56+
formData.password,
57+
formData.username
58+
);
59+
60+
if (error) {
61+
toast({
62+
title: "Sign Up Failed",
63+
description: error.message,
64+
variant: "destructive"
65+
});
66+
} else if (data.user) {
67+
// Check if email confirmation is required
68+
if (data.user.identities && data.user.identities.length === 0) {
69+
toast({
70+
title: "Email Already Registered",
71+
description: "Please sign in with your existing account",
72+
variant: "destructive"
73+
});
74+
} else if (data.session) {
75+
// Auto-confirmed, user is logged in
76+
toast({
77+
title: "Account Created! 🎉",
78+
description: "Redirecting to quiz...",
79+
});
80+
setTimeout(() => navigate('/quiz'), 500);
81+
} else {
82+
// Email confirmation required
83+
toast({
84+
title: "Check Your Email! 📧",
85+
description: "We sent you a confirmation link. If you don't see it, check spam folder.",
86+
});
87+
setMode('signin');
88+
}
89+
}
8190
} else {
82-
toast({
83-
title: "Welcome Back! 🎉",
84-
description: "Redirecting to quiz...",
85-
});
86-
setTimeout(() => navigate('/quiz'), 500);
91+
const { data, error } = await signInWithEmail(formData.email, formData.password);
92+
93+
if (error) {
94+
toast({
95+
title: "Sign In Failed",
96+
description: error.message === "Invalid login credentials"
97+
? "Wrong email or password. Please try again."
98+
: error.message,
99+
variant: "destructive"
100+
});
101+
} else if (data.user) {
102+
toast({
103+
title: "Welcome Back! 🎉",
104+
description: "Redirecting to quiz...",
105+
});
106+
setTimeout(() => navigate('/quiz'), 500);
107+
}
87108
}
109+
} catch (err) {
110+
toast({
111+
title: "Error",
112+
description: "Something went wrong. Please try again.",
113+
variant: "destructive"
114+
});
115+
console.error('Auth error:', err);
116+
} finally {
117+
setLoading(false);
88118
}
89-
90-
setLoading(false);
91119
};
92120

93121
return (

0 commit comments

Comments
 (0)