import { useState } from 'react'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Separator } from '@/components/ui/separator'; import { Badge } from '@/components/ui/badge'; import { useToast } from '@/hooks/use-toast'; import { useAuth } from '@/hooks/useAuth'; import { supabase } from '@/integrations/supabase/client'; import { Shield, Key, Smartphone, Globe, ExternalLink } from 'lucide-react'; const passwordSchema = z.object({ currentPassword: z.string().min(1, 'Current password is required'), newPassword: z.string().min(8, 'Password must be at least 8 characters'), confirmPassword: z.string() }).refine((data) => data.newPassword === data.confirmPassword, { message: "Passwords don't match", path: ["confirmPassword"] }); type PasswordFormData = z.infer; export function SecurityTab() { const { user } = useAuth(); const { toast } = useToast(); const [loading, setLoading] = useState(false); const form = useForm({ resolver: zodResolver(passwordSchema), defaultValues: { currentPassword: '', newPassword: '', confirmPassword: '' } }); const onSubmit = async (data: PasswordFormData) => { if (!user) return; setLoading(true); try { const { error } = await supabase.auth.updateUser({ password: data.newPassword }); if (error) throw error; form.reset(); toast({ title: 'Password updated', description: 'Your password has been successfully changed.' }); } catch (error: any) { toast({ title: 'Error', description: error.message || 'Failed to update password', variant: 'destructive' }); } finally { setLoading(false); } }; const handleSocialLogin = async (provider: 'google') => { try { const { error } = await supabase.auth.signInWithOAuth({ provider: provider }); if (error) throw error; toast({ title: 'Redirecting...', description: `Redirecting to ${provider} to link your account.` }); } catch (error: any) { toast({ title: 'Error', description: error.message || `Failed to link ${provider} account`, variant: 'destructive' }); } }; const handleUnlinkSocial = async (provider: 'google') => { try { // Note: Supabase doesn't have a direct unlink method // This would typically be handled through the admin API or by the user toast({ title: 'Feature not available', description: 'Please contact support to unlink social accounts.', }); } catch (error: any) { toast({ title: 'Error', description: error.message || `Failed to unlink ${provider} account`, variant: 'destructive' }); } }; // Mock data for connected accounts - in real app this would come from user metadata const connectedAccounts = [ { provider: 'google', connected: user?.app_metadata?.providers?.includes('google') || false, email: user?.email } ]; return (
{/* Change Password */}

Change Password

Update your password to keep your account secure.
{form.formState.errors.currentPassword && (

{form.formState.errors.currentPassword.message}

)}
{form.formState.errors.newPassword && (

{form.formState.errors.newPassword.message}

)}
{form.formState.errors.confirmPassword && (

{form.formState.errors.confirmPassword.message}

)}
{/* Social Login Connections */}

Connected Accounts

Manage your social login connections for easier access to your account. {connectedAccounts.map((account) => (

{account.provider}

{account.connected && account.email && (

{account.email}

)}
{account.connected ? ( <> Connected ) : ( )}
))}
{/* Two-Factor Authentication */}

Two-Factor Authentication

Add an extra layer of security to your account with two-factor authentication.

Authenticator App

Use an authenticator app to generate verification codes

{/* Login History */}

Login History

Review your recent login activity and active sessions.

Current Session

Web • {new Date().toLocaleDateString()}

Active

No other recent sessions found

); }