aboutsummaryrefslogblamecommitdiff
path: root/controllers/user.js
blob: 3f7b4a1afa9581da4e41de7c95d5accd19cec198 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
                                       
                                   
 


                      




                                                         
                                                    









                                                         

    
                    
                 



                          




                            


                       




                           


   
const User = require('../models/user');
const bcrypt = require('bcryptjs');

module.exports = {

  register(req, res) {
    if (req.body.password === req.body.confirmPassword) {
      const newUser = new User({
        firstname: req.body.firstname,
        lastname: req.body.lastname,
        email: req.body.email,
        password: bcrypt.hashSync(req.body.password)
      });
      newUser.save().then(() => {
        req.flash('msg', 'You successfully registered.');
        res.redirect('/login');
      });
    }
    else {
      req.flash('msg', 'Validation errors...');
      res.redirect('/register');
    }
  },

  logout(req, res) {
    req.logout();
    res.redirect('/');
  },

  registerView(req, res) {
    res.render('register', {
      title: 'Register',
      register: true,
      msg: req.flash('msg')
    });
  },

  loginView(req, res) {
    res.render('login', {
      title: 'Login',
      login: true,
      msg: req.flash('msg')
    });
  }

};