{"size": 2186, "ext": "js", "max_stars_repo_path": "backend/controllers/user.controller.js", "max_stars_repo_name": "felipedasilva/openjobs", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "backend/controllers/user.controller.js", "max_issues_repo_name": "felipedasilva/openjobs", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "backend/controllers/user.controller.js", "max_forks_repo_name": "felipedasilva/openjobs", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.9452054795, "max_line_length": 91, "alphanum_fraction": 0.4743824337}
'use strict';

const jwt     = require('jsonwebtoken'),
      _       = require('lodash'),
      User    = require('../models/user'),
      config  = require('../config');

const createToken = (user) => {
    return jwt.sign(_.omit(user, 'password'), config.secret, { expiresInMinutes: 60 * 5 });
};

const login = (req, res) => {
    if (!req.body.email || !req.body.password) {
        return res.status(400).send('Email e senha obrigratórios');
    }
    let query = { email: req.body.email };
    User.findOne(query, (err, user) => {
        if (err) {
            res.status(400).send("Error: " + JSON.stringify(err));
        } else if (!user) {
            res.status(400).send("Credenciais inválidas.");
        } else {
            user.comparePassword(req.body.password, (err, isMatch) => {
                if (isMatch && !err) {
                    res.status(201).send({
                        id_token: createToken(user)
                    });
                } else
                    res.status(400).send("Credenciais inválidas.");
            });
        }
    });
};

const register = (req, res) => {
    let newUser     = new User(_.pick(req.body, ['name', 'email', 'phone', 'password']));
    newUser.enabled = true;
    newUser.role    = ['user'];

    let errForm = newUser.validateSync();
    if(errForm){
        console.log(errForm);
        return res.status(400).send("Formulário inválido.");
    }
    
    let query = { $or : [
        { email: newUser.email },
        { phone: newUser.phone }
    ] };
    User.findOne(query, (err, user) => {
        if(err) {
            res.status(400).send("Ocorreu um erro.");
        } else if (user) {
            console.log(user);
            res.status(400).send("Email ou Celular já utilizado.");
        } else {
            newUser.save((err) => {
                if(err) {
                    consoe.log(err);
                    res.status(400).send("Ocorreu um erro.");
                }else 
                    res.status(201).json({
                        id_token: createToken(newUser)
                    });    
            });
        }
    });
};

module.exports = {
    register,
    login  
};