-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathtoken.js
More file actions
49 lines (39 loc) · 1 KB
/
token.js
File metadata and controls
49 lines (39 loc) · 1 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
/**
const jwt = require('jsonwebtoken')
// 密钥
const key = 'secretKey'
// 这里的 data 是用户名,使用用户名来生成唯一的 token
function generateToken(data) {
const token = jwt.sign({
data,
exp: Math.floor(Date.now() / 1000) + (3600 * 24 * 7), // 有效期一周
}, key)
return token
}
是否是有效的 token
是否过期
是否和数据库中保存的 token 一致
function isVaildToken(db, token) {
return new Promise(resolve => {
let result
try {
result = jwt.verify(token, key)
} catch(e) {
resolve(false)
}
const { exp } = result
const current = Math.floor(Date.now() / 1000)
if (current > exp) {
resolve(false)
}
db.collection(userCollection).findOne({ token }).then(res => {
console.log(res)
if (res) {
resolve(true)
} else {
resolve(false)
}
})
})
}
*/