-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
47 lines (43 loc) · 1.41 KB
/
auth.js
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
40
41
42
43
44
45
46
47
// This has to be the same key used in the JWTStrategy
const jwtSecret = 'your_jwt_secret';
const jwt = require('jsonwebtoken');
const passport = require('passport');
require('./passport'); // Your local passport file
/**
* Generates a JWT token for a user.
* @param {Object} user - The user object for whom the token is being generated.
* @returns {string} JWT token.
*/
let generateJWTToken = (user) => {
return jwt.sign(user, jwtSecret, {
subject: user.Name, // The username encoded in the JWT
expiresIn: '7d', // Token expiration time
algorithm: 'HS256' // Algorithm used to encode the JWT
});
};
/* POST login route */
module.exports = (router) => {
/**
* @route POST /login
* @description Authenticates a user and returns a JWT token upon successful authentication.
* @param {Object} req - The request object containing user credentials.
* @param {Object} res - The response object.
*/
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);
});
};