Skip to content

Commit

Permalink
♻️ do not rely on cookies server-side, only work with the bearer header
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Aug 13, 2024
1 parent 870865f commit cff7e6f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/composables/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const useApi = () => {
}

const fetch = $fetch.create({
headers: { Accept: 'application/json', Authentication: `Bearer: ${useCookie('token', cookieOptions).value}` },
onResponse: ({ response }) => {
if (silent.value) {
silent.value = false
Expand Down
6 changes: 3 additions & 3 deletions server/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export default defineEventHandler(async (event) => {
'/api/token',
]

const cookies = parseCookies(event)
if (cookies.token && auth.verify(useRuntimeConfig(event), cookies.token) === true)
await auth.set(cookies.token)
const bearer = (event.node.req.headers.authentication as string)?.split(' ')[1] || undefined
if (bearer && auth.verify(useRuntimeConfig(event), bearer) === true)
await auth.set(bearer)
if (gatedRoutes.some(route => getRequestURL(event).pathname.startsWith(route)))
if (!auth.user())
return metapi().error(event, 'Not Authenticated', 401)
Expand Down

0 comments on commit cff7e6f

Please sign in to comment.