feat: better error handling and better toast messages
This commit is contained in:
committed by
Mythie
parent
1e0cde850a
commit
dec7a9cb38
@@ -7,6 +7,7 @@ import { Loader } from 'lucide-react';
|
|||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import { TRPCClientError } from '@documenso/trpc/client';
|
||||||
import { trpc } from '@documenso/trpc/react';
|
import { trpc } from '@documenso/trpc/react';
|
||||||
import { cn } from '@documenso/ui/lib/utils';
|
import { cn } from '@documenso/ui/lib/utils';
|
||||||
import { Button } from '@documenso/ui/primitives/button';
|
import { Button } from '@documenso/ui/primitives/button';
|
||||||
@@ -43,23 +44,33 @@ export const ForgotPasswordForm = ({ className }: ForgotPasswordFormProps) => {
|
|||||||
const { mutateAsync: forgotPassword } = trpc.profile.forgotPassword.useMutation();
|
const { mutateAsync: forgotPassword } = trpc.profile.forgotPassword.useMutation();
|
||||||
|
|
||||||
const onFormSubmit = async ({ email }: TForgotPasswordFormSchema) => {
|
const onFormSubmit = async ({ email }: TForgotPasswordFormSchema) => {
|
||||||
// TODO: Handle error with try/catch
|
try {
|
||||||
await forgotPassword({
|
await forgotPassword({ email });
|
||||||
email,
|
|
||||||
});
|
|
||||||
|
|
||||||
toast({
|
toast({
|
||||||
title: 'Password updated',
|
title: 'Reset email sent',
|
||||||
description: 'Your password has been updated successfully.',
|
description: 'Your password reset mail has been sent successfully.',
|
||||||
duration: 5000,
|
duration: 5000,
|
||||||
});
|
});
|
||||||
|
|
||||||
await new Promise((resolve) => {
|
reset();
|
||||||
setTimeout(resolve, 2000);
|
router.push('/check-email');
|
||||||
});
|
} catch (err) {
|
||||||
|
if (err instanceof TRPCClientError && err.data?.code === 'BAD_REQUEST') {
|
||||||
reset();
|
toast({
|
||||||
router.push('/check-email');
|
title: 'An error occurred',
|
||||||
|
description: err.message,
|
||||||
|
variant: 'destructive',
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
toast({
|
||||||
|
title: 'An unknown error occurred',
|
||||||
|
variant: 'destructive',
|
||||||
|
description:
|
||||||
|
'We encountered an unknown error while attempting to send your email. Please try again later.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { Loader } from 'lucide-react';
|
|||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import { TRPCClientError } from '@documenso/trpc/client';
|
||||||
import { trpc } from '@documenso/trpc/react';
|
import { trpc } from '@documenso/trpc/react';
|
||||||
import { cn } from '@documenso/ui/lib/utils';
|
import { cn } from '@documenso/ui/lib/utils';
|
||||||
import { Button } from '@documenso/ui/primitives/button';
|
import { Button } from '@documenso/ui/primitives/button';
|
||||||
@@ -51,24 +52,38 @@ export const ResetPasswordForm = ({ className, token }: ResetPasswordFormProps)
|
|||||||
|
|
||||||
const { mutateAsync: resetPassword } = trpc.profile.resetPassword.useMutation();
|
const { mutateAsync: resetPassword } = trpc.profile.resetPassword.useMutation();
|
||||||
|
|
||||||
const onFormSubmit = async ({ password, repeatedPassword }: TResetPasswordFormSchema) => {
|
const onFormSubmit = async ({ password }: Omit<TResetPasswordFormSchema, 'repeatedPassword'>) => {
|
||||||
// TODO: Handle error with try/catch
|
try {
|
||||||
console.log(password, repeatedPassword, token);
|
await resetPassword({
|
||||||
|
password,
|
||||||
|
token,
|
||||||
|
});
|
||||||
|
|
||||||
await resetPassword({
|
reset();
|
||||||
password,
|
|
||||||
token,
|
|
||||||
});
|
|
||||||
|
|
||||||
reset();
|
toast({
|
||||||
|
title: 'Password updated',
|
||||||
|
description: 'Your password has been updated successfully.',
|
||||||
|
duration: 5000,
|
||||||
|
});
|
||||||
|
|
||||||
toast({
|
router.push('/signin');
|
||||||
title: 'Password updated',
|
} catch (err) {
|
||||||
description: 'Your password has been updated successfully.',
|
if (err instanceof TRPCClientError && err.data?.code === 'BAD_REQUEST') {
|
||||||
duration: 5000,
|
toast({
|
||||||
});
|
title: 'An error occurred',
|
||||||
|
description: err.message,
|
||||||
router.push('/signin');
|
variant: 'destructive',
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
toast({
|
||||||
|
title: 'An unknown error occurred',
|
||||||
|
variant: 'destructive',
|
||||||
|
description:
|
||||||
|
'We encountered an unknown error while attempting to reset your password. Please try again later.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ export interface SendResetPasswordOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const sendResetPassword = async ({ userId }: SendResetPasswordOptions) => {
|
export const sendResetPassword = async ({ userId }: SendResetPasswordOptions) => {
|
||||||
|
// TODO: Better Error Handling
|
||||||
|
|
||||||
const user = await prisma.user.findFirstOrThrow({
|
const user = await prisma.user.findFirstOrThrow({
|
||||||
where: {
|
where: {
|
||||||
id: userId,
|
id: userId,
|
||||||
|
|||||||
@@ -6,14 +6,19 @@ import { TForgotPasswordFormSchema } from '@documenso/trpc/server/profile-router
|
|||||||
import { sendForgotPassword } from '../auth/send-forgot-password';
|
import { sendForgotPassword } from '../auth/send-forgot-password';
|
||||||
|
|
||||||
export const forgotPassword = async ({ email }: TForgotPasswordFormSchema) => {
|
export const forgotPassword = async ({ email }: TForgotPasswordFormSchema) => {
|
||||||
const user = await prisma.user.findFirstOrThrow({
|
let user;
|
||||||
where: {
|
try {
|
||||||
email: email.toLowerCase(),
|
user = await prisma.user.findFirstOrThrow({
|
||||||
},
|
where: {
|
||||||
});
|
email: email.toLowerCase(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error('No account found with that email address.');
|
||||||
|
}
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new Error('A password reset email has been sent.');
|
throw new Error('No account found with that email address.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const existingToken = await prisma.passwordResetToken.findFirst({
|
const existingToken = await prisma.passwordResetToken.findFirst({
|
||||||
@@ -42,7 +47,7 @@ export const forgotPassword = async ({ email }: TForgotPasswordFormSchema) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error('Something went wrong');
|
throw new Error('We were unable to send your email. Please try again.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return await sendForgotPassword({
|
return await sendForgotPassword({
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ export type ResetPasswordOptions = {
|
|||||||
|
|
||||||
export const resetPassword = async ({ token, password }: ResetPasswordOptions) => {
|
export const resetPassword = async ({ token, password }: ResetPasswordOptions) => {
|
||||||
if (!token) {
|
if (!token) {
|
||||||
throw new Error('Invalid Token');
|
throw new Error('Invalid token provided. Please try again.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const foundToken = await prisma.passwordResetToken.findFirstOrThrow({
|
const foundToken = await prisma.passwordResetToken.findFirstOrThrow({
|
||||||
@@ -25,13 +25,13 @@ export const resetPassword = async ({ token, password }: ResetPasswordOptions) =
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!foundToken) {
|
if (!foundToken) {
|
||||||
throw new Error('Invalid Token');
|
throw new Error('Invalid token provided. Please try again.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
|
||||||
if (now > foundToken.expiry) {
|
if (now > foundToken.expiry) {
|
||||||
throw new Error('Token has expired');
|
throw new Error('Token has expired. Please try again.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const isSamePassword = await compare(password, foundToken.User.password!);
|
const isSamePassword = await compare(password, foundToken.User.password!);
|
||||||
@@ -59,7 +59,7 @@ export const resetPassword = async ({ token, password }: ResetPasswordOptions) =
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
if (!transactions) {
|
if (!transactions) {
|
||||||
throw new Error('Unable to update password');
|
throw new Error('We were unable to reset your password. Please try again.');
|
||||||
}
|
}
|
||||||
|
|
||||||
await sendResetPassword({ userId: foundToken.userId });
|
await sendResetPassword({ userId: foundToken.userId });
|
||||||
|
|||||||
@@ -69,8 +69,7 @@ export const profileRouter = router({
|
|||||||
email,
|
email,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
let message =
|
let message = 'We were unable to send your email. Please try again.';
|
||||||
'We were unable to update your profile. Please review the information you provided and try again.';
|
|
||||||
|
|
||||||
if (err instanceof Error) {
|
if (err instanceof Error) {
|
||||||
message = err.message;
|
message = err.message;
|
||||||
@@ -92,8 +91,7 @@ export const profileRouter = router({
|
|||||||
password,
|
password,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
let message =
|
let message = 'We were unable to reset your password. Please try again.';
|
||||||
'We were unable to update your profile. Please review the information you provided and try again.';
|
|
||||||
|
|
||||||
if (err instanceof Error) {
|
if (err instanceof Error) {
|
||||||
message = err.message;
|
message = err.message;
|
||||||
|
|||||||
Reference in New Issue
Block a user