-
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
1 parent
817d4a4
commit b60a841
Showing
6 changed files
with
44 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
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const { JWT_SECRET } = process.env; | ||
const extractToken = (authorization) => authorization.split(' ')[1]; | ||
|
||
const auth = (req, _res, next) => { | ||
const authorization = req.header('Authorization'); | ||
console.log(authorization); | ||
if (!authorization) { | ||
const err = new Error('Token not found'); | ||
err.status = 401; | ||
return next(err); | ||
} | ||
try { | ||
const token = extractToken(authorization); | ||
const { email } = jwt.verify(token, JWT_SECRET); | ||
req.email = email; | ||
} catch (err) { | ||
const newErr = new Error('Expired or invalid token'); | ||
newErr.status = 401; | ||
return next(newErr); | ||
} | ||
next(); | ||
}; | ||
|
||
module.exports = auth; |
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,5 +1,7 @@ | ||
const error = require('./error'); | ||
const auth = require('./auth'); | ||
|
||
module.exports = { | ||
error, | ||
auth, | ||
}; |
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