diff --git a/Procfile b/Procfile index 186c82b..7170736 100644 --- a/Procfile +++ b/Procfile @@ -1 +1 @@ -web:node server.js +web:node app.js diff --git a/README.md b/README.md index 83c4ff0..7c8516e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,2 @@ # library-app-api -Graphql api for Li -This is an API endpoint built with express-graphql for a library app. -Graphical route is at /graphql + diff --git a/app.js b/app.js index 5428af3..6332cb2 100644 --- a/app.js +++ b/app.js @@ -12,8 +12,6 @@ const app = express(); app.use(cors()) connectDB() - -app.use(express.static(path.join(__dirname, 'public'))); app.use("/", graphqlHTTP({ schema, graphiql: true diff --git a/middleware/authMiddleware.js b/middleware/authMiddleware.js index db5b532..aa5fd50 100644 --- a/middleware/authMiddleware.js +++ b/middleware/authMiddleware.js @@ -1,33 +1,18 @@ const jwt = require('jsonwebtoken') -const asyncHandler = require('express-async-handler') const User = require('../models/user.js') -const protect = asyncHandler(async (req, res, next) => { - let token - - if ( - req.headers.authorization && - req.headers.authorization - ) { - try { - token = req.headers.authorization - - const decoded = jwt.verify(token, "secret") - - req.user = await User.findById(decoded._id).select('-password') - - next() - } catch (error) { - console.error(error) - res.status(401) - throw new Error('Not authorized, token failed') - } +const ensureAuth =async (fn,args,token) =>{ + if(token){ + const decoded = jwt.verify(token, "secret ") + const user = await User.findById(decoded.id).select('-password') + if(user){ + args ? fn(args) : fn() + }else{ + throw new Error("Not authorized ") + } + }else{ + throw new Error("No token") } - - if (!token) { - res.status(401) - throw new Error('Not authorized, no token') - } -}) - -module.exports = {protect} +}/* +*/ +module.exports = ensureAuth diff --git a/schema.js b/schema.js index 8095ebd..bef9d0d 100644 --- a/schema.js +++ b/schema.js @@ -12,32 +12,13 @@ var Book = require("./models/book"); var Genre = require("./models/genre"); var Author = require("./models/author"); var User = require("./models/user") -const {createBook} = require("./controllers/book.js") - -const _ = require('lodash') -const ensureAuth =async (fn,args,token) =>{ - if(token){ - console.log("here") - const decoded = jwt.verify(token, "secret ") - console.log(decoded) - const user = await User.findById(decoded.id).select('-password') - - if(user){ - if(args){ - return fn(args) - - } - else { - return fn() - } - }else{ - throw new Error("no authorized ") - } - }else{ - throw new Error("No token") - } -}/* -*/ +const { + createBook +} = require("./controllers/book.js") +const {} = require("@/controllers/author.js") +const {} = require("@/controllers/user.js") +const {} = require("@/controllers/genre.js") +const ensureAuth = require("./middlewares/authMiddleware.js"); const authorType = new GraphQLObjectType({ name: 'author', fields: ()=>({