fix: tidy up code

This commit is contained in:
Mythie
2023-09-05 11:29:23 +10:00
parent 692722d32e
commit 6e095921e6
3 changed files with 33 additions and 30 deletions

View File

@ -1,6 +1,6 @@
'use client';
import { useEffect, useRef } from 'react';
import { useEffect } from 'react';
import { useSearchParams } from 'next/navigation';
@ -11,7 +11,7 @@ import { useForm } from 'react-hook-form';
import { FcGoogle } from 'react-icons/fc';
import { z } from 'zod';
import { ErrorCodes } from '@documenso/lib/next-auth/error-codes';
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 { Input } from '@documenso/ui/primitives/input';
@ -19,9 +19,10 @@ import { Label } from '@documenso/ui/primitives/label';
import { useToast } from '@documenso/ui/primitives/use-toast';
const ErrorMessages = {
[ErrorCodes.CredentialsNotFound]: 'Credentials not found',
[ErrorCodes.IncorrectEmailPassword]: 'Incorrect email or password',
[ErrorCodes.UserMissingPassword]: 'User is missing password',
[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',
};
export const ZSignInFormSchema = z.object({
@ -36,8 +37,8 @@ export type SignInFormProps = {
};
export const SignInForm = ({ className }: SignInFormProps) => {
const searchParams = useSearchParams();
const { toast } = useToast();
const searchParams = useSearchParams();
const {
register,
@ -51,30 +52,26 @@ export const SignInForm = ({ className }: SignInFormProps) => {
resolver: zodResolver(ZSignInFormSchema),
});
const timer = useRef<NodeJS.Timeout | null>(null);
const errorCode = searchParams?.get('error');
useEffect(() => {
const error = searchParams?.get('error');
if (error) {
timer.current = setTimeout(() => {
// FIXME: Toast not firing without the TimeOut
const timeout: NodeJS.Timeout | null = null;
if (isErrorCode(errorCode)) {
setTimeout(() => {
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',
description: ErrorMessages[errorCode] ?? 'An unknown error occurred',
});
}, 100);
}, 0);
}
return () => {
if (timer.current) {
clearInterval(timer.current);
if (timeout) {
clearTimeout(timeout);
}
timer.current = null;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [errorCode, toast]);
const onFormSubmit = async ({ email, password }: TSignInFormSchema) => {
try {

View File

@ -7,7 +7,7 @@ import GoogleProvider, { GoogleProfile } from 'next-auth/providers/google';
import { prisma } from '@documenso/prisma';
import { getUserByEmail } from '../server-only/user/get-user-by-email';
import { ErrorCodes } from './error-codes';
import { ErrorCode } from './error-codes';
export const NEXT_AUTH_OPTIONS: AuthOptions = {
adapter: PrismaAdapter(prisma),
@ -24,23 +24,23 @@ export const NEXT_AUTH_OPTIONS: AuthOptions = {
},
authorize: async (credentials, _req) => {
if (!credentials) {
throw new Error(ErrorCodes.CredentialsNotFound);
throw new Error(ErrorCode.CREDENTIALS_NOT_FOUND);
}
const { email, password } = credentials;
const user = await getUserByEmail({ email }).catch(() => {
throw new Error(ErrorCodes.IncorrectEmailPassword);
throw new Error(ErrorCode.INCORRECT_EMAIL_PASSWORD);
});
if (!user.password) {
throw new Error(ErrorCodes.UserMissingPassword);
throw new Error(ErrorCode.USER_MISSING_PASSWORD);
}
const isPasswordsSame = await compare(password, user.password);
if (!isPasswordsSame) {
throw new Error(ErrorCodes.IncorrectEmailPassword);
throw new Error(ErrorCode.INCORRECT_EMAIL_PASSWORD);
}
return {

View File

@ -1,5 +1,11 @@
export const ErrorCodes = {
IncorrectEmailPassword: 'incorrect-email-password',
UserMissingPassword: 'missing-password',
CredentialsNotFound: 'credentials-not-found',
export const isErrorCode = (code: unknown): code is ErrorCode => {
return typeof code === 'string' && code in ErrorCode;
};
export type ErrorCode = (typeof ErrorCode)[keyof typeof ErrorCode];
export const ErrorCode = {
INCORRECT_EMAIL_PASSWORD: 'INCORRECT_EMAIL_PASSWORD',
USER_MISSING_PASSWORD: 'USER_MISSING_PASSWORD',
CREDENTIALS_NOT_FOUND: 'CREDENTIALS_NOT_FOUND',
} as const;