Skip to content

Move Session route for Nuxt Auth Utils to /.nuxt-auth-utils/session to avoid polluting /api namespace #406

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ interface UserSessionComposable {
```

> [!IMPORTANT]
> Nuxt Auth Utils uses the `/api/_auth/session` route for session management. Ensure your API route middleware doesn't interfere with this path.
> Nuxt Auth Utils uses the `/session/_auth/session` route for session management. Ensure your API route middleware doesn't interfere with this path.

## Server Utils

Expand Down Expand Up @@ -556,7 +556,7 @@ We leverage hooks to let you extend the session data with your own data or log w
```ts
// server/plugins/session.ts
export default defineNitroPlugin(() => {
// Called when the session is fetched during SSR for the Vue composable (/api/_auth/session)
// Called when the session is fetched during SSR for the Vue composable (/session/_auth/session)
// Or when we call useUserSession().fetch()
sessionHooks.hook('fetch', async (session, event) => {
// extend User Session by calling your database
Expand Down
8 changes: 4 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ export default defineNuxtModule<ModuleOptions>({

addServerImportsDir(resolver.resolve('./runtime/server/utils'))
addServerHandler({
handler: resolver.resolve('./runtime/server/api/session.delete'),
route: '/api/_auth/session',
handler: resolver.resolve('./runtime/server/session/session.delete'),
route: '/session/_auth/session',
method: 'delete',
})
addServerHandler({
handler: resolver.resolve('./runtime/server/api/session.get'),
route: '/api/_auth/session',
handler: resolver.resolve('./runtime/server/session/session.get'),
route: '/session/_auth/session',
method: 'get',
})
// Set node:crypto as unenv external
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/app/composables/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function useUserSession(): UserSessionComposable {
const authReadyState = useState('nuxt-auth-ready', () => false)

const clear = async () => {
await useRequestFetch()('/api/_auth/session', {
await useRequestFetch()('/session/_auth/session', {
method: 'DELETE',
onResponse({ response: { headers } }) {
// Forward the Set-Cookie header to the main server event
Expand All @@ -27,7 +27,7 @@ export function useUserSession(): UserSessionComposable {
}

const fetch = async () => {
sessionState.value = await useRequestFetch()<UserSession>('/api/_auth/session', {
sessionState.value = await useRequestFetch()<UserSession>('/session/_auth/session', {
headers: {
accept: 'application/json',
},
Expand Down
2 changes: 1 addition & 1 deletion test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('ssr', async () => {

it('returns an empty session', async () => {
// Get response to a server-rendered page with `$fetch`.
const session = await $fetch('/api/_auth/session')
const session = await $fetch('/session/_auth/session')
// Session should be an object with an `id` property
expect(session).toBeInstanceOf(Object)
expect(session).toHaveProperty('id')
Expand Down