-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
+page.server.ts
52 lines (44 loc) · 1.42 KB
/
+page.server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type { Actions, PageServerLoad } from './$types';
import { redirect, fail } from '@sveltejs/kit';
import prisma from '@db';
import { env } from '$env/dynamic/private';
import { sendResetEmail } from '$lib/server/email/reset-password';
export const load: PageServerLoad = async () => {
if (env.MAIL_ENABLED === 'false') {
redirect(303, '/');
}
};
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');
if (!usernameOrEmail) {
return fail(400, {
success: false,
errors: ['All fields are required'],
});
}
const user = await prisma.user.findFirst({
where: {
OR: [
{ username: usernameOrEmail.toString() },
{ email: usernameOrEmail.toString() },
],
},
});
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',
};
},
};