sveltekit-demo / src /hooks.ts
julien-c's picture
julien-c HF staff
npm init svelte@next (w/o the README)
9d177b4 verified
raw
history blame
805 Bytes
import cookie from 'cookie';
import { v4 as uuid } from '@lukeed/uuid';
import type { Handle } from '@sveltejs/kit';
export const handle: Handle = async ({ request, resolve }) => {
const cookies = cookie.parse(request.headers.cookie || '');
request.locals.userid = cookies.userid || uuid();
// TODO https://github.com/sveltejs/kit/issues/1046
const method = request.url.searchParams.get('_method');
if (method) {
request.method = method.toUpperCase();
}
const response = await resolve(request);
if (!cookies.userid) {
// if this is the first time the user has visited this app,
// set a cookie so that we recognise them when they return
response.headers['set-cookie'] = cookie.serialize('userid', request.locals.userid, {
path: '/',
httpOnly: true
});
}
return response;
};