From 022846e7063d69c9639f4f3f545bcf2e8cac3dec Mon Sep 17 00:00:00 2001 From: admineral <50579369+admineral@users.noreply.github.com> Date: Mon, 27 Nov 2023 20:50:38 +0100 Subject: [PATCH] Fix Unauthorized error by ensuring user ID in JWT token is a string --- app/actions.ts | 6 +----- auth.ts | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/app/actions.ts b/app/actions.ts index 5bfc41f..2c8a5dd 100644 --- a/app/actions.ts +++ b/app/actions.ts @@ -51,11 +51,7 @@ export async function removeChat({ id, path }: { id: string; path: string }) { const uid = await kv.hget(`chat:${id}`, 'userId') - // Convert both IDs to strings before comparing - // This is necessary because the session user ID is a string, while the chat user ID is a number - // The strict inequality check (!==) in JavaScript checks both value and type, so it would fail if the types are different - // By converting both IDs to strings, we ensure that the comparison is done between two strings, and it will work correctly even if the IDs are originally of different types - if (String(uid) !== String(session?.user?.id)) { + if (uid !== session?.user?.id) { return { error: 'Unauthorized' } diff --git a/auth.ts b/auth.ts index 7c0c6a0..5e1cf7b 100644 --- a/auth.ts +++ b/auth.ts @@ -18,7 +18,7 @@ export const { callbacks: { jwt({ token, profile }) { if (profile) { - token.id = profile.id + token.id = String(profile.id); // Convert profile.id to a string token.image = profile.avatar_url || profile.picture } return token