Skip to content

Commit 4c90c18

Browse files
✨ Use a session to store key id
1 parent df896ea commit 4c90c18

File tree

6 files changed

+59
-7
lines changed

6 files changed

+59
-7
lines changed

package-lock.json

Lines changed: 17 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@dicebear/collection": "^6.0.2",
3939
"@dicebear/core": "^6.0.2",
4040
"lodash": "^4.17.21",
41-
"mongodb": "^5.1.0"
41+
"mongodb": "^5.1.0",
42+
"svelte-kit-cookie-session": "^3.4.1"
4243
}
4344
}

src/app.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
// See https://kit.svelte.dev/docs/types#app
22
// for information about these interfaces
3+
4+
import type { KeyId } from '$lib/crypto';
5+
6+
interface SessionData {
7+
keyId?: KeyId;
8+
}
9+
310
declare global {
411
namespace App {
12+
interface Locals {
13+
session: import('svelte-kit-cookie-session').Session<SessionData>;
14+
}
15+
16+
interface PageData {
17+
session: SessionData;
18+
}
519
// interface Error {}
620
// interface Locals {}
721
// interface PageData {}

src/hooks.server.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { handleSession } from 'svelte-kit-cookie-session';
2+
3+
export const handle = handleSession({
4+
secret: 'SOME_COMPLEX_SECRET_AT_LEAST_32_CHARS'
5+
});

src/routes/+page.server.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,28 @@ import client from '$lib/server/db';
33
import type { KeyId } from '$lib/crypto';
44

55
export const actions = {
6-
default: async ({ request }) => {
6+
default: async ({ request, locals }) => {
77
const formData = await request.formData();
8-
const keyId = formData.get('key_id');
8+
const keyId = formData.get('key_id') as KeyId | undefined;
99
const content = formData.get('encrypted_journal');
1010
if (!keyId) {
1111
throw new Error("No 'key_id' field in form data");
1212
}
1313
if (!content) {
1414
throw new Error("No 'encrypted_journal' field in form data");
1515
}
16+
await locals.session.set({ keyId });
1617
await client.store(keyId as KeyId, content.toString());
1718
}
1819
} satisfies Actions;
20+
21+
export const load = async ({ locals }) => {
22+
const keyId = locals.session.data.keyId;
23+
if (!keyId) {
24+
return {};
25+
}
26+
const encryptedJournal = await client.get(keyId);
27+
return {
28+
encryptedJournal
29+
};
30+
};

src/routes/+page.svelte

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
import { browser } from '$app/environment';
33
import keyPair from '$lib/stores/key-pair';
44
import { arrayBufferToBase64 } from '$lib/array-buffer';
5-
import { journal, encryptedJournal } from '$lib/stores/journal';
5+
import { journal, encryptedJournal, initJournalFromEncryptedContent } from '$lib/stores/journal';
66
import KeyPair from '../lib/key-pair.svelte';
7+
import type { PageData } from './$types';
8+
9+
export let data: PageData;
10+
$: if ($keyPair && data.encryptedJournal) {
11+
initJournalFromEncryptedContent(data.encryptedJournal, $keyPair.privateKey);
12+
}
713
814
$: base64EncodedJournal = browser
915
? arrayBufferToBase64($encryptedJournal.value || new ArrayBuffer(0))

0 commit comments

Comments
 (0)