forked from pawelmalak/flame
-
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.
Added auth middleware. Added access control to apps
- Loading branch information
1 parent
d1c61bb
commit e3f1679
Showing
16 changed files
with
92 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
module.exports = { | ||
login: require('./login'), | ||
validate: require('./validate'), | ||
}; |
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,21 @@ | ||
const asyncWrapper = require('../../middleware/asyncWrapper'); | ||
const ErrorResponse = require('../../utils/ErrorResponse'); | ||
const jwt = require('jsonwebtoken'); | ||
|
||
// @desc Verify token | ||
// @route POST /api/auth/verify | ||
// @access Public | ||
const validate = asyncWrapper(async (req, res, next) => { | ||
try { | ||
jwt.verify(req.body.token, process.env.SECRET); | ||
|
||
res.status(200).json({ | ||
success: true, | ||
data: { token: { isValid: true } }, | ||
}); | ||
} catch (err) { | ||
return next(new ErrorResponse('Token expired', 401)); | ||
} | ||
}); | ||
|
||
module.exports = validate; |
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,25 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const auth = (req, res, next) => { | ||
const authHeader = req.header('Authorization'); | ||
let token; | ||
let tokenIsValid = false; | ||
|
||
if (authHeader && authHeader.startsWith('Bearer ')) { | ||
token = authHeader.split(' ')[1]; | ||
} | ||
|
||
if (token) { | ||
try { | ||
jwt.verify(token, process.env.SECRET); | ||
} finally { | ||
tokenIsValid = true; | ||
} | ||
} | ||
|
||
req.isAuthenticated = tokenIsValid; | ||
|
||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
|
||
const { login } = require('../controllers/auth'); | ||
const { login, validate } = require('../controllers/auth'); | ||
const requireBody = require('../middleware/requireBody'); | ||
|
||
router.route('/').post(requireBody(['password', 'duration']), login); | ||
|
||
router.route('/validate').post(requireBody(['token']), validate); | ||
|
||
module.exports = router; |