'use client' import { cn } from '@/lib/utils' import { Button } from '@/components/ui/button' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { useRouter, useSearchParams } from 'next/navigation' import { useState } from 'react' import { useGlobal } from '@/providers/GlobalProvider' import { Loader2 } from 'lucide-react' import { toast } from 'sonner' export function LoginForm({ className, ...props }: React.ComponentProps<'div'>) { const router = useRouter() const { setUser } = useGlobal() const [isLoading, setIsLoading] = useState(false) const searchParams = useSearchParams() const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!isLoading) { const formData = new FormData(e.currentTarget) const email = String(formData.get('email')) const password = String(formData.get('password')) if (!email || !password) return setIsLoading(true) try { const loginReq = await fetch('/api/users/login', { method: 'POST', credentials: 'include', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email, password, }), }) const loginResponse = await loginReq.json() if (loginResponse.errors?.length) { loginResponse.errors.forEach((e: Error) => { toast(e.message) }) } if (loginResponse.user) setUser(loginResponse.user) if (loginResponse.token) router.push(searchParams.get('redirectUrl') || '/') } catch (error) { toast('Unknown issue while authenticating. Try again') setIsLoading(false) } } } return (
Welcome back Login to access your account
By clicking Login or Request Access, you agree to our{' '} Terms of Service and Privacy Policy.
) }