Skip to content

Commit

Permalink
Minor code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Yureien committed Nov 4, 2023
1 parent 082af73 commit 75e11b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/lib/server/email/reset-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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';

Expand Down
27 changes: 14 additions & 13 deletions src/routes/(auth)/forgot-password/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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' };
}
};

0 comments on commit 75e11b6

Please sign in to comment.