Browse Source

jwt two resolvers in module

Ivan Asmer 4 years ago
parent
commit
546a7293b5
2 changed files with 32 additions and 31 deletions
  1. 29 28
      index.js
  2. 3 3
      models.js

+ 29 - 28
index.js

@@ -159,6 +159,8 @@ function mmExpandSchema(gqlSchema){
 ;(async () => {
     const {Savable, slice, getModels} = await require('./models.js')()
 
+    const jwtGQL = require('./jwt')
+
     class User extends Savable {
         static get relations(){
             return {
@@ -308,36 +310,35 @@ function mmExpandSchema(gqlSchema){
 
     `)
 
+    app.use('/graphql', express_graphql(jwtGQL({anonSchema, anonResolvers, schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
 
-    app.use('/graphql', express_graphql(async (req, res, gql) => { 
-        const authorization = req.headers.authorization 
+    //app.use('/graphql', express_graphql(async (req, res, gql) => { 
+        //const authorization = req.headers.authorization 
         
-        if (authorization && authorization.startsWith('Bearer ')){
-            console.log('token provided')
-            const token = authorization.substr("Bearer ".length)
-            const decoded = jwt.verify(token, jwtSecret)
-            if (decoded){
-                console.log('token verified', decoded)
-
-                let slicedModels  = await getModels(decoded.sub.id)
-
-                return {
-                    schema: schema,
-                    rootValue: {},
-                    graphiql: true, 
-                    context: {jwt: decoded.sub,
-                              models: slicedModels}
-                }
-            }
-        }
-        else {
-            return {
-                schema: anonSchema,
-                rootValue: anonResolvers,
-                graphiql: true, 
-            }
-        }
-    }))
+        //if (authorization && authorization.startsWith('Bearer ')){
+            //console.log('token provided')
+            //const token = authorization.substr("Bearer ".length)
+            //const decoded = jwt.verify(token, jwtSecret)
+            //if (decoded){
+                //console.log('token verified', decoded)
+
+                //let slicedModels  = await getModels(decoded.sub.id)
+
+                //return {
+                    //schema: schema,
+                    //rootValue: {},
+                    //graphiql: true, 
+                    //context: {jwt: decoded.sub,
+                              //models: slicedModels}
+                //}
+            //}
+        //}
+        //return {
+            //schema: anonSchema,
+            //rootValue: anonResolvers,
+            //graphiql: true, 
+        //}
+    //}))
 
     app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
 })()

+ 3 - 3
models.js

@@ -5,7 +5,7 @@ const {connect}          = require('mm')
 module.exports = async (dbName='shop') => {
     const {Savable, slice} = await connect(dbName)
 
-    async function getModels(id){
+    async function getModels({id}){
         const SlicedSavable = slice([id, 'user', 'admin'])
 
         class User extends SlicedSavable {
@@ -91,9 +91,9 @@ module.exports = async (dbName='shop') => {
 
         const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
 
-        return {
+        return {models: {
             SlicedSavable, User, Good, Category, Order, OrderGood
-        }
+        }}
     }
 
     return {