Skip to content

Commit

Permalink
cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
im-ghost committed Oct 14, 2022
1 parent c1cc0d2 commit 2e17079
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web:node server.js
web:node app.js
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

2 changes: 0 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 14 additions & 29 deletions middleware/authMiddleware.js
Original file line number Diff line number Diff line change
@@ -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
33 changes: 7 additions & 26 deletions schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: ()=>({
Expand Down

0 comments on commit 2e17079

Please sign in to comment.