2024-06-25 02:45:00 +00:00
|
|
|
import { updateEmailDto } from "$lib/dtos/update-email.dto.js";
|
|
|
|
|
import { verifyEmailDto } from "$lib/dtos/verify-email.dto.js";
|
|
|
|
|
import { fail, setError, superValidate } from "sveltekit-superforms";
|
|
|
|
|
import { zod } from "sveltekit-superforms/adapters";
|
|
|
|
|
|
2024-06-25 16:14:45 +00:00
|
|
|
export let load = async (event) => {
|
|
|
|
|
const authedUser = await event.locals.getAuthedUserOrThrow()
|
2024-06-25 02:45:00 +00:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
authedUser,
|
|
|
|
|
updateEmailForm: await superValidate(authedUser, zod(updateEmailDto)),
|
|
|
|
|
verifyEmailForm: await superValidate(zod(verifyEmailDto))
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
|
updateEmail: async ({ request, locals }) => {
|
|
|
|
|
const updateEmailForm = await superValidate(request, zod(updateEmailDto));
|
|
|
|
|
if (!updateEmailForm.valid) return fail(400, { updateEmailForm })
|
|
|
|
|
const { data, error } = await locals.api.iam.email.update.$post({ json: updateEmailForm.data }).then(locals.parseApiResponse);
|
|
|
|
|
if (error) return setError(updateEmailForm, 'email', error);
|
|
|
|
|
return { updateEmailForm }
|
|
|
|
|
},
|
|
|
|
|
verifyEmail: async ({ request, locals }) => {
|
|
|
|
|
const verifyEmailForm = await superValidate(request, zod(verifyEmailDto));
|
|
|
|
|
console.log(verifyEmailForm)
|
|
|
|
|
if (!verifyEmailForm.valid) return fail(400, { verifyEmailForm })
|
|
|
|
|
const { error } = await locals.api.iam.email.verify.$post({ json: verifyEmailForm.data }).then(locals.parseApiResponse);
|
|
|
|
|
if (error) return setError(verifyEmailForm, 'token', error);
|
|
|
|
|
return { verifyEmailForm }
|
|
|
|
|
}
|
|
|
|
|
};
|