2023-06-09 18:21:18 +10:00
|
|
|
'use client';
|
|
|
|
|
|
2023-09-02 11:46:12 +05:30
|
|
|
import { useEffect, useRef } from 'react';
|
|
|
|
|
|
2023-09-01 16:25:27 +05:30
|
|
|
import { useSearchParams } from 'next/navigation';
|
2023-08-26 12:31:40 +05:30
|
|
|
|
2023-06-09 18:21:18 +10:00
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
2023-09-02 11:46:12 +05:30
|
|
|
import { Loader } from 'lucide-react';
|
2023-06-09 18:21:18 +10:00
|
|
|
import { signIn } from 'next-auth/react';
|
|
|
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
|
import { FcGoogle } from 'react-icons/fc';
|
|
|
|
|
import { z } from 'zod';
|
|
|
|
|
|
2023-09-01 16:25:27 +05:30
|
|
|
import { ErrorCodes } from '@documenso/lib/next-auth/error-codes';
|
2023-06-09 18:21:18 +10:00
|
|
|
import { cn } from '@documenso/ui/lib/utils';
|
|
|
|
|
import { Button } from '@documenso/ui/primitives/button';
|
|
|
|
|
import { Input } from '@documenso/ui/primitives/input';
|
|
|
|
|
import { Label } from '@documenso/ui/primitives/label';
|
|
|
|
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
|
|
|
|
|
2023-09-01 16:25:27 +05:30
|
|
|
const ErrorMessages = {
|
|
|
|
|
[ErrorCodes.CredentialsNotFound]: 'Credentials not found',
|
|
|
|
|
[ErrorCodes.IncorrectEmailPassword]: 'Incorrect email or password',
|
|
|
|
|
[ErrorCodes.UserMissingPassword]: 'User is missing password',
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-09 18:21:18 +10:00
|
|
|
export const ZSignInFormSchema = z.object({
|
|
|
|
|
email: z.string().email().min(1),
|
2023-08-26 09:53:58 -05:00
|
|
|
password: z.string().min(6).max(72),
|
2023-06-09 18:21:18 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export type TSignInFormSchema = z.infer<typeof ZSignInFormSchema>;
|
|
|
|
|
|
|
|
|
|
export type SignInFormProps = {
|
|
|
|
|
className?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const SignInForm = ({ className }: SignInFormProps) => {
|
2023-09-01 16:25:27 +05:30
|
|
|
const searchParams = useSearchParams();
|
2023-06-09 18:21:18 +10:00
|
|
|
const { toast } = useToast();
|
2023-08-26 12:31:40 +05:30
|
|
|
const [showPassword, setShowPassword] = useState(false);
|
2023-06-09 18:21:18 +10:00
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
register,
|
|
|
|
|
handleSubmit,
|
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
|
} = useForm<TSignInFormSchema>({
|
|
|
|
|
values: {
|
|
|
|
|
email: '',
|
|
|
|
|
password: '',
|
|
|
|
|
},
|
|
|
|
|
resolver: zodResolver(ZSignInFormSchema),
|
|
|
|
|
});
|
|
|
|
|
|
2023-09-02 11:46:12 +05:30
|
|
|
const timer = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const error = searchParams?.get('error');
|
|
|
|
|
if (error) {
|
|
|
|
|
timer.current = setTimeout(() => {
|
2023-09-02 11:51:21 +05:30
|
|
|
// FIXME: Toast not firing without the TimeOut
|
2023-09-02 11:46:12 +05:30
|
|
|
toast({
|
|
|
|
|
variant: 'destructive',
|
|
|
|
|
description:
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
|
|
|
ErrorMessages[searchParams?.get('error') as keyof typeof ErrorMessages] ??
|
|
|
|
|
'an unknown error occurred',
|
|
|
|
|
});
|
|
|
|
|
}, 100);
|
|
|
|
|
}
|
|
|
|
|
return () => {
|
|
|
|
|
if (timer.current) {
|
|
|
|
|
clearInterval(timer.current);
|
|
|
|
|
}
|
|
|
|
|
timer.current = null;
|
|
|
|
|
};
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, []);
|
|
|
|
|
|
2023-06-09 18:21:18 +10:00
|
|
|
const onFormSubmit = async ({ email, password }: TSignInFormSchema) => {
|
|
|
|
|
try {
|
|
|
|
|
await signIn('credentials', {
|
|
|
|
|
email,
|
|
|
|
|
password,
|
2023-08-16 09:03:28 +02:00
|
|
|
callbackUrl: '/documents',
|
2023-06-09 18:21:18 +10:00
|
|
|
}).catch((err) => {
|
|
|
|
|
console.error(err);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// throw new Error('Not implemented');
|
|
|
|
|
} 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 {
|
2023-06-13 01:53:12 -04:00
|
|
|
await signIn('google', { callbackUrl: '/dashboard' });
|
|
|
|
|
// throw new Error('Not implemented');
|
2023-06-09 18:21:18 +10:00
|
|
|
} 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 (
|
2023-09-02 11:46:12 +05:30
|
|
|
<form
|
|
|
|
|
className={cn('flex w-full flex-col gap-y-4', className)}
|
|
|
|
|
onSubmit={handleSubmit(onFormSubmit)}
|
|
|
|
|
>
|
|
|
|
|
<div>
|
|
|
|
|
<Label htmlFor="email" className="text-slate-500">
|
|
|
|
|
Email
|
|
|
|
|
</Label>
|
|
|
|
|
|
|
|
|
|
<Input id="email" type="email" className="bg-background mt-2" {...register('email')} />
|
|
|
|
|
|
|
|
|
|
{errors.email && <span className="mt-1 text-xs text-red-500">{errors.email.message}</span>}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<Label htmlFor="password" className="text-slate-500">
|
|
|
|
|
Password
|
|
|
|
|
</Label>
|
|
|
|
|
|
|
|
|
|
<Input
|
|
|
|
|
id="password"
|
|
|
|
|
type="password"
|
|
|
|
|
minLength={6}
|
|
|
|
|
maxLength={72}
|
|
|
|
|
autoComplete="current-password"
|
|
|
|
|
className="bg-background mt-2"
|
|
|
|
|
{...register('password')}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{errors.password && (
|
|
|
|
|
<span className="mt-1 text-xs text-red-500">{errors.password.message}</span>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<Button size="lg" disabled={isSubmitting} className="dark:bg-documenso dark:hover:opacity-90">
|
|
|
|
|
{isSubmitting && <Loader className="mr-2 h-5 w-5 animate-spin" />}
|
|
|
|
|
Sign In
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
<div className="relative flex items-center justify-center gap-x-4 py-2 text-xs uppercase">
|
|
|
|
|
<div className="bg-border h-px flex-1" />
|
|
|
|
|
<span className="text-muted-foreground bg-transparent">Or continue with</span>
|
|
|
|
|
<div className="bg-border h-px flex-1" />
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
size="lg"
|
|
|
|
|
variant={'outline'}
|
|
|
|
|
className="bg-background text-muted-foreground border"
|
|
|
|
|
disabled={isSubmitting}
|
|
|
|
|
onClick={onSignInWithGoogleClick}
|
2023-06-09 18:21:18 +10:00
|
|
|
>
|
2023-09-02 11:46:12 +05:30
|
|
|
<FcGoogle className="mr-2 h-5 w-5" />
|
|
|
|
|
Google
|
|
|
|
|
</Button>
|
|
|
|
|
</form>
|
2023-06-09 18:21:18 +10:00
|
|
|
);
|
|
|
|
|
};
|