Simple HTTP Authentication middleware of koa
$ npm install koa-http-auth
const koa = require('koa')
const BasicAuth = require('koa-http-auth').Basic
const app = koa()
app.use(BasicAuth('Simple Application'))
app.use(function * (next) {
if (this.request.auth == null) { // No authorization provided
this.body = 'Please log in.'
return // Middleware will auto give 401 response
}
if (this.request.auth.user !== 'user' ||
this.request.auth.password('password')) {
this.body = 'Invalid user.'
delete this.request.auth // Delete request.auth ...
return // ... will make middleware give 401 response too.
}
if (this.url === '/logout') {
this.body = 'You are successfully logged out.'
delete this.request.auth // Delete request.auth unconditionally ...
return // ... will make user logged out.
}
this.body = 'Welcome back!'
yield next
})
const koa = require('koa')
const DigestAuth = require('koa-http-auth').Digest
const app = koa()
app.use(DigestAuth('Simple Application'))
app.use(function * (next) {
if (this.request.auth == null) { // No authorization provided
this.body = 'Please log in.'
return // Middleware will auto give 401 response
}
if (this.request.auth.user !== 'user' ||
this.request.auth.password('password')) {
this.body = 'Invalid user.'
delete this.request.auth // Delete request.auth ...
return // ... will make middleware give 401 response too.
}
if (this.url === '/logout') {
this.body = 'You are successfully logged out.'
delete this.request.auth // Delete request.auth unconditionally ...
return // ... will make user logged out.
}
this.body = 'Welcome back!'
yield next
})