How to use 'svelte-kit-cookie-session' package?

91 Views Asked by At

According to , I wrote the code as shown below.

  1. src/hooks.ts
import { handleSession } from 'svelte-kit-cookie-session';

export const handle = handleSession({
    secret: 'SOME_COMPLEX_SECRET_AT_LEAST_32_CHARS'
});

  1. src/routes/login/page.server.ts
import { type Actions, fail, redirect } from '@sveltejs/kit';
import { Logger } from 'tslog';
import * as api from '../../lib/api';
const logger = new Logger({ name: 'login' });

export const load = async ({}) => {
    logger.debug(`load START`);
    // ...
    logger.debug(`load END`);
};

export const actions: Actions = {
    login: async ({ request, locals }) => {
        const data = await request.formData();
        logger.debug(`actions login START`);
        const body = await api.post(
            'auth/login',
            {
                id: data.get('id'),
                password: data.get('password')
            },
            ''
        );
        if (body.errors) {
            return fail(body.error, body);
        }
        console.log(body.accessToken);
        const { counter = 0 } = locals.session.data;
        await locals.session.set({ counter: counter + 1 });

        throw redirect(307, '/'); // main으로 redirect
    }
} satisfies Actions;

This is not the complete login logic. I want to make sure the application works. However the following error pops out, whenever I try to run the application. TypeError: Cannot read properties of undefined (reading 'data'). I know what the error means, but how can I fix it? I'm just following the official docs.

I researched a lot and tried many things, but nothing worked.

0

There are 0 best solutions below