宇天 2020-09-18 14:45:42 +08:00
parent 1628dee066
commit 037918cd58
3 changed files with 11 additions and 14 deletions

View File

@ -1,12 +1,12 @@
![module info](https://nodei.co/npm/@gm5/controller.png?downloads=true&downloadRank=true&stars=true)
![module info](https://nodei.co/npm/@gm5/jwt.png?downloads=true&downloadRank=true&stars=true)
# @gm5/controller
# @gm5/jwt
> 控制器基类
> json web token
## 安装
```bash
npm install @gm5/controller
npm install @gm5/jwt
```

View File

@ -12,11 +12,9 @@ function hmac(str, secret) {
return base64encode(buf, true)
}
export default class Jwt {
constructor(expires, secret) {
this.expires = expires
this.secret = secret
}
export default {
expires: 7 * 24 * 3600,
secret: 'this is secret key',
// 签名, 返回token
sign(data) {
@ -34,11 +32,10 @@ export default class Jwt {
auth_str = hmac(`${header}.${payload}`, secret)
return [header, payload, auth_str].join('.')
}
},
// 校验token
verify(token) {
var { secret } = this
verify(token = '') {
var jwt = token.split('.')
var auth_str, payload
@ -53,7 +50,7 @@ export default class Jwt {
return 'expired'
}
if (hmac(jwt.join('.'), secret) === auth_str) {
if (hmac(jwt.join('.'), this.secret) === auth_str) {
return payload.data
}

View File

@ -2,7 +2,7 @@
"name": "@gm5/jwt",
"version": "1.0.0",
"type": "module",
"description": "JSON WEB TOKEN",
"description": "json web token",
"main": "index.js",
"author": "yutent",
"keywords": ["fivejs", "jwt", "http"],