From dc55ef6b47cb942dd3b485b60d640067463f6aaf Mon Sep 17 00:00:00 2001 From: Kasper Peulen Date: Thu, 22 Aug 2024 14:09:32 +0200 Subject: [PATCH] Make the demo actually secure --- app/actions.ts | 8 ++--- app/note/edit/[id]/page.tsx | 2 +- components/auth-button.stories.tsx | 3 +- components/auth-button.tsx | 18 +++++------ components/logout-button.stories.tsx | 2 +- components/logout-button.tsx | 6 ++-- components/note-ui.stories.tsx | 17 +++------- components/note-ui.tsx | 28 ++++++----------- lib/session.ts | 47 ++++++++++++++++------------ package.json | 1 + tsconfig.json | 2 +- 11 files changed, 61 insertions(+), 73 deletions(-) diff --git a/app/actions.ts b/app/actions.ts index 8c9ec82..f5751af 100644 --- a/app/actions.ts +++ b/app/actions.ts @@ -6,12 +6,8 @@ import { cookies } from 'next/headers' import { redirect } from 'next/navigation' import { getUserFromSession } from '#lib/session' -export async function saveNote( - noteId: number | undefined, - title: string, - body: string, -) { - const user = getUserFromSession() +export async function saveNote(noteId: number | undefined, title: string, body: string) { + const user = await getUserFromSession() if (!user) { redirect('/') diff --git a/app/note/edit/[id]/page.tsx b/app/note/edit/[id]/page.tsx index ac9f01d..e8add20 100644 --- a/app/note/edit/[id]/page.tsx +++ b/app/note/edit/[id]/page.tsx @@ -13,7 +13,7 @@ type Props = { } export default async function EditPage({ params }: Props) { - const user = getUserFromSession() + const user = await getUserFromSession() const note = await db.note.findUnique({ where: { diff --git a/components/auth-button.stories.tsx b/components/auth-button.stories.tsx index dc34a31..a8926a1 100644 --- a/components/auth-button.stories.tsx +++ b/components/auth-button.stories.tsx @@ -7,6 +7,7 @@ const meta = { args: { noteId: null, }, + parameters: { react: { rsc: true } }, } satisfies Meta export default meta @@ -15,7 +16,7 @@ type Story = StoryObj export const LoggedIn: Story = { beforeEach: () => { - getUserFromSession.mockReturnValue('storybookjs') + getUserFromSession.mockResolvedValue('storybookjs') }, args: { children: 'Add' }, } diff --git a/components/auth-button.tsx b/components/auth-button.tsx index 472dab4..ca87b36 100644 --- a/components/auth-button.tsx +++ b/components/auth-button.tsx @@ -9,8 +9,8 @@ type Props = { noteId: number | null } -export default function AuthButton({ children, noteId }: Props) { - const user = getUserFromSession() +export default async function AuthButton({ children, noteId }: Props) { + const user = await getUserFromSession() const isDraft = noteId == null if (user) { @@ -18,10 +18,9 @@ export default function AuthButton({ children, noteId }: Props) { // Use hard link