53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
|
|
import { backend } from "$lib/backend";
|
||
|
|
import { error, type Actions } from "@sveltejs/kit";
|
||
|
|
import { redirect } from 'sveltekit-flash-message/server';
|
||
|
|
|
||
|
|
|
||
|
|
export const actions = {
|
||
|
|
default: async (event) => {
|
||
|
|
|
||
|
|
const { request } = event;
|
||
|
|
|
||
|
|
const data = await request.formData();
|
||
|
|
const username = data.get('username');
|
||
|
|
const password = data.get('password');
|
||
|
|
const remember = !!data.get('remember');
|
||
|
|
|
||
|
|
const backend2 = await backend.withEvent(event).oath();
|
||
|
|
const resp = await backend2.submitJson('login', {
|
||
|
|
username,
|
||
|
|
password,
|
||
|
|
remember
|
||
|
|
});
|
||
|
|
|
||
|
|
const { status } = resp;
|
||
|
|
const respData = await resp.json();
|
||
|
|
|
||
|
|
if ([200, 204].indexOf(status) < 0) {
|
||
|
|
// login error
|
||
|
|
console.log(`/login: status ${status}, data below`);
|
||
|
|
console.debug(respData);
|
||
|
|
switch(status) {
|
||
|
|
case 400:
|
||
|
|
redirect({message: 'Invalid login'}, event);
|
||
|
|
break;
|
||
|
|
case 404:
|
||
|
|
redirect({message: 'Invalid username or password'}, event);
|
||
|
|
break;
|
||
|
|
case 403:
|
||
|
|
redirect({message: 'Login not allowed'}, event);
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
redirect({message: `Unknown error (HTTP ${status})`}, event);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
// login success
|
||
|
|
const { id: myId } = respData;
|
||
|
|
|
||
|
|
redirect(303, "/user/" + myId);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} satisfies Actions;
|
||
|
|
|