-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
23 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
web:node server.js | ||
web:node app.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters