Skip to content

Commit

Permalink
fix: always return 200 for session endpoint (#130)
Browse files Browse the repository at this point in the history
* fix: always return 200 for session endpoint

* chore: add test
  • Loading branch information
atinux committed Jul 30, 2024
1 parent a4cfa89 commit 26f4aa0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/runtime/server/api/session.get.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { eventHandler } from 'h3'
import { requireUserSession, sessionHooks } from '../utils/session'
import { getUserSession, sessionHooks } from '../utils/session'
import type { UserSessionRequired } from '#auth-utils'

export default eventHandler(async (event) => {
const session = await requireUserSession(event)
const session = await getUserSession(event)

await sessionHooks.callHookParallel('fetch', session, event)
if (session.user) {
await sessionHooks.callHookParallel('fetch', session as UserSessionRequired, event)
}

return session
})
6 changes: 6 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ describe('ssr', async () => {
const html = await $fetch('/')
expect(html).toContain('<div>Nuxt Auth Utils</div>')
})

it('returns an empty session', async () => {
// Get response to a server-rendered page with `$fetch`.
const session = await $fetch('/api/_auth/session')
expect(session).toStrictEqual({})
})
})

0 comments on commit 26f4aa0

Please sign in to comment.