-
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.
Merge pull request #3 from ShaunDaniel/JWT-Auth
Added JWT to authorization header
- Loading branch information
Showing
13 changed files
with
175 additions
and
129 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
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,6 +1,24 @@ | ||
function isAuthenticated(req, res, next) { | ||
if (req.user) {return next();} | ||
} | ||
|
||
|
||
module.exports = isAuthenticated; | ||
function authenticateToken(req, res, next) { | ||
// Get auth header value | ||
const bearerHeader = req.headers['authorization']; | ||
// Check if bearer is undefined | ||
if(typeof bearerHeader !== 'undefined') { | ||
// Split at the space | ||
const bearer = bearerHeader.split(' '); | ||
// Get token from array | ||
const bearerToken = bearer[1]; | ||
// Verify the token | ||
jwt.verify(bearerToken, process.env.JWT_SECRET, (err, authData) => { | ||
if(err) { | ||
res.sendStatus(403); | ||
} else { | ||
// If verification is successful, attach the user data to the request | ||
req.user = authData; | ||
next(); | ||
} | ||
}); | ||
} else { | ||
// Forbidden | ||
res.sendStatus(403); | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.