'use client'; import { useState } from 'react'; import { zodResolver } from '@hookform/resolvers/zod'; import { Eye, EyeOff } from 'lucide-react'; import { signIn } from 'next-auth/react'; import { useForm } from 'react-hook-form'; import { FcGoogle } from 'react-icons/fc'; import { z } from 'zod'; import { ErrorCode, isErrorCode } from '@documenso/lib/next-auth/error-codes'; import { cn } from '@documenso/ui/lib/utils'; import { Button } from '@documenso/ui/primitives/button'; import { FormErrorMessage } from '@documenso/ui/primitives/form/form-error-message'; import { Input } from '@documenso/ui/primitives/input'; import { Label } from '@documenso/ui/primitives/label'; import { useToast } from '@documenso/ui/primitives/use-toast'; const ERROR_MESSAGES = { [ErrorCode.CREDENTIALS_NOT_FOUND]: 'The email or password provided is incorrect', [ErrorCode.INCORRECT_EMAIL_PASSWORD]: 'The email or password provided is incorrect', [ErrorCode.USER_MISSING_PASSWORD]: 'This account appears to be using a social login method, please sign in using that method', }; const LOGIN_REDIRECT_PATH = '/documents'; export const ZSignInFormSchema = z.object({ email: z.string().email().min(1), password: z.string().min(6).max(72), }); export type TSignInFormSchema = z.infer; export type SignInFormProps = { className?: string; }; export const SignInForm = ({ className }: SignInFormProps) => { const { toast } = useToast(); const [showPassword, setShowPassword] = useState(false); const { register, handleSubmit, formState: { errors, isSubmitting }, } = useForm({ values: { email: '', password: '', }, resolver: zodResolver(ZSignInFormSchema), }); const onFormSubmit = async ({ email, password }: TSignInFormSchema) => { try { const result = await signIn('credentials', { email, password, callbackUrl: LOGIN_REDIRECT_PATH, redirect: false, }); if (result?.error && isErrorCode(result.error)) { toast({ variant: 'destructive', description: ERROR_MESSAGES[result.error], }); return; } if (!result?.url) { throw new Error('An unknown error occurred'); } window.location.href = result.url; } catch (err) { toast({ title: 'An unknown error occurred', description: 'We encountered an unknown error while attempting to sign you In. Please try again later.', }); } }; const onSignInWithGoogleClick = async () => { try { await signIn('google', { callbackUrl: LOGIN_REDIRECT_PATH }); } catch (err) { toast({ title: 'An unknown error occurred', description: 'We encountered an unknown error while attempting to sign you In. Please try again later.', variant: 'destructive', }); } }; return (
Or continue with
); };