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 {
#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.')
}
}