-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.js
37 lines (31 loc) · 781 Bytes
/
utils.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
// generate token using secret from process.env.JWT_SECRET
var jwt = require('jsonwebtoken');
// generate token and return it
function generateToken(user) {
//1. Don't use password and other sensitive fields
//2. Use the information that are useful in other parts
if (!user) return null;
var u = {
userId: user.userId,
name: user.name,
username: user.username,
isAdmin: user.isAdmin
};
return jwt.sign(u, process.env.JWT_SECRET, {
expiresIn: 60 * 60 * 24 // expires in 24 hours
});
}
// return basic user details
function getCleanUser(user) {
if (!user) return null;
return {
userId: user.userId,
name: user.name,
username: user.username,
isAdmin: user.isAdmin
};
}
module.exports = {
generateToken,
getCleanUser
}