Guest User

Untitled

a guest
Feb 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. // in express we can pass a callback to another callback using next
  2. // for example if we need to choose between two response (if condition is made res.send else res.send) to do that we need to pass next()
  3. // callback to else in our middleware
  4.  
  5. // in the auth_middleware.js
  6. let authenticated = false;
  7. let requireLogin = (req, res, next) => {
  8. if (!authenticated) {
  9. res.send('not logged in'); // res.redirect('/login');
  10. console.log('not logged');
  11. } else {
  12. next();
  13. console.log('you are logged');
  14. }
  15. }
  16.  
  17. // in the router
  18. let auth = require('../../auth_middleware.js');
  19. router.get('/profile', auth.userLogged, (req, res) => {
  20. res.send('helloooooo to your profile'); //or res.render('dashboard.pug');
  21. })
Add Comment
Please, Sign In to add comment