From 00cf0d9905a0d75d116d6e9656aa1a67cc600022 Mon Sep 17 00:00:00 2001 From: toqvist Date: Sat, 6 Apr 2024 17:43:57 +0200 Subject: [PATCH] Auth works but not with cookie --- astro/src/components/KiosMap.tsx | 6 ++++-- astro/src/utils/hooks.ts | 12 +++++++----- payload/src/collections/Users.ts | 5 +++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/astro/src/components/KiosMap.tsx b/astro/src/components/KiosMap.tsx index fedfedb..ac4c33c 100644 --- a/astro/src/components/KiosMap.tsx +++ b/astro/src/components/KiosMap.tsx @@ -131,9 +131,11 @@ export const KiosMap = () => { width={120} src="/kios-logo.png" alt="" /> - - {(myself && myself.name) && + + {(myself && myself.name) + ?

Logged in as: {myself.name}

+ :

Logged in

} { (!hasAuthCookie() && !authToken) && diff --git a/astro/src/utils/hooks.ts b/astro/src/utils/hooks.ts index 6194167..628de6c 100644 --- a/astro/src/utils/hooks.ts +++ b/astro/src/utils/hooks.ts @@ -9,10 +9,6 @@ const nonAuthHeaders = { "Content-Type": "application/json", } -const authHeaders = { - "Content-Type": "application/json", -} - const getMakers = async () => { const url = `${API_URL}/api/makers` console.log("Fetching url:", url) @@ -74,12 +70,18 @@ const getMyself = async (authToken: string) => { const url = `${API_URL}/api/users/me` console.log("Fetching url:", url) + const authHeaders = { + "Content-Type": "application/json", + "Authorization": `JWT ${authToken}`, + } + + const response = await axios.get(`${API_URL}/api/users/me`, { withCredentials: true, headers: authHeaders }); - const user: User = response.data.docs; + const user: User = response.data console.log(`Fetch result from ${url}`, user) return user; diff --git a/payload/src/collections/Users.ts b/payload/src/collections/Users.ts index c5da1f2..9362bff 100644 --- a/payload/src/collections/Users.ts +++ b/payload/src/collections/Users.ts @@ -13,6 +13,11 @@ const Users: CollectionConfig = { verify: false, // Require email verification before being allowed to authenticate maxLoginAttempts: 5, // Automatically lock a user out after X amount of failed logins lockTime: 600 * 1000, // Time period to allow the max login attempts + cookies: { + secure: true, + sameSite: "lax", + domain: process.env.ASTRO_HOST + }, }, fields: [ // Email added by default