diff --git a/index.js b/index.js index 30f2cff..09c162c 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ */ export default class Controller { + #auth = null // 定义一个模板变量 assign(key, val) { @@ -30,23 +31,24 @@ export default class Controller { }) } - // jwt 生成token及校验token - jwt(data) { - var { enabled, ttl } = this.context.get('jwt') - var { mixKey } = this.request - var auth = this.request.header('authorization') + get jwt() { + return { + result: this.#auth, + sign(data) { + let { ttl } = this.context.get('jwt') + let { __mix_key__ } = this.request + return this.context.$$jwt.sign(data, __mix_key__, ttl) + }, + verify() { + this.#auth = null + let token = this.request.header('authorization') + let { __mix_key__ } = this.request - if (enabled) { - if (data) { - return this.context.$$jwt.sign(data, mixKey, ttl) - } else { - if (auth) { - this.jwt.result = this.context.$$jwt.verify(auth, mixKey) - return this.jwt.result + if (token) { + this.#auth = this.context.$$jwt.verify(token, __mix_key__) + return this.#auth } } - } else { - throw Error('Jwt was disabled.') } }