40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
|
|
import { loginAuthLoginPost } from '../../client/sdk.gen.ts';
|
||
|
|
import { redirect, type Actions, type ServerLoad } from '@sveltejs/kit';
|
||
|
|
|
||
|
|
export const load: ServerLoad = async ({ locals }) => {
|
||
|
|
if (locals.authToken) {
|
||
|
|
return redirect(307, '/');
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
isLoggedIn: !!locals.authToken
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
export const actions = {
|
||
|
|
default: async ({ request, locals, cookies }) => {
|
||
|
|
const formData = await request.formData();
|
||
|
|
const email = formData.get('email') as string;
|
||
|
|
const password = formData.get('password') as string;
|
||
|
|
|
||
|
|
const { response, data } = await loginAuthLoginPost({
|
||
|
|
headers: {
|
||
|
|
'Content-Type': 'application/json',
|
||
|
|
Authorization: locals.authToken ? `Bearer ${locals.authToken}` : ''
|
||
|
|
},
|
||
|
|
body: { email, password }
|
||
|
|
});
|
||
|
|
|
||
|
|
if (response.status === 200 && data) {
|
||
|
|
cookies.set('auth_token', data.access_token, {
|
||
|
|
path: '/',
|
||
|
|
expires: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000) // 7 days
|
||
|
|
});
|
||
|
|
|
||
|
|
return redirect(307, '/');
|
||
|
|
}
|
||
|
|
|
||
|
|
return { success: response.status === 200, error: response.status !== 200 ? data : null };
|
||
|
|
}
|
||
|
|
} satisfies Actions;
|