summaryrefslogtreecommitdiff
path: root/client/src/routes/login/+page.server.ts
blob: 738b8ad0184bbe9df560ebaf6d65c9059a4b8d3f (plain)
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
import { redirect } from '@sveltejs/kit';
import { base } from '$app/paths';
import type { Actions } from './$types';
import { client } from '$lib/fetch-client';

export const actions = {
  default: async ({ request, fetch }) => {
    const data = await request.formData();
    const username = data.get('username');
    const password = data.get('password');
    const ret = data.get('return');

    const login = await client.POST('/login', {
      body: {
        username: username?.toString() || '',
        password: password?.toString() || ''
      },
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      fetch
    });
    if (login.data?.ok === true) {
      if (ret) {
        redirect(303, ret.toString());
      } else {
        redirect(303, base);
      }
    } else {
      return {
        error: true,
        username: username
      };
    }
  }
} satisfies Actions;