Skip to content

Instantly share code, notes, and snippets.

@captDaylight captDaylight/auth.js
Last active Nov 22, 2016

Embed
What would you like to do?
Generating JWT for my current project
const jwt = require('jsonwebtoken');
const models = require('../models');
const Strategy = require('passport-local');
function generateToken(req, res, next) {
res.cookie('token',
jwt.sign({ id: req.user.id }, process.env.SECRET),
{
maxAge: 86400000,
httpOnly: true,
secure: true
});
next();
}
function respond(req, res) {
res.status(200).json({ status: true, data: { user: req.user.dataValues } });
}
module.exports = function(router, passport){
require('./login')(passport);
router.post('/auth/', passport.authenticate(
'login', { session: false }), generateToken, respond);
router.get('/logout/', function(req, res) {
res.clearCookie('token');
res.status(200).json({
status: true
});
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.