mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
fix: add layout and minor updates
This commit is contained in:
@ -3,54 +3,51 @@ import crypto from 'crypto';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import { TForgotPasswordFormSchema } from '@documenso/trpc/server/profile-router/schema';
|
||||
|
||||
import { ONE_DAY, ONE_HOUR } from '../../constants/time';
|
||||
import { sendForgotPassword } from '../auth/send-forgot-password';
|
||||
|
||||
export const forgotPassword = async ({ email }: TForgotPasswordFormSchema) => {
|
||||
let user;
|
||||
try {
|
||||
user = await prisma.user.findFirstOrThrow({
|
||||
where: {
|
||||
email: email.toLowerCase(),
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
email: {
|
||||
equals: email,
|
||||
mode: 'insensitive',
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error('No account found with that email address.');
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new Error('No account found with that email address.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Find a token that was created in the last day and hasn't expired
|
||||
const existingToken = await prisma.passwordResetToken.findFirst({
|
||||
where: {
|
||||
userId: user.id,
|
||||
expiry: {
|
||||
lt: new Date(),
|
||||
},
|
||||
createdAt: {
|
||||
gte: new Date(Date.now() - 1000 * 60 * 60),
|
||||
gt: new Date(Date.now() - ONE_HOUR),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (existingToken) {
|
||||
throw new Error('A password reset email has been sent.');
|
||||
return;
|
||||
}
|
||||
|
||||
const token = crypto.randomBytes(64).toString('hex');
|
||||
const expiry = new Date();
|
||||
expiry.setHours(expiry.getHours() + 24); // Set expiry to one hour from now
|
||||
const token = crypto.randomBytes(18).toString('hex');
|
||||
|
||||
try {
|
||||
await prisma.passwordResetToken.create({
|
||||
data: {
|
||||
token,
|
||||
expiry,
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error('We were unable to send your email. Please try again.');
|
||||
}
|
||||
|
||||
return await sendForgotPassword({
|
||||
userId: user.id,
|
||||
await prisma.passwordResetToken.create({
|
||||
data: {
|
||||
token,
|
||||
expiry: new Date(Date.now() + ONE_DAY),
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
await sendForgotPassword({
|
||||
userId: user.id,
|
||||
}).catch((err) => console.error(err));
|
||||
};
|
||||
|
||||
18
packages/lib/server-only/user/get-reset-token-validity.ts
Normal file
18
packages/lib/server-only/user/get-reset-token-validity.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
type GetResetTokenValidityOptions = {
|
||||
token: string;
|
||||
};
|
||||
|
||||
export const getResetTokenValidity = async ({ token }: GetResetTokenValidityOptions) => {
|
||||
const found = await prisma.passwordResetToken.findFirst({
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
where: {
|
||||
token,
|
||||
},
|
||||
});
|
||||
|
||||
return !!found;
|
||||
};
|
||||
@ -15,7 +15,7 @@ export const resetPassword = async ({ token, password }: ResetPasswordOptions) =
|
||||
throw new Error('Invalid token provided. Please try again.');
|
||||
}
|
||||
|
||||
const foundToken = await prisma.passwordResetToken.findFirstOrThrow({
|
||||
const foundToken = await prisma.passwordResetToken.findFirst({
|
||||
where: {
|
||||
token,
|
||||
},
|
||||
@ -34,7 +34,7 @@ export const resetPassword = async ({ token, password }: ResetPasswordOptions) =
|
||||
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 || '');
|
||||
|
||||
if (isSamePassword) {
|
||||
throw new Error('Your new password cannot be the same as your old password.');
|
||||
@ -42,7 +42,7 @@ export const resetPassword = async ({ token, password }: ResetPasswordOptions) =
|
||||
|
||||
const hashedPassword = await hash(password, SALT_ROUNDS);
|
||||
|
||||
const transactions = await prisma.$transaction([
|
||||
await prisma.$transaction([
|
||||
prisma.user.update({
|
||||
where: {
|
||||
id: foundToken.userId,
|
||||
@ -58,10 +58,5 @@ export const resetPassword = async ({ token, password }: ResetPasswordOptions) =
|
||||
}),
|
||||
]);
|
||||
|
||||
if (!transactions) {
|
||||
throw new Error('We were unable to reset your password. Please try again.');
|
||||
}
|
||||
|
||||
await sendResetPassword({ userId: foundToken.userId });
|
||||
return transactions;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user