Skip to content

Latest commit

 

History

History
54 lines (42 loc) · 914 Bytes

readme.md

File metadata and controls

54 lines (42 loc) · 914 Bytes

Simple user module of koa

Installation

npm install koa-user

Example

var user = require('koa-user')
var koa = require('koa')
var app = koa()

function check(username, passwd) {
  // username & passwd are given from koa-user
  // check, always by db search
  return function(fn) {
    db.query('SELECT * from users where username=' + username, function(err, result) {
      if (!err && result.passwd === passwd) {
        fn(null, result)
        // the second arg expect to be the user object
        // just like {id: 100, name: 'myname', ...}
        // user will be bind on `this`
      } else {
        fn(err)
      }
    })
  }
}

app.use(user(check))

app.use(function* () {
  this.body = this.user // this.user is the result you given from check handler
})

app.listen(3000)

Todo

  • add setting cookie
  • add options

License

MIT