mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
feat: better error handling and better toast messages
This commit is contained in:
committed by
Mythie
parent
1e0cde850a
commit
dec7a9cb38
@ -6,14 +6,19 @@ import { TForgotPasswordFormSchema } from '@documenso/trpc/server/profile-router
|
||||
import { sendForgotPassword } from '../auth/send-forgot-password';
|
||||
|
||||
export const forgotPassword = async ({ email }: TForgotPasswordFormSchema) => {
|
||||
const user = await prisma.user.findFirstOrThrow({
|
||||
where: {
|
||||
email: email.toLowerCase(),
|
||||
},
|
||||
});
|
||||
let user;
|
||||
try {
|
||||
user = await prisma.user.findFirstOrThrow({
|
||||
where: {
|
||||
email: email.toLowerCase(),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error('No account found with that email address.');
|
||||
}
|
||||
|
||||
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({
|
||||
@ -42,7 +47,7 @@ export const forgotPassword = async ({ email }: TForgotPasswordFormSchema) => {
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error('Something went wrong');
|
||||
throw new Error('We were unable to send your email. Please try again.');
|
||||
}
|
||||
|
||||
return await sendForgotPassword({
|
||||
|
||||
@ -12,7 +12,7 @@ export type ResetPasswordOptions = {
|
||||
|
||||
export const resetPassword = async ({ token, password }: ResetPasswordOptions) => {
|
||||
if (!token) {
|
||||
throw new Error('Invalid Token');
|
||||
throw new Error('Invalid token provided. Please try again.');
|
||||
}
|
||||
|
||||
const foundToken = await prisma.passwordResetToken.findFirstOrThrow({
|
||||
@ -25,13 +25,13 @@ export const resetPassword = async ({ token, password }: ResetPasswordOptions) =
|
||||
});
|
||||
|
||||
if (!foundToken) {
|
||||
throw new Error('Invalid Token');
|
||||
throw new Error('Invalid token provided. Please try again.');
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
|
||||
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!);
|
||||
@ -59,7 +59,7 @@ export const resetPassword = async ({ token, password }: ResetPasswordOptions) =
|
||||
]);
|
||||
|
||||
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 });
|
||||
|
||||
Reference in New Issue
Block a user