Fix Unauthorized error by ensuring user ID in JWT token is a string
This commit is contained in:
parent
df02a18d8c
commit
022846e706
2 changed files with 2 additions and 6 deletions
|
|
@ -51,11 +51,7 @@ export async function removeChat({ id, path }: { id: string; path: string }) {
|
||||||
|
|
||||||
const uid = await kv.hget<string>(`chat:${id}`, 'userId')
|
const uid = await kv.hget<string>(`chat:${id}`, 'userId')
|
||||||
|
|
||||||
// Convert both IDs to strings before comparing
|
if (uid !== session?.user?.id) {
|
||||||
// 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 {
|
return {
|
||||||
error: 'Unauthorized'
|
error: 'Unauthorized'
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
auth.ts
2
auth.ts
|
|
@ -18,7 +18,7 @@ export const {
|
||||||
callbacks: {
|
callbacks: {
|
||||||
jwt({ token, profile }) {
|
jwt({ token, profile }) {
|
||||||
if (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
|
token.image = profile.avatar_url || profile.picture
|
||||||
}
|
}
|
||||||
return token
|
return token
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue