You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
911 B
34 lines
911 B
/**
|
|
* 生成和验证jwt
|
|
* */
|
|
const jwt = require('jsonwebtoken');
|
|
const { encrypt, decrypt } = require('./encryptionString')
|
|
|
|
// authorization
|
|
// Token.decrypt(ctx.header.authorization); // 获取其中的令牌
|
|
|
|
const CFG = global.cfg ? global.cfg.encryption : require('../../config/default.config')
|
|
|
|
module.exports = {
|
|
// 加密
|
|
encrypt: (token, time = CFG.user.login.timeLimit) => {
|
|
// 需要设置超时时间
|
|
return encrypt(jwt.sign(token, CFG.encryption.secretKey, {expiresIn: time / 1000}))
|
|
},
|
|
// 解密
|
|
decrypt: (token) => {
|
|
try {
|
|
token = decrypt(token)
|
|
let data = jwt.verify(token, CFG.encryption.secretKey);
|
|
return {
|
|
token:true,
|
|
id:data
|
|
};
|
|
} catch (e) {
|
|
return {
|
|
token:false,
|
|
data:e
|
|
}
|
|
}
|
|
}
|
|
} |