Middleware & Auth
Chained middleware with validation and error handling
Test credentials
Email: admin@example.com
Password: password123
Try wrong credentials to see error handling.
Chain Break Behavior
When middleware does not call next(), the chain breaks — remaining middleware is skipped, but the handler still runs with whatever context was accumulated so far.
// mw2 does NOT call next()
const mw2Skip = defineMiddleware(async () => {
// chain breaks here — mw3 never runs
// handler still executes with ctx from mw1 only
})
middleware: [mw1, mw2Skip, mw3]
// Result: mw1 ✓, mw2 skipped, mw3 ✗, handler ✓How it works
// server/actions/login.post.ts
const authMiddleware = defineMiddleware(async ({ event, next }) => {
console.log(`[Auth] ${event.method} ${event.path}`)
return next({ ctx: { requestTime: Date.now() } })
})
export default defineAction({
input: z.object({
email: z.string().email(),
password: z.string().min(8),
}),
middleware: [authMiddleware, rateLimitInfoMiddleware],
handler: async ({ input, ctx }) => { ... },
})