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









                                                       
                                                                








                                                    



                                                                        
                                            








                                                          
                                                                      
 


                                                                     
                                                                
                                                             




                                                                      

                             





           
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');

const User = require('../models/User');
const {masterKey} = require("../config/env");

module.exports = {

  register(req, res) {
    if (req.body.password !== req.body.confirmPassword)
      res.status(400).json({status: "Passwords do not match!"});
    else {
      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(user => res.json({
          status: "User successfully registered!",
          token: jwt.sign({sub: user._id}, masterKey, {expiresIn: "1d"})
        }))
        .catch(err => res.status(400).json({
          status: "Error when registering user!",
          error: err
        }));
    }
  },

  login(req, res) {
    User.findOne({email: req.body.email}, (err, user) => {
      if (err)
        res.status(500).json({status: "Database error.", error: err});

      if (!user)
        return res.status(404).json({status: "No such user found!"});

      if (!bcrypt.compareSync(req.body.password, user.password))
        res.status(401).json({status: "Wrong credentials!"});
      else {
        const payload = {sub: user._id};
        const token = jwt.sign(payload, masterKey, {expiresIn: "1d"});
        res.json({
          status: "Successfully logged in!",
          token,
          isAdmin: user.admin
        });
      }
    });
  }

};