mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-07 04:22:04 +00:00
Replaces https://github.com/vercel/vercel/pull/7674 with the latest SvelteKit boilerplate.
24 lines
661 B
JavaScript
24 lines
661 B
JavaScript
import * as cookie from 'cookie';
|
|
|
|
/** @type {import('@sveltejs/kit').Handle} */
|
|
export const handle = async ({ event, resolve }) => {
|
|
const cookies = cookie.parse(event.request.headers.get('cookie') || '');
|
|
event.locals.userid = cookies['userid'] || crypto.randomUUID();
|
|
|
|
const response = await resolve(event);
|
|
|
|
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(
|
|
'set-cookie',
|
|
cookie.serialize('userid', event.locals.userid, {
|
|
path: '/',
|
|
httpOnly: true
|
|
})
|
|
);
|
|
}
|
|
|
|
return response;
|
|
};
|