From 76d205a1c4e99a63eee304d0c84baa7bda07100a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Mon, 21 Sep 2020 19:24:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=85=A5cookie=E6=93=8D=E4=BD=9C;jwt?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E6=89=8B=E5=8A=A8=E8=A7=A6=E5=8F=91=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 29 ++++++++++++++++++++++++----- package.json | 3 +++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 921d6b3..2d379b1 100644 --- a/index.js +++ b/index.js @@ -17,16 +17,12 @@ export default class Controller { jwt.expires = ctx.get('session').ttl jwt.secret = ctx.get('jwt') - this.jwt = { - sign: jwt.sign, - result: jwt.verify(req.header('authorized')) - } + this.jwt = { sign: jwt.sign } // smarty.config('path', this.ctx.get('VIEWS')) // smarty.config('ext', this.ctx.get('temp_ext')) // smarty.config('cache', !!this.ctx.get('temp_cache')) - this.cookie = ctx.ins('cookie') // this.session = this.ctx.ins('session') } @@ -56,6 +52,29 @@ export default class Controller { // }) // } + checkAuth() { + this.jwt.result = jwt.verify(this.request.header('authorized')) + } + + // cookie读写 + cookie(key, val, opt) { + if (arguments.length < 2) { + return this.request.cookie(key) + } + + if (!opt) { + opt = {} + } + opt.domain = opt.domain || this.context.get('domain') + + if (val === null || val === undefined) { + delete opt.expires + opt.maxAge = -1 + } + + this.response.cookie(key, val, opt) + } + // RESFULL-API规范的纯API返回 send(status = 200, msg = 'success', data = {}) { if (typeof msg === 'object') { diff --git a/package.json b/package.json index 47961cc..48ea33e 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,9 @@ "type": "module", "description": "控制器基类。", "main": "index.js", + "dependencies": { + "@gm5/jwt": "^1.0.0" + }, "author": "yutent", "keywords": ["fivejs", "controller", "http"], "repository": "https://github.com/bytedo/gmf.controller.git",