coyotte508 HF staff commited on
Commit
067b52a
1 Parent(s): b37570d

✨ Add cookie (#17)

Browse files
package-lock.json CHANGED
@@ -10,6 +10,7 @@
10
  "dependencies": {
11
  "@huggingface/inference": "^2.0.0-rc2",
12
  "autoprefixer": "^10.4.14",
 
13
  "mongodb": "^5.3.0",
14
  "postcss": "^8.4.21",
15
  "tailwind-scrollbar": "^3.0.0",
@@ -1428,6 +1429,18 @@
1428
  "node": ">=4"
1429
  }
1430
  },
 
 
 
 
 
 
 
 
 
 
 
 
1431
  "node_modules/debug": {
1432
  "version": "4.3.4",
1433
  "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
 
10
  "dependencies": {
11
  "@huggingface/inference": "^2.0.0-rc2",
12
  "autoprefixer": "^10.4.14",
13
+ "date-fns": "^2.29.3",
14
  "mongodb": "^5.3.0",
15
  "postcss": "^8.4.21",
16
  "tailwind-scrollbar": "^3.0.0",
 
1429
  "node": ">=4"
1430
  }
1431
  },
1432
+ "node_modules/date-fns": {
1433
+ "version": "2.29.3",
1434
+ "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz",
1435
+ "integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==",
1436
+ "engines": {
1437
+ "node": ">=0.11"
1438
+ },
1439
+ "funding": {
1440
+ "type": "opencollective",
1441
+ "url": "https://opencollective.com/date-fns"
1442
+ }
1443
+ },
1444
  "node_modules/debug": {
1445
  "version": "4.3.4",
1446
  "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
package.json CHANGED
@@ -31,6 +31,7 @@
31
  "dependencies": {
32
  "@huggingface/inference": "^2.0.0-rc2",
33
  "autoprefixer": "^10.4.14",
 
34
  "mongodb": "^5.3.0",
35
  "postcss": "^8.4.21",
36
  "tailwind-scrollbar": "^3.0.0",
 
31
  "dependencies": {
32
  "@huggingface/inference": "^2.0.0-rc2",
33
  "autoprefixer": "^10.4.14",
34
+ "date-fns": "^2.29.3",
35
  "mongodb": "^5.3.0",
36
  "postcss": "^8.4.21",
37
  "tailwind-scrollbar": "^3.0.0",
src/app.d.ts CHANGED
@@ -3,7 +3,9 @@
3
  declare global {
4
  namespace App {
5
  // interface Error {}
6
- // interface Locals {}
 
 
7
  // interface PageData {}
8
  // interface Platform {}
9
  }
 
3
  declare global {
4
  namespace App {
5
  // interface Error {}
6
+ interface Locals {
7
+ sessionId: string;
8
+ }
9
  // interface PageData {}
10
  // interface Platform {}
11
  }
src/hooks.server.ts ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { Handle } from '@sveltejs/kit';
2
+ import { addYears } from 'date-fns';
3
+
4
+ export const handle: Handle = async ({ event, resolve }) => {
5
+ const token = event.cookies.get('session');
6
+
7
+ event.locals.sessionId = token || crypto.randomUUID();
8
+
9
+ // Refresh cookie expiration date
10
+ event.cookies.set('session', event.locals.sessionId, {
11
+ path: '/',
12
+ sameSite: 'lax',
13
+ secure: true,
14
+ httpOnly: true,
15
+ expires: addYears(new Date(), 1)
16
+ });
17
+
18
+ const response = await resolve(event);
19
+
20
+ return response;
21
+ };
src/routes/+page.server.ts CHANGED
@@ -2,12 +2,14 @@ import type { PageServerLoad } from './$types';
2
  import { collections } from '$lib/server/database';
3
  import type { Conversation } from '$lib/types/Conversation';
4
 
5
- export const load: PageServerLoad = async () => {
6
  const { conversations } = collections;
7
 
8
  return {
9
  conversations: await conversations
10
- .find()
 
 
11
  .sort({ updatedAt: -1 })
12
  .project<Pick<Conversation, 'title' | '_id' | 'updatedAt' | 'createdAt'>>({
13
  title: 1,
 
2
  import { collections } from '$lib/server/database';
3
  import type { Conversation } from '$lib/types/Conversation';
4
 
5
+ export const load: PageServerLoad = async (event) => {
6
  const { conversations } = collections;
7
 
8
  return {
9
  conversations: await conversations
10
+ .find({
11
+ sessionId: event.locals.sessionId
12
+ })
13
  .sort({ updatedAt: -1 })
14
  .project<Pick<Conversation, 'title' | '_id' | 'updatedAt' | 'createdAt'>>({
15
  title: 1,