Browse Source

jwt module split to check function and graphql middlware

Ivan Asmer 4 years ago
parent
commit
5d23ded7be
3 changed files with 29 additions and 24 deletions
  1. 1 4
      index.js
  2. 24 16
      jwt.js
  3. 4 4
      models.js

+ 1 - 4
index.js

@@ -1,5 +1,3 @@
-const ObjectID    = require("mongodb").ObjectID;
-const jwt         = require('jsonwebtoken')
 const jwtSecret   = 'CbymrfGfnB'
 
 const express = require('express');
@@ -11,7 +9,7 @@ const expand = require('./expand')
 ;(async () => {
 
     const {Savable, slice, getModels} = await require('./models.js')()
-    const jwtGQL = require('./jwt')
+    const jwtGQL = require('./jwt').jwtGQL
 
     const {anonSchema, anonResolvers} = require('./anon')({Savable, secret: jwtSecret})
 
@@ -103,4 +101,3 @@ const expand = require('./expand')
     app.use('/graphql', express_graphql(jwtGQL({anonSchema, anonResolvers, schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
     app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
 })()
-

+ 24 - 16
jwt.js

@@ -1,12 +1,19 @@
 const jwt         = require('jsonwebtoken')
-module.exports = ({anonSchema, anonResolvers={}, schema, rootValue={},secret, createContext, graphiql=true}) => 
-    async (req, res, gql) => { 
-        const authorization = req.headers.authorization 
-        
-        if (authorization && authorization.startsWith('Bearer ')){
-            const token = authorization.substr("Bearer ".length)
-            const decoded = jwt.verify(token, secret)
-            if (decoded){
+function jwtCheck(req, secret) {
+    const authorization = req && req.headers && req.headers.authorization 
+
+    if (authorization && authorization.startsWith('Bearer ')){
+        const token = authorization.substr("Bearer ".length)
+        const decoded = jwt.verify(token, secret)
+        return decoded
+    }
+}
+
+module.exports = {
+    jwtGQL: ({anonSchema, anonResolvers={}, schema, rootValue={},secret, createContext, graphiql=true}) => 
+        async (req, res, gql) => { 
+            let decoded;
+            if (decoded = jwtCheck(req, secret)){
                 let context  = await createContext(decoded.sub)
                 context.jwt  = decoded.sub
 
@@ -15,12 +22,13 @@ module.exports = ({anonSchema, anonResolvers={}, schema, rootValue={},secret, cr
                     rootValue, 
                     graphiql,
                     context
-                }
+                };
             }
-        }
-        return {
-            schema: anonSchema,
-            rootValue: anonResolvers,
-            graphiql, 
-        }
-    }
+            return {
+                schema: anonSchema,
+                rootValue: anonResolvers,
+                graphiql, 
+            }
+        },
+    jwtCheck,
+}

+ 4 - 4
models.js

@@ -1,6 +1,5 @@
-const MongoClient = require("mongodb").MongoClient;
 const ObjectID    = require("mongodb").ObjectID;
-const {connect}          = require('mm')
+const {connect}   = require('mm')
 
 module.exports = async (dbName='shop') => {
     const {Savable, slice} = await connect(dbName)
@@ -92,8 +91,9 @@ module.exports = async (dbName='shop') => {
         const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
 
         return {models: {
-            SlicedSavable, User, Good, Category, Order, OrderGood
-        }}
+                            SlicedSavable, User, Good, Category, Order, OrderGood
+                        }, 
+                thisUser}
     }
 
     return {