Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always return 200 for session endpoint #130

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Comment on lines +8 to +10
Copy link

@katlyn katlyn Sep 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for requiring that user being defined on the session is required before running the fetch hook? This seems to be a breaking change, compared to the previous implementation. For my use case i would like the user property to be populated dynamically by a session hook based on other information in the session, but this change prevents that use case.


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({})
})
})