Basic Auth middleware for h3 / nitro / ... (based on ezpass)
import { createApp, defineEventHandler, toNodeListener } from 'h3'
import { listen } from 'listhen'
import { createBasicAuthMiddleware } from 'h3-basic-auth'
const app = createApp()
app.use(createBasicAuthMiddleware({
sessionSecret: 'secret', // Set this to something useful
username: 'test', // The valid username
password: 'test' // The valid password
}))
app.use(defineEventHandler(event => `Welcome ${event.context.auth?.session.user}!`))
listen(toNodeListener(app))
// server/middleware/basic-auth.ts
export default createBasicAuthMiddleware({
sessionSecret: 'secret', // Set this to something useful
username: 'test', // The valid username
password: 'test' // The valid password
})
sessionSecret
bypass
onAuthorize
username
password
app.use(createBasicAuthMiddleware({
provider: 'basic',
username: 'test',
password: 'test'
}))
- Clone Repository
- Install dependencies with
pnpm install
- Use
pnpm dev test/basic
to start basic example
MIT