diff options
author | Mateja <mail@matejamaric.com> | 2021-05-07 00:17:56 +0200 |
---|---|---|
committer | Mateja <mail@matejamaric.com> | 2021-05-07 00:17:56 +0200 |
commit | d7e364d83153425e412032dd997e9c58cbc235eb (patch) | |
tree | a2095100f5c5e65633c0015a0cc378deb040ac62 /routes/web.js | |
parent | 3237737dc88ebff88c258972ac902f9221a003b5 (diff) | |
download | node-playground-d7e364d83153425e412032dd997e9c58cbc235eb.tar.gz node-playground-d7e364d83153425e412032dd997e9c58cbc235eb.zip |
Using `passport.js` for login system.
Diffstat (limited to 'routes/web.js')
-rw-r--r-- | routes/web.js | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/routes/web.js b/routes/web.js index 8960bb3..b6a85fe 100644 --- a/routes/web.js +++ b/routes/web.js @@ -1,19 +1,28 @@ const express = require('express'); +const passport = require('passport'); const router = express.Router(); + const postController = require('../controllers/post'); const userController = require('../controllers/user'); +const isAuth = (req, res, next) => { + if (req.isAuthenticated()) next(); + else res.redirect('/login'); +}; + router.get('/', postController.index); -router.get('/new-post', postController.create); -router.post('/new-post', postController.store); -router.get('/remove-post/:id', postController.destroy); +router.get('/new-post', isAuth, postController.create); +router.post('/new-post', isAuth, postController.store); +router.get('/remove-post/:id', isAuth, postController.destroy); router.get('/register', userController.registerView); router.get('/login', userController.loginView); router.post('/register', userController.register); -router.post('/login', userController.login); - +router.post('/login', passport.authenticate('local', { + failureRedirect: '/login', + successRedirect: '/new-post' +})); router.get('/logout', userController.logout); module.exports = router; |