From d3e4bd2df2a258a2387faad015e86b23532bdad7 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Sun, 9 Mar 2025 12:41:42 +0000 Subject: [PATCH] Implement authentication functionality Adds authentication functionality to the application. --- src/contexts/AuthContext.tsx | 31 ++++++++++++ src/pages/Auth.tsx | 94 ++++++++++++++++++++++++++++++++++-- 2 files changed, 122 insertions(+), 3 deletions(-) diff --git a/src/contexts/AuthContext.tsx b/src/contexts/AuthContext.tsx index 5464709..834db4d 100644 --- a/src/contexts/AuthContext.tsx +++ b/src/contexts/AuthContext.tsx @@ -4,6 +4,8 @@ import { supabase } from '@/integrations/supabase/client'; import { Session, User } from '@supabase/supabase-js'; import { useToast } from '@/hooks/use-toast'; +type Provider = 'github' | 'google' | 'microsoft'; + type AuthContextType = { session: Session | null; user: User | null; @@ -12,6 +14,7 @@ type AuthContextType = { signIn: (email: string, password: string) => Promise; signUp: (email: string, password: string, fullName: string) => Promise; signOut: () => Promise; + signInWithProvider: (provider: Provider) => Promise; }; const AuthContext = createContext(undefined); @@ -128,6 +131,33 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => { } }; + const signInWithProvider = async (provider: Provider) => { + try { + setLoading(true); + const { error } = await supabase.auth.signInWithOAuth({ + provider, + options: { + redirectTo: window.location.origin + '/dashboard', + }, + }); + + if (error) throw error; + + toast({ + title: "Redirecting...", + description: `Signing in with ${provider}`, + }); + } catch (error: any) { + toast({ + title: `Error signing in with ${provider}`, + description: error.message, + variant: "destructive", + }); + console.error(`Error signing in with ${provider}:`, error); + setLoading(false); + } + }; + const signOut = async () => { try { setLoading(true); @@ -159,6 +189,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => { signIn, signUp, signOut, + signInWithProvider, }} > {children} diff --git a/src/pages/Auth.tsx b/src/pages/Auth.tsx index a06961a..e383bbb 100644 --- a/src/pages/Auth.tsx +++ b/src/pages/Auth.tsx @@ -8,9 +8,10 @@ import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; +import { Separator } from '@/components/ui/separator'; const Auth = () => { - const { user, signIn, signUp, loading } = useAuth(); + const { user, signIn, signUp, signInWithProvider, loading } = useAuth(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [fullName, setFullName] = useState(''); @@ -41,6 +42,11 @@ const Auth = () => { } }; + const handleSocialSignIn = (provider: 'github' | 'google' | 'microsoft') => { + setAuthError(null); + signInWithProvider(provider); + }; + return (
@@ -101,7 +107,7 @@ const Auth = () => {
{authError}
)} - + + +
+
+ +
+
+ + OR CONTINUE WITH + +
+
+ +
+ + + +
@@ -173,7 +220,7 @@ const Auth = () => {
{authError}
)} - + + +
+
+ +
+
+ + OR CONTINUE WITH + +
+
+ +
+ + + +