import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import { loginSchema, type LoginInput } from "@/lib/validations/auth" import { AuthService } from "@/services/auth" import { Button } from "@/components/ui/button" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { useState } from "react" import { Separator } from "@/components/ui/separator" import axios from "axios" interface LoginFormProps { onSuccess: () => void onToggleMode: () => void } export function LoginForm({ onSuccess, onToggleMode }: LoginFormProps) { const [error, setError] = useState(null) const [isLoading, setIsLoading] = useState(false) const form = useForm({ resolver: zodResolver(loginSchema), defaultValues: { email: "", password: "", }, }) async function onSubmit(values: LoginInput) { setIsLoading(true) setError(null) try { await AuthService.login(values.email, values.password) onSuccess() } catch (err: unknown) { if (axios.isAxiosError(err)) { setError(err.response?.data?.detail || "Login failed. Please check your credentials.") } else { setError("An unexpected error occurred.") } } finally { setIsLoading(false) } } const handleOIDCLogin = async () => { try { await AuthService.loginWithOIDC() } catch (err: unknown) { console.error("SSO Login failed:", err) setError("Failed to initialize SSO login.") } } return ( Login Enter your email and password to access your account.
{error && (
{error}
)} ( Email )} /> ( Password )} />
Or continue with
Don't have an account?{" "}
) }