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' | 'discord') => { try { const { error } = await supabase.auth.signInWithOAuth({ provider: provider, options: { redirectTo: `${window.location.origin}/settings` } }); 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' | 'discord') => { try { // For now, show a message that this feature requires manual action // In a production app, this would typically be handled through the admin API toast({ title: `${provider.charAt(0).toUpperCase() + provider.slice(1)} Account`, description: `To unlink your ${provider} account, please sign in without using ${provider} and then you can remove this connection. For assistance, contact support.`, }); } catch (error: any) { toast({ title: 'Error', description: error.message || `Failed to unlink ${provider} account`, variant: 'destructive' }); } }; // Get connected accounts from user identities const connectedAccounts = [ { provider: 'google', connected: user?.identities?.some(identity => identity.provider === 'google') || false, email: user?.identities?.find(identity => identity.provider === 'google')?.identity_data?.email || user?.email, icon: '🔍' }, { provider: 'discord', connected: user?.identities?.some(identity => identity.provider === 'discord') || false, email: user?.identities?.find(identity => identity.provider === 'discord')?.identity_data?.email, icon: '🎮' } ]; 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. To enable social providers, configure them in your Supabase Auth settings. {connectedAccounts.map((account) => (
{account.icon}

{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

); }