Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Ensure middleware order is always correct #45

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions playground/server/middleware/logSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This middleware should already be able to access the sesssion, as the module session middleware should run BEFORE all other middlewares
console.log('registering 2')

export default eventHandler((event) => {
console.log('this is the current session: ', event.context.session)
})
6 changes: 3 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ export default defineNuxtModule<ModuleOptions>({
// 3. Locate runtime directory and transpile module
const { resolve } = createResolver(import.meta.url)

// 4. Setup middleware, use `.unshift` to ensure (reasonably well) that the session middleware is first
const handler = resolve('./runtime/server/middleware/session')
// 4. Setup middleware
const handler = resolve('./runtime/server/middleware/0.session')
const serverHandler = {
middleware: true,
handler
}
nuxt.options.serverHandlers.unshift(serverHandler)
addServerHandler(serverHandler)

// 5. Register desired session API endpoints
if (options.api.isEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import { processSessionIp, getHashedIpAddress } from './ipPinning'
import { SessionExpired } from './exceptions'
import { useRuntimeConfig } from '#imports'

console.log('registering 1')

declare module 'h3' {
interface H3EventContext {
session: Session
}
}

const SESSION_COOKIE_NAME = 'sessionId'
const safeSetCookie = (event: H3Event, name: string, value: string) => setCookie(event, name, value, {
// Max age of cookie in seconds
Expand Down