Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // in express we can pass a callback to another callback using next
- // 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()
- // callback to else in our middleware
- // in the auth_middleware.js
- let authenticated = false;
- let requireLogin = (req, res, next) => {
- if (!authenticated) {
- res.send('not logged in'); // res.redirect('/login');
- console.log('not logged');
- } else {
- next();
- console.log('you are logged');
- }
- }
- // in the router
- let auth = require('../../auth_middleware.js');
- router.get('/profile', auth.userLogged, (req, res) => {
- res.send('helloooooo to your profile'); //or res.render('dashboard.pug');
- })
Add Comment
Please, Sign In to add comment