tokenUtil.js
853 字节
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
/**
* Created by fzy on 17/8/16.
*/
var jwt = require('jsonwebtoken');
var ObjectId = require('objectid');
function JwtSession() {
}
module.exports = new JwtSession() ;
/**
* 生成token
* @param users
* @param cb
*/
JwtSession.prototype.getSession = (users) =>{
console.log(users)
var expiresIn = 60 * 60 * 1000;
var payload = {};
payload.id = users.id;
payload.name = users.userName;
payload.userRole =users.userRole;
var options = {
"expiresIn": expiresIn
};
var token = jwt.sign(payload, '3mang', options);
users.token = token;
users.tokenTime = expiresIn;
return users;
};
/**
* 验证token
* @param token
* @param cb
*/
JwtSession.prototype.prverifySession = async(token) => {
try {
let back =jwt.verify(token, '3mang');
return back;
} catch (error) {
throw new Error(error)
}
};