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.
$ npm install koa-login
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));
Click here for an example usage of this package.
All options are mandatory!
secret
string which will be used to sign the jwtuserProvider
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 usertokenDataProvider
function which takes the user object and returns an object containing data which will be encrypted inside the token
$ npm install -g mocha
$ mocha