-
Notifications
You must be signed in to change notification settings - Fork 1
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
2 changed files
with
18 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
module.exports = (req, res, next) => { | ||
return next(); | ||
const token = req.cookies.token; | ||
|
||
// verify jwt if it exists | ||
if (token) { | ||
return jwt.verify(token, process.env.SECRET, (err) => { | ||
if (err) return res.status(500).send('Error reading JWT.'); | ||
|
||
// continue if verification is successful | ||
return next(); | ||
}); | ||
} | ||
|
||
// if jwt doesn't exist, display message to user | ||
return res.status(403).send('You need to log in to view this page.'); | ||
}; |