-
Notifications
You must be signed in to change notification settings - Fork 670
Expand file tree
/
Copy path+page.server.ts
More file actions
50 lines (42 loc) · 1.54 KB
/
+page.server.ts
File metadata and controls
50 lines (42 loc) · 1.54 KB
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
import { fail, redirect, type Actions } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { ChangePasswordSchema } from '$lib/utils/schemas';
import { setError, superValidate } from 'sveltekit-superforms';
import { setFlash } from 'sveltekit-flash-message/server';
import { BASE_API_URL } from '$lib/utils/constants';
import { m } from '$paraglide/messages';
import { zod4 as zod } from 'sveltekit-superforms/adapters';
export const load: PageServerLoad = async (event) => {
const form = await superValidate(event.request, zod(ChangePasswordSchema));
return { form };
};
export const actions: Actions = {
default: async (event) => {
const form = await superValidate(event.request, zod(ChangePasswordSchema));
if (!form.valid) {
return fail(400, { form });
}
const endpoint = `${BASE_API_URL}/iam/change-password/`;
const requestInitOptions: RequestInit = {
method: 'POST',
body: JSON.stringify(form.data)
};
const res = await event.fetch(endpoint, requestInitOptions);
if (!res.ok) {
const response = await res.json();
console.error('server response:', response);
if (response) {
setError(form, 'old_password', response);
}
if (response.new_password) {
setError(form, 'new_password', response.new_password);
}
if (response.confirm_new_password) {
setError(form, 'confirm_new_password', response.confirm_new_password);
}
return fail(res.status, { form });
}
setFlash({ type: 'success', message: m.passwordSuccessfullyChanged() }, event);
redirect(302, '/my-profile');
}
};