Skip to content

Commit fbcb5a5

Browse files
committed
feat: add Github Auth
1 parent 0682e0d commit fbcb5a5

2 files changed

Lines changed: 53 additions & 4 deletions

File tree

src/components/Auth.tsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { useState } from 'react';
22
import { useAuth } from '../contexts/AuthContext';
3-
import { Keyboard, Mail, Lock, AlertCircle } from 'lucide-react';
3+
import { Keyboard, Mail, Lock, AlertCircle, Github } from 'lucide-react';
44

55
export default function Auth() {
66
const [isLogin, setIsLogin] = useState(true);
77
const [email, setEmail] = useState('');
88
const [password, setPassword] = useState('');
99
const [error, setError] = useState('');
1010
const [loading, setLoading] = useState(false);
11-
const { signIn, signUp } = useAuth();
11+
const { signIn, signUp, signInWithGithub } = useAuth();
1212

1313
const handleSubmit = async (e: React.FormEvent) => {
1414
e.preventDefault();
@@ -26,6 +26,16 @@ export default function Auth() {
2626
setLoading(false);
2727
};
2828

29+
const handleGithubSignIn = async () => {
30+
setError('');
31+
setLoading(true);
32+
const { error } = await signInWithGithub();
33+
if (error) {
34+
setError(error.message);
35+
}
36+
setLoading(false);
37+
};
38+
2939
return (
3040
<div className="min-h-screen bg-gradient-to-br from-blue-50 to-green-50 flex items-center justify-center px-4">
3141
<div className="max-w-md w-full">
@@ -68,6 +78,27 @@ export default function Auth() {
6878
</div>
6979
)}
7080

81+
{/* GitHub Sign-In Button */}
82+
<button
83+
onClick={handleGithubSignIn}
84+
disabled={loading}
85+
className="w-full py-3 mb-4 bg-gray-900 text-white rounded-lg hover:bg-gray-800 transition-colors font-medium disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2"
86+
>
87+
<Github className="w-5 h-5" />
88+
Sign in with GitHub
89+
</button>
90+
91+
<div className="relative mb-4">
92+
<div className="absolute inset-0 flex items-center">
93+
<div className="w-full border-t border-gray-300"></div>
94+
</div>
95+
<div className="relative flex justify-center text-sm">
96+
<span className="px-2 bg-white text-gray-500">
97+
Or continue with email
98+
</span>
99+
</div>
100+
</div>
101+
71102
<form onSubmit={handleSubmit} className="space-y-4">
72103
<div>
73104
<label className="block text-sm font-medium text-gray-700 mb-2">

src/contexts/AuthContext.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ interface AuthContextType {
88
signUp: (email: string, password: string) => Promise<{ error: any }>;
99
signIn: (email: string, password: string) => Promise<{ error: any }>;
1010
signOut: () => Promise<void>;
11+
signInWithGithub: () => Promise<{ error: any }>;
1112
}
1213

1314
const AuthContext = createContext<AuthContextType | undefined>(undefined);
@@ -33,20 +34,37 @@ export function AuthProvider({ children }: { children: ReactNode }) {
3334

3435
const signUp = async (email: string, password: string) => {
3536
const { error } = await supabase.auth.signUp({ email, password });
36-
return { error };
37+
if (error) {
38+
// Handle specific errors
39+
if (error.message.includes('password')) {
40+
return { error: { message: 'Password is compromised or too weak. Choose a stronger one.' } };
41+
}
42+
return { error };
43+
}
44+
return { error: null };
3745
};
3846

3947
const signIn = async (email: string, password: string) => {
4048
const { error } = await supabase.auth.signInWithPassword({ email, password });
4149
return { error };
4250
};
4351

52+
const signInWithGithub = async () => {
53+
const { error } = await supabase.auth.signInWithOAuth({
54+
provider: 'github',
55+
options: {
56+
redirectTo: window.location.origin, // Adjust for production
57+
},
58+
});
59+
return { error };
60+
};
61+
4462
const signOut = async () => {
4563
await supabase.auth.signOut();
4664
};
4765

4866
return (
49-
<AuthContext.Provider value={{ user, loading, signUp, signIn, signOut }}>
67+
<AuthContext.Provider value={{ user, loading, signUp, signIn, signOut, signInWithGithub }}>
5068
{children}
5169
</AuthContext.Provider>
5270
);

0 commit comments

Comments
 (0)