aboutsummaryrefslogtreecommitdiff
path: root/server/controllers/user.js
blob: e3c1078e8487340dd0c4c293b31021f0ca4a2743 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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.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(() => res.json({status: "User successfully registered!"}))
        .catch(err => res.json({
          status: "Error when registering user!",
          error: err
        }));
    }
  },

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

      if (!bcrypt.compareSync(req.body.password, user.password))
        res.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
        });
      }
    });
  }

};