Skip to content
Snippets Groups Projects
user.js 3.18 KiB
Newer Older
const express = require('express');

const userApp = express();

const libs = `${process.cwd()}/libs`;

const config = require(`${libs}/config`);

const log = require(`${libs}/log`)(module);

const User = require(`${libs}/models/user`);
const jwt = require('jwt-simple');

const required_fields = ["email", "password", "name", "cpf", "schooling", "segment", "role", "institution_name", "state", "city"];
function emailSyntax(email) {
Lucas Gabriel Lima's avatar
Lucas Gabriel Lima committed
    const regex = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
    return regex.test(email);
Lucas Gabriel Lima's avatar
Lucas Gabriel Lima committed
userApp.post('/', (req, res, next) => {
    if (!req.body.email) {
        res.json({success: false, msg: 'Please pass email.'});
    } else {
        next();
    }

}, (req, res, next) => {
    if (!req.body.password) {
        res.json({success: false, msg: 'Please pass password.'});
Lucas Gabriel Lima's avatar
Lucas Gabriel Lima committed
    } else {
        next();
Lucas Gabriel Lima's avatar
Lucas Gabriel Lima committed
}, (req, res, next) => {
    if(!emailSyntax(req.body.email)){
        res.json({success: false, msg: 'Invalid email syntax.'});
    } else {
        next();
Lucas Gabriel Lima's avatar
Lucas Gabriel Lima committed
    }
}, (req, res, next) => {
    for (let property of required_fields){
        // if(!Object.prototype.hasOwnProperty.call(req.body, property)){
        //     res.json({success: false, msg: 'Please fill out all mandatory fields.'});
        //     return;
        // }
Lucas Gabriel Lima's avatar
Lucas Gabriel Lima committed
}, (req, res, next) => {
    var newUser = new User({
        email: req.body.email,
        password: req.body.password,
        name: req.body.name,
        cpf: req.body.cpf,
        schooling: req.body.schooling,
        course: req.body.course,
        segment: req.body.segment,
        role: req.body.role,
        institution_name: req.body.institution_name,
        state: req.body.state,
        city: req.body.city,
        receive_emails: req.body.receive_emails
Lucas Gabriel Lima's avatar
Lucas Gabriel Lima committed
    });

    // save the user
    newUser.save((err) => {
        if (err) {
            res.json({success: false, msg: 'Email already in use.'});
        } else {
            res.json({success: true, msg: 'Successful created new user.'});
        }
    });
userApp.post('/authenticate', (req, res, next) => {
    if (!req.body.email || !req.body.password) {
        res.json({success: false, msg: 'Please pass email and password.'});
    } else {
        next();
    }

}, (req, res, next) => {
    User.findOne({
        email: req.body.email
    }, (err, user) => {
        if (err) throw err;

        if(!user){
            res.json({success: false, msg: 'Authentication failed. User not found.'});
        }
        else {
            user.comparePassword(req.body.password, (err, isMatch) => {
                if (isMatch && !err) {
                    var secret = config.get('mongodb:secret');

                    // if user is found and password is right create a token
                    var token = jwt.encode(user, secret);

                    //returns user info including token as json
                    res.json({success: true, token: 'JWT ' + token});
                }
                else {
Lucas Gabriel Lima's avatar
Lucas Gabriel Lima committed
                    res.json({success: false, msg: 'Authentication failed. Wrong password'});
module.exports = userApp;