diff --git a/src/lib/server/email/reset-password.ts b/src/lib/server/email/reset-password.ts index 9457eba..b09913d 100644 --- a/src/lib/server/email/reset-password.ts +++ b/src/lib/server/email/reset-password.ts @@ -6,7 +6,7 @@ import { sendEmail } from './base'; export const sendResetEmail = async (userId: string) => { const user = await prisma.user.findUnique({ where: { id: userId } }); if (!user) return false; - + const resetToken = await generatePasswordResetToken(userId); const verifyUrl = `${env.PUBLIC_URL}/reset-password?token=${encodeURIComponent( @@ -15,7 +15,7 @@ export const sendResetEmail = async (userId: string) => { const content = `To reset your password, please click the following link: ${verifyUrl} - If you did not make this request, please disgregard this email.`; +If you did not make this request, please disgregard this email.`; const subject = 'YABin: Password reset request'; diff --git a/src/routes/(auth)/forgot-password/+page.server.ts b/src/routes/(auth)/forgot-password/+page.server.ts index 479cebb..32e6030 100644 --- a/src/routes/(auth)/forgot-password/+page.server.ts +++ b/src/routes/(auth)/forgot-password/+page.server.ts @@ -12,6 +12,10 @@ export const load: PageServerLoad = async () => { export const actions: Actions = { default: async ({ request }) => { + if (env.MAIL_ENABLED !== 'true') { + return fail(400, { success: false, errors: ['E-mail is disabled'] }); + } + const data = await request.formData(); const usernameOrEmail = data.get('username-email'); @@ -20,20 +24,17 @@ export const actions: Actions = { return fail(400, { success: false, errors: ['All fields are required'] }); } - if (env.MAIL_ENABLED === 'true') { - const user = await prisma.user.findFirst({ - where: { - OR: [{ username: usernameOrEmail.toString() }, { email: usernameOrEmail.toString() }] - } - }); - - if (user) { - sendResetEmail(user.id); + const user = await prisma.user.findFirst({ + where: { + OR: [{ username: usernameOrEmail.toString() }, { email: usernameOrEmail.toString() }] } - // Return success regardless of whether username/email is found or not - return { success: true, message: 'Please check e-mail for a password reset link' }; - } else { - return fail(400, { success: false, errors: ['E-mail is disabled'] }); + }); + + if (user) { + sendResetEmail(user.id); } + + // Return success regardless of whether username/email is found or not + return { success: true, message: 'Please check e-mail for a password reset link' }; } };