'use client'; import * as React from 'react'; import { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'; import { useAuth } from '@/hooks/useAuth'; import { useAuthMethods } from '@/hooks/useAuthMethods'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { authClient } from '@/lib/auth-client'; import { Separator } from '@/components/ui/separator'; import { toast, Toaster } from 'sonner'; import { showErrorToast } from '@/lib/utils'; import { Loader2, Mail, Globe, Eye, EyeOff } from 'lucide-react'; export function LoginForm() { const [isLoading, setIsLoading] = useState(false); const [showPassword, setShowPassword] = useState(false); const [ssoEmail, setSsoEmail] = useState(''); const { login } = useAuth(); const { authMethods, isLoading: isLoadingMethods } = useAuthMethods(); // Determine which tab to show by default const getDefaultTab = () => { if (authMethods.emailPassword) return 'email'; if (authMethods.sso.enabled) return 'sso'; return 'email'; // fallback }; async function handleLogin(e: React.FormEvent) { e.preventDefault(); setIsLoading(true); const form = e.currentTarget; const formData = new FormData(form); const email = formData.get('email') as string | null; const password = formData.get('password') as string | null; if (!email || !password) { toast.error('Please enter both email and password'); setIsLoading(false); return; } try { await login(email, password); toast.success('Login successful!'); // Small delay before redirecting to see the success message setTimeout(() => { window.location.href = '/'; }, 1000); } catch (error) { showErrorToast(error, toast); } finally { setIsLoading(false); } } async function handleSSOLogin(domain?: string, providerId?: string) { setIsLoading(true); try { if (!domain && !ssoEmail) { toast.error('Please enter your email or select a provider'); return; } const baseURL = typeof window !== 'undefined' ? window.location.origin : 'http://localhost:4321'; await authClient.signIn.sso({ email: ssoEmail || undefined, domain: domain, providerId: providerId, callbackURL: `${baseURL}/`, errorCallbackURL: `${baseURL}/auth-error`, newUserCallbackURL: `${baseURL}/`, scopes: ['openid', 'email', 'profile'], // TODO: This is not being respected by the SSO plugin. }); } catch (error) { showErrorToast(error, toast); } finally { setIsLoading(false); } } return ( <>
Gitea Mirror Logo
Gitea Mirror Log in to manage your GitHub to Gitea mirroring
{isLoadingMethods ? (
) : ( <> {/* Show tabs only if multiple auth methods are available */} {authMethods.sso.enabled && authMethods.emailPassword ? ( Email SSO
{authMethods.sso.providers.length > 0 && ( <>

Sign in with your organization account

{authMethods.sso.providers.map(provider => ( ))}
Or
)}
setSsoEmail(e.target.value)} className="w-full rounded-md border border-input bg-background px-3 py-2 text-sm shadow-sm transition-colors placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring" placeholder="Enter your work email" disabled={isLoading} />

We'll redirect you to your organization's SSO provider

) : ( // Single auth method - show email/password only <>
)} )}

Don't have an account? Contact your administrator.

); }