-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(JWT): Add support for JWT Authentication (#30)
Closes #27
- Loading branch information
1 parent
c035504
commit 4917e6b
Showing
10 changed files
with
90 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,5 +1,6 @@ | ||
export default { | ||
env: 'development', | ||
jwtSecret: '0a6b944d-d2fb-46fc-a85e-0295c986cd9f', | ||
db: 'mongodb://localhost/express-mongoose-es6-rest-api-development', | ||
port: 3000 | ||
}; |
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,6 @@ | ||
export default { | ||
env: 'production', | ||
jwtSecret: '0a6b944d-d2fb-46fc-a85e-0295c986cd9f', | ||
db: 'mongodb://localhost/express-mongoose-es6-rest-api-production', | ||
port: 3000 | ||
}; |
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,6 @@ | ||
export default { | ||
env: 'test', | ||
jwtSecret: '0a6b944d-d2fb-46fc-a85e-0295c986cd9f', | ||
db: 'mongodb://localhost/express-mongoose-es6-rest-api-test', | ||
port: 3000 | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import httpStatus from 'http-status'; | ||
import APIError from '../helpers/APIError'; | ||
|
||
const config = require('../../config/env'); | ||
|
||
// sample user, used for authentication | ||
const user = { | ||
username: 'react', | ||
password: 'express' | ||
}; | ||
|
||
/** | ||
* Returns jwt token if valid username and password is provided | ||
* @param req | ||
* @param res | ||
* @param next | ||
* @returns {*} | ||
*/ | ||
function login(req, res, next) { | ||
// Ideally you'll fetch this from the db | ||
// Idea here was to show how jwt works with simplicity | ||
if (req.body.username === user.username && req.body.password === user.password) { | ||
const token = jwt.sign({ | ||
username: user.username | ||
}, config.jwtSecret); | ||
return res.json({ | ||
token, | ||
username: user.username | ||
}); | ||
} | ||
|
||
const err = new APIError('Authentication error', httpStatus.UNAUTHORIZED); | ||
return next(err); | ||
} | ||
|
||
/** | ||
* This is a protected route. Will return random number only if jwt token is provided in header. | ||
* @param req | ||
* @param res | ||
* @returns {*} | ||
*/ | ||
function getRandomNumber(req, res) { | ||
// req.user is assigned by jwt middleware if valid token is provided | ||
return res.json({ | ||
user: req.user, | ||
num: Math.random() * 100 | ||
}); | ||
} | ||
|
||
export default { login, getRandomNumber }; |
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,19 @@ | ||
import express from 'express'; | ||
import validate from 'express-validation'; | ||
import expressJwt from 'express-jwt'; | ||
import paramValidation from '../../config/param-validation'; | ||
import authCtrl from '../controllers/auth'; | ||
import config from '../../config/env'; | ||
|
||
const router = express.Router(); // eslint-disable-line new-cap | ||
|
||
/** POST /api/auth/login - Returns token if correct username and password is provided */ | ||
router.route('/login') | ||
.post(validate(paramValidation.login), authCtrl.login); | ||
|
||
/** GET /api/auth/random-number - Protected route, | ||
* needs token returned by the above as header. Authorization: Bearer {token} */ | ||
router.route('/random-number') | ||
.get(expressJwt({ secret: config.jwtSecret }), authCtrl.getRandomNumber); | ||
|
||
export default router; |
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