From 6211b4cf90ca371052ba3c7c5127e0f376146b19 Mon Sep 17 00:00:00 2001
From: 付智勇 <fuzhiyong@efangtec.com>
Date: Wed, 23 Aug 2017 10:00:54 +0800
Subject: [PATCH] token 验证

---
 app.js            | 29 +++++++++++++++++++----------
 util/filterUrl.js |  2 +-
 util/tokenUtil.js | 21 ++++++++-------------
 3 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/app.js b/app.js
index 0186a31..4be2407 100644
--- a/app.js
+++ b/app.js
@@ -11,6 +11,7 @@ const cors = require('koa-cors');
 const index = require('./routes/index')
 const users = require('./routes/users')
 const filterUrl = require(__dirname+'/util/filterUrl')
+var tokenUtil =  require('./util/tokenUtil');
 const _ = require('lodash');
 // error handler
 onerror(app)
@@ -30,17 +31,25 @@ app.use(views(__dirname + '/views', {
 
 // logger
 app.use(async (ctx, next) => {
-  const start = new Date();
-  if(filterUrl.indexOf(ctx.request.url) != -1){
-        await next();
-  }else if(!ctx.header.token){
-    ctx.response.status = 400;
-    ctx.response.body = {msg:'请登录'}
-  }else{
-    await next();
+  try{
+    const start = new Date();
+    if(filterUrl.indexOf(ctx.request.url) != -1){
+          await next();
+    }else if(!ctx.header.token){
+      ctx.response.status = 200;
+      ctx.response.body = {code:0,msg:'请登录'}
+    }else{
+      let isToken = await tokenUtil.prverifySession(ctx.header.token);
+      await next();      
+    }
+    const ms = new Date() - start;
+    console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)    
+  }catch (e){
+    console.log('返回的err',e.message)
+    ctx.response.status = 200;
+    ctx.response.body = {code:0,msg:e.message}
   }
-  const ms = new Date() - start
-  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
+  
 })
 
 // routes
diff --git a/util/filterUrl.js b/util/filterUrl.js
index a04f6ef..b995a9d 100644
--- a/util/filterUrl.js
+++ b/util/filterUrl.js
@@ -1 +1 @@
-module.exports = ["/users/addUser","/users/login"]
\ No newline at end of file
+module.exports = ["/users/addUser",]
\ No newline at end of file
diff --git a/util/tokenUtil.js b/util/tokenUtil.js
index c10c368..1499525 100644
--- a/util/tokenUtil.js
+++ b/util/tokenUtil.js
@@ -15,7 +15,8 @@ module.exports = new JwtSession() ;
  * @param cb
  */
 JwtSession.prototype.getSession =  (users) =>{
-  var expiresIn = 60 * 60 * 60 * 60 * 60;
+  // var expiresIn = 60 * 60 * 60 * 60 * 60;
+  var expiresIn = 60 * 60 ;
   var payload = {};
   payload.id = users.id;
   payload.name = users.loginName;
@@ -36,16 +37,10 @@ JwtSession.prototype.getSession =  (users) =>{
  * @param token
  * @param cb
  */
-JwtSession.verifySession = function (token,cb) {
-   jwt.verify(token, 'Efangcpap',function (err ,data) {
-     console.dir(data);
-       if (err && err.name === 'TokenExpiredError') {
-         cb && cb({message: 'token expired'});
-       } else if (err && err.name === 'JsonWebTokenError') {
-         cb && cb({message: 'invalid token'});
-       }else {
-         cb && cb (null,data);
-       }
-   });
-
+JwtSession.prototype.prverifySession =  async(token) => {
+  try {
+    let back =jwt.verify(token, '3mang');
+  } catch (error) {
+    throw new Error(error)
+  }
 };
--
libgit2 0.24.0