22 lines
614 B
JavaScript
22 lines
614 B
JavaScript
const JsonWebToken = require('jsonwebtoken')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
module.exports = {
|
|
createToken: function (content, key) {
|
|
// content 可以是数字,非空字符串或非空对象,不可以是数组。
|
|
try {
|
|
return JsonWebToken.sign(content, crypto.createHash('sha256').update(key, 'utf8').digest('hex'))
|
|
} catch (exp) {
|
|
return null
|
|
}
|
|
},
|
|
verifyToken: function (token, key) {
|
|
try {
|
|
return JsonWebToken.verify(token, crypto.createHash('sha256').update(key, 'utf8').digest('hex'))
|
|
} catch (exp) {
|
|
return null
|
|
}
|
|
},
|
|
}
|