const jwtSecret = 'your_jwt_secret'; // This has to be the same key used in the JWTStrategy
const jwt = require('jsonwebtoken'),
passport = require('passport');
require('./passport'); // Your local passport file
/**
* Generates a JWT token
* @name generateToken
* @param Generates a JWT token
* @kind function
*/
let generateJWTToken = (user) => {
return jwt.sign(user, jwtSecret, {
subject: user.Username, // This is the username you’re encoding in the JWT
expiresIn: '7d', // This specifies that the token will expire in 7 days
algorithm: 'HS256' // This is the algorithm used to “sign” or encode the values of the JWT
});
}
/**
* API call to login a user with Passport Authentication and generate a token
* @param {string} /users/:id/
* @param {object} Username: string
* @param {object} Password: number
* @name loginUserAndToken
* @kind function
*/
module.exports = (router) => {
router.post('/login', (req, res) => {
passport.authenticate('local', { session: false }, (error, user, info) => {
if (error || !user) {
return res.status(400).json({
message: 'Something is not right',
user: user
});
}
req.login(user, { session: false }, (error) => {
if (error) {
res.send(error);
}
let token = generateJWTToken(user.toJSON());
return res.json({ user, token });
});
})(req, res);
});
}