Permalink
Browse files

Added custom middleware support with middlewares folder

  • Loading branch information...
erayaydin committed Jun 27, 2016
1 parent 320f83c commit 3a0e44d9baf58834a625bee487a572cf700cd19b
Showing with 39 additions and 30 deletions.
  1. +2 −1 README.md
  2. +12 −6 app.js
  3. +0 −23 config/passport.js
  4. +11 −0 middlewares/isAuthenticated.js
  5. +14 −0 middlewares/isAuthorized.js
@@ -32,6 +32,7 @@ Project starter for **Node.js** web applications.
| **controllers**/home.js | Controller for home page |
| **controllers**/user.js | Controller for user account management |
| **models**/User.js | User model |
| **middlewares**/ | Middlewares folder |
| **public**/ | Static files (fonts, css, js, images) |
| **views**/ | Template files |
| **views**/layout.jade | Main layout template |
@@ -87,7 +88,7 @@ Project starter for **Node.js** web applications.
## Ideas - Todo
- [X] Adding `socket.io`
- [ ] Custom middleware support with **middlewares**/ folder.
- [X] Custom middleware support with **middlewares**/ folder.
- [ ] Adding test scripts for user login and register.
- [ ] Add `bower` support
- [ ] Add **sass** and **public js assets** support with minify.
18 app.js
@@ -36,6 +36,12 @@ const userController = require('./controllers/user');
*/
const passportConfig = require('./config/passport');
/**
* Middlewares
*/
const isAuthenticated = require('./middlewares/isAuthenticated');
const isAuthorized = require('./middlewares/isAuthorized');
/**
* Express Server
*/
@@ -100,18 +106,18 @@ app.use(express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 }))
app.get('/', homeController.index);
app.get('/login', userController.login);
app.post('/login', userController.doLogin);
app.get('/logout', passportConfig.isAuthenticated, userController.logout);
app.get('/logout', isAuthenticated, userController.logout);
app.get('/forgot', userController.forgot);
app.post('/forgot', userController.doForgot);
app.get('/reset/:token', userController.reset);
app.post('/reset/:token', userController.doReset);
app.get('/register', userController.register);
app.post('/register', userController.doRegister);
app.get('/account', passportConfig.isAuthenticated, userController.account);
app.post('/account/profile', passportConfig.isAuthenticated, userController.updateProfile);
app.post('/account/password', passportConfig.isAuthenticated, userController.updatePassword);
app.post('/account/delete', passportConfig.isAuthenticated, userController.deleteAccount);
app.get('/account/unlink/:provider', passportConfig.isAuthenticated, userController.oauthUnlink);
app.get('/account', isAuthenticated, userController.account);
app.post('/account/profile', isAuthenticated, userController.updateProfile);
app.post('/account/password', isAuthenticated, userController.updatePassword);
app.post('/account/delete', isAuthenticated, userController.deleteAccount);
app.get('/account/unlink/:provider', isAuthenticated, userController.oauthUnlink);
/**
* OAuth Authentication Routes
@@ -322,26 +322,3 @@ passport.use(new InstagramStrategy({
});
}
}));
/**
* Login Required middleware.
*/
exports.isAuthenticated = (req, res, next) => {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login');
};
/**
* Authorization Required middleware.
*/
exports.isAuthorized = (req, res, next) => {
const provider = req.path.split('/').slice(-1)[0];
if (_.find(req.user.tokens, { kind: provider })) {
next();
} else {
res.redirect(`/auth/${provider}`);
}
};
@@ -0,0 +1,11 @@
const _ = require('lodash');
/**
* Login Required middleware.
*/
module.exports = (req, res, next) => {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login');
};
@@ -0,0 +1,14 @@
const _ = require('lodash');
/**
* Authorization Required middleware.
*/
module.exports = (req, res, next) => {
const provider = req.path.split('/').slice(-1)[0];
if (_.find(req.user.tokens, { kind: provider })) {
next();
} else {
res.redirect(`/auth/${provider}`);
}
};

0 comments on commit 3a0e44d

Please sign in to comment.