Updated SvelteKit example. (#7301)

This commit is contained in:
Stephanie Dietz
2022-01-17 18:17:03 -06:00
committed by GitHub
parent fc420c9e21
commit bd88a55580
25 changed files with 2085 additions and 395 deletions

View File

@@ -0,0 +1,20 @@
import cookie from 'cookie';
import { v4 as uuid } from '@lukeed/uuid';
export const handle = async ({ request, resolve }) => {
const cookies = cookie.parse(request.headers.cookie || '');
request.locals.userid = cookies.userid || uuid();
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;
};