koa-login

0.0.6 • Public • Published

koa-login

npm Travis Code Climate Code Climate npm npm

David David

Description

Use in conjunction with koa-auth-jwt.
This middleware can be used to provide a simple login endpoint in koa.
The jsonwebtoken package is used to create and sign JSON web tokens (JWT).
Your login form has to send name and password in the request body.

Installation

$ npm install koa-login

Usage

var login = require('koa-login');
var options = {
  secret: process.env.SECRET,
  userProvider: name => {
    /*
      The userProvider should find the user by name e.g. in the database.
      It should return a Promise which resolves to an object containing two properties:
      {
        hash: // hash value of the users password,
        user: // user object itself
      }
    */
    return // TODO
  },
  publicDataProvider: user => {
    return {
      name: user.name
    };
  },
  tokenDataProvider: user => {
    return {
      id: user.id
    };
  }
};
 
// endpoint expects 'name' and 'password' inside 'this.request.body'
router.post('/api/login', login(options));

Example

Click here for an example usage of this package.

Options

All options are mandatory!

  • secret string which will be used to sign the jwt
  • userProvider function which takes a name and returns a Promise. The Promise should return an object containing two properties: hash & user. Hash is the hashed value of the users password and user the user object itself.
  • publicDataProvider function which takes the user object and returns an object containing public, unencrypted data which will be sent back to the logged in user
  • tokenDataProvider function which takes the user object and returns an object containing data which will be encrypted inside the token

Test

$ npm install -g mocha  
$ mocha

Author

marc1404

License

MIT

Package Sidebar

Install

npm i koa-login

Weekly Downloads

2

Version

0.0.6

License

MIT

Last publish

Collaborators

  • marc1404