-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
94 lines (89 loc) · 2.65 KB
/
auth.js
File metadata and controls
94 lines (89 loc) · 2.65 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* @module Authentication
* @description JWT authentication module for user login
* @requires jsonwebtoken
* @requires passport
*/
const jwtSecret = "your_jwt_secret";
const jwt = require("jsonwebtoken"),
passport = require("passport");
require("./passport.js");
/**
* Generates JWT token for authenticated user
* @function generateJWTToken
* @param {Object} user - User document from MongoDB
* @returns {string} JWT token
* @description Creates a signed JWT token containing user data
* @example
* // Returns JWT token string
* generateJWTToken({
* Username: "testuser",
* _id: "123456"
* })
*/
let generateJWTToken = (user) => {
return jwt.sign(user.toJSON(), jwtSecret, {
subject: user.Username, // This is the username encoded 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
});
};
/**
* Login endpoint
* @route POST /login
* @param {Object} req.body.Username - Username
* @param {Object} req.body.Password - Password
* @returns {Object} User object with JWT token
* @throws {Error} 400 - Username and password required
* @throws {Error} 401 - Authentication failed
* @example
* // Request
* {
* "Username": "testuser",
* "Password": "password123"
* }
*
* // Success Response: 200 OK
* {
* "user": {
* "Username": "testuser",
* "_id": "123456"
* },
* "token": "JWT_TOKEN_STRING"
* }
*
* // Error Response: 400 Bad Request
* {
* "message": "Username and password are required"
* }
*/
module.exports = (router) => {
router.post("/api/login", (req, res, next) => {
// Validation request body
if (!req.body.Username || !req.body.Password) {
return res.status(400).json({
message: "Username and password are required"
});
}
passport.authenticate("local", { session: false }, (error, user, info) => {
if (error || !user) {
return res.status(401).json("Failed to login " + info.message);
}
req.login(user, { session: false }, (error) => {
if (error) {
console.log("Login error");
return res.status(500).json({
message: "Login error",
error: error.message
});
}
let token = generateJWTToken(user);
return res.json({
user: user,
token: token
});
});
})(req, res, next);
});
return router;
};