boredgame/src/hooks.ts

25 lines
656 B
TypeScript
Raw Normal View History

2022-01-28 05:27:12 +00:00
import cookie from 'cookie';
import { v4 as uuid } from '@lukeed/uuid';
import type { Handle } from '@sveltejs/kit';
export const handle: Handle = async ({ event, resolve }) => {
const cookies = cookie.parse(event.request.headers.get('cookie') || '');
event.locals.userid = cookies.userid || uuid();
const response = await resolve(event);
if (!cookies.userid) {
// if this is the first time the user has visited this app,
2022-04-19 06:33:45 +00:00
// set a cookie so that we recognize them when they return
2022-01-28 05:27:12 +00:00
response.headers.set(
'set-cookie',
cookie.serialize('userid', event.locals.userid, {
path: '/',
httpOnly: true
})
);
}
return response;
};