diff --git a/src/contexts/AuthContext.tsx b/src/contexts/AuthContext.tsx index 5464709..02c77dc 100644 --- a/src/contexts/AuthContext.tsx +++ b/src/contexts/AuthContext.tsx @@ -1,7 +1,7 @@ import { createContext, useContext, useState, useEffect, ReactNode } from 'react'; import { supabase } from '@/integrations/supabase/client'; -import { Session, User } from '@supabase/supabase-js'; +import { Session, User, Provider } from '@supabase/supabase-js'; import { useToast } from '@/hooks/use-toast'; type AuthContextType = { @@ -11,6 +11,7 @@ type AuthContextType = { loading: boolean; signIn: (email: string, password: string) => Promise; signUp: (email: string, password: string, fullName: string) => Promise; + signInWithProvider: (provider: Provider) => Promise; signOut: () => Promise; }; @@ -128,6 +129,31 @@ 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; + + } catch (error: any) { + toast({ + title: "Error signing in", + description: error.message, + variant: "destructive", + }); + console.error(`Error signing in with ${provider}:`, error); + throw error; + } finally { + setLoading(false); + } + }; + const signOut = async () => { try { setLoading(true); @@ -158,6 +184,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => { loading, signIn, signUp, + signInWithProvider, signOut, }} > diff --git a/src/pages/Auth.tsx b/src/pages/Auth.tsx index a06961a..c5d64ba 100644 --- a/src/pages/Auth.tsx +++ b/src/pages/Auth.tsx @@ -2,15 +2,16 @@ import { useState } from 'react'; import { Navigate } from 'react-router-dom'; import { useAuth } from '@/contexts/AuthContext'; -import { ShieldCheck, Mail, Lock, User } from 'lucide-react'; +import { ShieldCheck, Mail, Lock, User, Github, Google, Microsoft } from 'lucide-react'; import { Button } from '@/components/ui/button'; 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,15 @@ const Auth = () => { } }; + const handleSocialSignIn = async (provider: 'google' | 'github' | 'microsoft') => { + setAuthError(null); + try { + await signInWithProvider(provider); + } catch (error: any) { + setAuthError(error.message); + } + }; + return (
@@ -101,7 +111,7 @@ const Auth = () => {
{authError}
)} - + + +
+
+ +
+
+ + Or continue with + +
+
+ +
+ + + +
@@ -173,7 +221,7 @@ const Auth = () => {
{authError}
)} - + + +
+
+ +
+
+ + Or continue with + +
+
+ +
+ + + +