Last active
November 22, 2016 20:17
-
-
Save captDaylight/b913796bb6cdc5d5c530028613c27aee to your computer and use it in GitHub Desktop.
Generating JWT for my current project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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