SHARE
TWEET

Untitled

a guest Jan 26th, 2019 125 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express   =   require('express');
  2. const usersRouter = express.Router();
  3. const passport  =   require('passport');
  4. const path = require('path');
  5. const mongoose = require('mongoose');
  6. const { check, body, validationResult } = require('express-validator/check');
  7. const User = require('../models/users_model');
  8. const isAuthenticated = require('./isauthenticated');
  9.  
  10. /* GET user home page. */
  11. usersRouter.get('/', function(req, res, next) {
  12.   // console.log('req.user: \n' + req.user, 'req.isAuthenticated(): ' + req.isAuthenticated())
  13.   res.render('index', {
  14.     title: 'WestApps',
  15.     okMessage: req.flash('okMessage'),
  16.     username: req.flash('username'),
  17.   });
  18. });
  19.  
  20. // get register / sign up
  21. usersRouter.get('/register', function(req, res, next) {
  22.   res.render('register', {
  23.     registerForm: 'WestApps register form',
  24.     errors: req.flash('errors'),
  25.     username: req.flash('username'),
  26.     email: req.flash('email'),
  27.   });
  28. });
  29.  
  30. // post register
  31. usersRouter.post('/register', [
  32.   check('email').isEmail(),
  33.   check('password').isLength({min: 6}),
  34.   check('confirmPassword').isLength({min: 6}),
  35. ], (req, res, next) => {
  36.   body('confirmPassword').custom((value, { req }) => {
  37.     if (value !== req.body.password) {
  38.       throw new Error('Password confirmation does not match password');
  39.     }
  40.   });
  41.   const errors = validationResult(req);
  42.   let errorsList = [];
  43.   if (!errors.isEmpty()) {
  44.     console.log(errors.array());
  45.     for (let i = 0; i < errors.array().length; i ++) {
  46.       let param = errors.array()[i].param;
  47.       let msg = errors.array()[i].msg;
  48.       if (param == 'confirmPassword') {
  49.         msg = 'Confirm password does not match password.';
  50.       }
  51.       errorsList.push(`${param} : ${msg} `);
  52.     }
  53.     req.flash('errors',errorsList);
  54.     req.flash('username', req.body.username);
  55.     req.flash('email', req.body.email);
  56.     res.redirect('/users/register');
  57.   } else {
  58.     let userInfo = {
  59.         email: req.body.email,
  60.         username: req.body.username,
  61.         password: req.body.password,
  62.         registerTime: req.body.registerTime,
  63.         active: true,
  64.         }
  65.     console.log('registering user: \n', userInfo);
  66.     User.register(new User(userInfo), userInfo.password)
  67.       .then(result => {
  68.         req.flash('okMessage', 'You are now registered');
  69.         req.flash('username', result.username);
  70.         res.redirect('/users');
  71.       })
  72.       .catch(err => {
  73.         req.flash('errors',err.message);
  74.         req.flash('username', req.body.username);
  75.         req.flash('email', req.body.email);
  76.         res.redirect('/users/register');
  77.       });
  78.     }
  79. });
  80.  
  81. // user login and authentication
  82. usersRouter.get('/login', function(req, res, next) {
  83.   res.render('login',{
  84.      loginForm: 'Login',
  85.      errors: req.flash('errors'),
  86.    });
  87. });
  88.  
  89.  
  90. // usersRouter.post('/login',
  91. //   passport.authenticate('local', {failureRedirect: '/users/login', } ),
  92. //   function(req, res, next) {
  93. //     //If Local Strategy Comes True
  94. //      console.log('Authentication Successful');
  95. //     req.flash('okMessage','You are Logged In');
  96. //     req.flash('username',req.user.username);
  97. //     res.redirect('/admin/console');
  98. // });
  99.  
  100. usersRouter.post('/login', function(req, res, next){
  101.   passport.authenticate('local', function(err, user, info){
  102.     if (err) {
  103.       req.flash('errors', err);
  104.       return res.redirect('/users/login');
  105.     }
  106.     if (!user) {
  107.       req.flash('errors', 'invalid user or password');
  108.       return res.redirect('/users/login');
  109.     }
  110.     req.logIn(user, function(err){
  111.       if (err) {
  112.         req.flash('errors', err.toString());
  113.         return res.redirect('/users/login');
  114.       }
  115.       req.flash('okMessage', 'You are now logged in');
  116.       req.flash('username', req.user.username);
  117.       return res.redirect('/admin/console');
  118.     });
  119.   })(req, res, next);
  120. });
  121.  
  122.  
  123. // user logout
  124. usersRouter.get('/logout', function(req,res,next) {
  125.     req.logout();
  126.     req.flash('okMessage','You have logged out');
  127.     res.redirect('/');
  128. });
  129.  
  130.  
  131.  
  132. module.exports = usersRouter;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top