-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
39 lines (35 loc) · 1.35 KB
/
Copy pathauth.js
File metadata and controls
39 lines (35 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Defining the authentication key, this has to be the same key used in the JWTStrategy
const jwtSecret = 'your_jwt_secret';
// Importing JSON Web Token and passport
const jwt = require('jsonwebtoken'),
passport = require('passport');
// Importing the local passport file
require('./passport');
// Generating the JWT token based on the username and password
let generateJWTToken = (user) => {
return jwt.sign(user, jwtSecret, {
subject: user.Username, // The username that's encoding in the JWT
expiresIn: '7d', // Specifing the token to expire in 7 days
algorithm: 'HS256' // The algorithm is used to sign or encode the values of the JWT
});
}
/* POST login. */
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}); // It returns the token
});
})(req, res);
});
}