From df02a18d8c45a88b0a35aa5b6d0ffef34131a5a7 Mon Sep 17 00:00:00 2001 From: admineral <50579369+admineral@users.noreply.github.com> Date: Mon, 27 Nov 2023 19:31:17 +0100 Subject: [PATCH 1/4] Fix type mismatch in removeChat function --- app/actions.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/actions.ts b/app/actions.ts index 2c8a5dd..5bfc41f 100644 --- a/app/actions.ts +++ b/app/actions.ts @@ -51,7 +51,11 @@ export async function removeChat({ id, path }: { id: string; path: string }) { const uid = await kv.hget(`chat:${id}`, 'userId') - if (uid !== session?.user?.id) { + // 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)) { return { error: 'Unauthorized' } 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 2/4] 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 From 39d971ecb635aada59456e50fd5403b267c0311e Mon Sep 17 00:00:00 2001 From: admineral <50579369+admineral@users.noreply.github.com> Date: Mon, 27 Nov 2023 20:55:14 +0100 Subject: [PATCH 3/4] Fix Unauthorized error by ensuring user ID in JWT token is a string --- auth.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auth.ts b/auth.ts index 5e1cf7b..a9f8abb 100644 --- a/auth.ts +++ b/auth.ts @@ -25,7 +25,7 @@ export const { }, session: ({ session, token }) => { if (session?.user && token?.id) { - session.user.id = String(token.id) + session.user.id = String(token.id); // Convert token.id to a string } return session }, From 18576f43b9959a224dd7965556628357b19f350e Mon Sep 17 00:00:00 2001 From: admineral <50579369+admineral@users.noreply.github.com> Date: Mon, 27 Nov 2023 22:14:06 +0100 Subject: [PATCH 4/4] Convert uid to string for consistent comparison with session.user.id --- app/actions.ts | 3 ++- auth.ts | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/actions.ts b/app/actions.ts index 2c8a5dd..7fe08a7 100644 --- a/app/actions.ts +++ b/app/actions.ts @@ -49,7 +49,8 @@ export async function removeChat({ id, path }: { id: string; path: string }) { } } - const uid = await kv.hget(`chat:${id}`, 'userId') + //Convert uid to string for consistent comparison with session.user.id + const uid = String(await kv.hget(`chat:${id}`, 'userId')) if (uid !== session?.user?.id) { return { diff --git a/auth.ts b/auth.ts index a9f8abb..7c0c6a0 100644 --- a/auth.ts +++ b/auth.ts @@ -18,14 +18,14 @@ export const { callbacks: { jwt({ token, profile }) { if (profile) { - token.id = String(profile.id); // Convert profile.id to a string + token.id = profile.id token.image = profile.avatar_url || profile.picture } return token }, session: ({ session, token }) => { if (session?.user && token?.id) { - session.user.id = String(token.id); // Convert token.id to a string + session.user.id = String(token.id) } return session },