-
Notifications
You must be signed in to change notification settings - Fork 50
/
token.js
33 lines (31 loc) · 1.03 KB
/
token.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
var jwt = require('jsonwebtoken');
var jwtScrect = 'aXloaWR1NzZidmgyaGpnaDEyNCEh'; //签名
//登录接口 生成token的方法
var setToken = function (user_name, user_id) {
return new Promise((resolve, reject) => {
//expiresln 设置token过期的时间
//{ user_name: user_name, user_id: user_id } 传入需要解析的值( 一般为用户名,用户id 等)
const token = jwt.sign({ user_name: user_name, user_id: user_id }, jwtScrect, { expiresIn: '24h' });
resolve(token)
})
}
//各个接口需要验证token的方法
var getToken = function (token) {
return new Promise((resolve, reject) => {
if (!token) {
console.log('token is null')
reject({
error: 'token is null'
})
}
else {
//第二种 改版后的
var info = jwt.verify(token.split(' ')[1], jwtScrect);
resolve(info); //解析返回的值(sign 传入的值)
}
})
}
module.exports = {
setToken,
getToken
}