language-learning-app/frontend/src/routes/register/+page.server.ts

29 lines
785 B
TypeScript
Raw Normal View History

import { redirect, type Actions, type ServerLoad } from '@sveltejs/kit';
import { registerApiAuthRegisterPost } from '../../client/sdk.gen.ts';
export const load: ServerLoad = async ({ locals }) => {
if (locals.authToken) redirect(307, '/app');
return {};
};
export const actions = {
default: async ({ request }) => {
const data = await request.formData();
const email = data.get('email') as string;
const password = data.get('password') as string;
const { response, data: body } = await registerApiAuthRegisterPost({
body: { email, password }
});
if (response.status === 201 && body?.success) {
return { success: true };
}
return {
success: false,
error: body?.error_message ?? 'Registration failed. Please try again.'
};
}
} satisfies Actions;