Skip to content

Commit

Permalink
✨ Use a session to store key id
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernardstanislas committed Apr 1, 2023
1 parent df896ea commit 4c90c18
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
20 changes: 17 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@dicebear/collection": "^6.0.2",
"@dicebear/core": "^6.0.2",
"lodash": "^4.17.21",
"mongodb": "^5.1.0"
"mongodb": "^5.1.0",
"svelte-kit-cookie-session": "^3.4.1"
}
}
14 changes: 14 additions & 0 deletions src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces

import type { KeyId } from '$lib/crypto';

interface SessionData {
keyId?: KeyId;
}

declare global {
namespace App {
interface Locals {
session: import('svelte-kit-cookie-session').Session<SessionData>;
}

interface PageData {
session: SessionData;
}
// interface Error {}
// interface Locals {}
// interface PageData {}
Expand Down
5 changes: 5 additions & 0 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { handleSession } from 'svelte-kit-cookie-session';

export const handle = handleSession({
secret: 'SOME_COMPLEX_SECRET_AT_LEAST_32_CHARS'
});
16 changes: 14 additions & 2 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,28 @@ import client from '$lib/server/db';
import type { KeyId } from '$lib/crypto';

export const actions = {
default: async ({ request }) => {
default: async ({ request, locals }) => {
const formData = await request.formData();
const keyId = formData.get('key_id');
const keyId = formData.get('key_id') as KeyId | undefined;
const content = formData.get('encrypted_journal');
if (!keyId) {
throw new Error("No 'key_id' field in form data");
}
if (!content) {
throw new Error("No 'encrypted_journal' field in form data");
}
await locals.session.set({ keyId });
await client.store(keyId as KeyId, content.toString());
}
} satisfies Actions;

export const load = async ({ locals }) => {
const keyId = locals.session.data.keyId;
if (!keyId) {
return {};
}
const encryptedJournal = await client.get(keyId);
return {
encryptedJournal
};
};
8 changes: 7 additions & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
import { browser } from '$app/environment';
import keyPair from '$lib/stores/key-pair';
import { arrayBufferToBase64 } from '$lib/array-buffer';
import { journal, encryptedJournal } from '$lib/stores/journal';
import { journal, encryptedJournal, initJournalFromEncryptedContent } from '$lib/stores/journal';
import KeyPair from '../lib/key-pair.svelte';
import type { PageData } from './$types';
export let data: PageData;
$: if ($keyPair && data.encryptedJournal) {
initJournalFromEncryptedContent(data.encryptedJournal, $keyPair.privateKey);
}
$: base64EncodedJournal = browser
? arrayBufferToBase64($encryptedJournal.value || new ArrayBuffer(0))
Expand Down

1 comment on commit 4c90c18

@vercel
Copy link

@vercel vercel bot commented on 4c90c18 Apr 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.