Guest User

Untitled

a guest
Jul 20th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. const express = require('express')
  2. , app = express();
  3.  
  4. // route without protection
  5. app.get('/', (req, res) => {
  6. res.send('hi')
  7. })
  8.  
  9.  
  10. app.get('/login', (req, res) => {
  11. // obviously, you would have some kind of validation of user here before assigning/creating req.session.user
  12. req.session.user = 'foo';
  13.  
  14. res.redirect('/protected');
  15. })
  16.  
  17. // route with protection
  18. app.get('/protected',
  19. , (req, res, next) => {
  20. // once you incorporate express-session middleware, req.session can be used.
  21. if(req.session.user) {
  22. next()
  23. }
  24. }
  25. , (req, res) => {
  26. res.send('protected')
  27. })
  28.  
  29.  
  30. app.listen(3000, console.log('listening...'))
Add Comment
Please, Sign In to add comment