yutent 2023-10-27 19:19:08 +08:00
parent b7747e4db2
commit fc291dd12a
1 changed files with 16 additions and 14 deletions

View File

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