正在显示
3 个修改的文件
包含
28 行增加
和
24 行删除
@@ -11,6 +11,7 @@ const cors = require('koa-cors'); | @@ -11,6 +11,7 @@ const cors = require('koa-cors'); | ||
11 | const index = require('./routes/index') | 11 | const index = require('./routes/index') |
12 | const users = require('./routes/users') | 12 | const users = require('./routes/users') |
13 | const filterUrl = require(__dirname+'/util/filterUrl') | 13 | const filterUrl = require(__dirname+'/util/filterUrl') |
14 | +var tokenUtil = require('./util/tokenUtil'); | ||
14 | const _ = require('lodash'); | 15 | const _ = require('lodash'); |
15 | // error handler | 16 | // error handler |
16 | onerror(app) | 17 | onerror(app) |
@@ -30,17 +31,25 @@ app.use(views(__dirname + '/views', { | @@ -30,17 +31,25 @@ app.use(views(__dirname + '/views', { | ||
30 | 31 | ||
31 | // logger | 32 | // logger |
32 | app.use(async (ctx, next) => { | 33 | app.use(async (ctx, next) => { |
33 | - const start = new Date(); | ||
34 | - if(filterUrl.indexOf(ctx.request.url) != -1){ | ||
35 | - await next(); | ||
36 | - }else if(!ctx.header.token){ | ||
37 | - ctx.response.status = 400; | ||
38 | - ctx.response.body = {msg:'请登录'} | ||
39 | - }else{ | ||
40 | - await next(); | 34 | + try{ |
35 | + const start = new Date(); | ||
36 | + if(filterUrl.indexOf(ctx.request.url) != -1){ | ||
37 | + await next(); | ||
38 | + }else if(!ctx.header.token){ | ||
39 | + ctx.response.status = 200; | ||
40 | + ctx.response.body = {code:0,msg:'请登录'} | ||
41 | + }else{ | ||
42 | + let isToken = await tokenUtil.prverifySession(ctx.header.token); | ||
43 | + await next(); | ||
44 | + } | ||
45 | + const ms = new Date() - start; | ||
46 | + console.log(`${ctx.method} ${ctx.url} - ${ms}ms`) | ||
47 | + }catch (e){ | ||
48 | + console.log('返回的err',e.message) | ||
49 | + ctx.response.status = 200; | ||
50 | + ctx.response.body = {code:0,msg:e.message} | ||
41 | } | 51 | } |
42 | - const ms = new Date() - start | ||
43 | - console.log(`${ctx.method} ${ctx.url} - ${ms}ms`) | 52 | + |
44 | }) | 53 | }) |
45 | 54 | ||
46 | // routes | 55 | // routes |
@@ -15,7 +15,8 @@ module.exports = new JwtSession() ; | @@ -15,7 +15,8 @@ module.exports = new JwtSession() ; | ||
15 | * @param cb | 15 | * @param cb |
16 | */ | 16 | */ |
17 | JwtSession.prototype.getSession = (users) =>{ | 17 | JwtSession.prototype.getSession = (users) =>{ |
18 | - var expiresIn = 60 * 60 * 60 * 60 * 60; | 18 | + // var expiresIn = 60 * 60 * 60 * 60 * 60; |
19 | + var expiresIn = 60 * 60 ; | ||
19 | var payload = {}; | 20 | var payload = {}; |
20 | payload.id = users.id; | 21 | payload.id = users.id; |
21 | payload.name = users.loginName; | 22 | payload.name = users.loginName; |
@@ -36,16 +37,10 @@ JwtSession.prototype.getSession = (users) =>{ | @@ -36,16 +37,10 @@ JwtSession.prototype.getSession = (users) =>{ | ||
36 | * @param token | 37 | * @param token |
37 | * @param cb | 38 | * @param cb |
38 | */ | 39 | */ |
39 | -JwtSession.verifySession = function (token,cb) { | ||
40 | - jwt.verify(token, 'Efangcpap',function (err ,data) { | ||
41 | - console.dir(data); | ||
42 | - if (err && err.name === 'TokenExpiredError') { | ||
43 | - cb && cb({message: 'token expired'}); | ||
44 | - } else if (err && err.name === 'JsonWebTokenError') { | ||
45 | - cb && cb({message: 'invalid token'}); | ||
46 | - }else { | ||
47 | - cb && cb (null,data); | ||
48 | - } | ||
49 | - }); | ||
50 | - | 40 | +JwtSession.prototype.prverifySession = async(token) => { |
41 | + try { | ||
42 | + let back =jwt.verify(token, '3mang'); | ||
43 | + } catch (error) { | ||
44 | + throw new Error(error) | ||
45 | + } | ||
51 | }; | 46 | }; |
-
请 注册 或 登录 后发表评论