{"size": 1603, "ext": "js", "max_stars_repo_path": "controllers/usuario.js", "max_stars_repo_name": "aliaa115/pic-cloud", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "controllers/usuario.js", "max_issues_repo_name": "aliaa115/pic-cloud", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "controllers/usuario.js", "max_forks_repo_name": "aliaa115/pic-cloud", "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": 34.1063829787, "max_line_length": 102, "alphanum_fraction": 0.5689332502}

const passport = require('passport');
const Usuario = require('../models/usuario');
var datos = new Usuario()
var ind = require('../index')
module.exports = usuarioGlobal = new Usuario();
var host = process.env.ADDRESS || 'localhost';
var port = process.env.PORT || 2001;

module.exports.postSignup = (req, res, next) => {
    const nuevoUsuario = new Usuario({
        email: req.body.email,
        nombre: req.body.nombre,
        password: req.body.password,
        edad: req.body.edad
    });

    Usuario.findOne({ email: req.body.email }, (err, usuarioExistente) => {
        if (err) res.status(400).sed({ message: `Error al crear el usuario: ${err}` });
        if (usuarioExistente) return res.status(400).render('./php/noLoged');
        nuevoUsuario.save((err) => {
            if (err) next(err);
            req.logIn(nuevoUsuario, (err) => {
                if (err) return next(err);
                res.status(200).redirect(301, `http://${host}:${port}` + '/perfil/' + req.user.nombre)
            })
        })
    })
}

module.exports.postLogin = (req, res, next) => {
    passport.authenticate('local', (err, usuario, info) => {
        if (err) next(err);
        if (!usuario) return res.status(400).render('./php/noLoged');
        req.logIn(usuario, (err) => {
            if (err) return next(err);
            res.status(200).redirect(301, `http://${host}:${port}` + '/perfil/' + req.user.nombre)
        })
        req.params.email = req.user.email
    })(req, res, next);
}

module.exports.logout = (req, res) => {
    req.logout();
    res.status(200).render('./index')
}
