cupcoffee-auth-jwt

JWT authentication to CupcoffeeJs


Keywords
cupcoffee, jwt, token, auth
License
ISC
Install
npm install cupcoffee-auth-jwt@0.1.3

Documentation

cupcoffee-auth-jwt

JWT authentication to CupcoffeeJs

Install

Create environment variable: NODE_CUPCOFFEE_JWT_SECRET or NODE_JWT_SECRET with secret code to JWT. Example: NODE_CUPCOFFEE_JWT_SECRET=MY.SCRET.CODE.TO.JWT

Or manually add to require('cupcoffee-auth-jwt')('MY.SCRET.CODE.TO.JWT')

Middware

middware/index.js

var cupJwt = require('cupcoffee-auth-jwt')()

module.exports = () => {

    this.express = () => {
        return cupJwt.middware('/api/*', ({req, res, next}) => {
           //Success callback
            return next()
        })
    }

    return this;

}

Routes

routes/index.js

var cupJwt = require('cupcoffee-auth-jwt')()

module.exports = ({router, controller}) => {

    router.post('/login', cupJwt.validate((user, pass, next) => {
        //.. User authentication, return false or data user
        var result = { email: "user@email.com"}
        next(result)
    }).login);

    router.get('/api/*', (req, res) => {
        controller.http(req, res).find();
    })

    return router;
};

Token

In the HTML header to send x-access-token with the token and x-key with the username;