Skip to content
This repository has been archived by the owner on Dec 25, 2019. It is now read-only.

Gerhut/koa-http-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa-http-auth

Build Status codecov.io Dependency Status devDependency Status js-standard-style

Simple HTTP Authentication middleware of koa

Install

$ npm install koa-http-auth

Usage

Basic Authentication

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
})

Digest Authentication

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
})

About

HTTP authorization middleware of koa.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published