{"size": 2579, "ext": "js", "max_stars_repo_path": "api/controllers/userControllers.js", "max_stars_repo_name": "Nathanlauga/blockchain-hyperledger-fabric", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "api/controllers/userControllers.js", "max_issues_repo_name": "Nathanlauga/blockchain-hyperledger-fabric", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "api/controllers/userControllers.js", "max_forks_repo_name": "Nathanlauga/blockchain-hyperledger-fabric", "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": 38.4925373134, "max_line_length": 111, "alphanum_fraction": 0.4881737107}
// Dependencies
const jwt = require('jsonwebtoken');
const config = require('../config/index');

// Models
const User = require('../models/user');

module.exports = {
    signup: async (req, res) => {
        try {
            const timestamp = new Date().getTime();
            if (req.body.password !== req.body.confirm_password) {
                return res.status(422).json({ message: 'Passwords must match' });
            }
            const user = await User.create(req.body);
            const token = jwt.sign({ sub: user._id, iat: timestamp }, config.secret, {
                expiresIn: '3600',
            });
            return res.status(201).json({ user, token: `JWT ${token}`, message: 'Successful signin' });
        } catch (error) {
            console.log(error);
            return res.status(500).json({ message: 'Error with server' });
        }
    },
    login: async (req, res) => {
        try {
            const timestamp = new Date().getTime();
            const user = await User.findOne({
                $or: [
                    { email: req.body.login.toLowerCase() },
                    { username: req.body.login.toLowerCase() },
                ],
            });
            if (!user) {
                return res.status(422).json({
                    message: "L'email, ou le nom d'utilisateur n'existe pas",
                });
            }
            user.comparePassword(req.body.password, (err, isMatch) => {
                if (err && !isMatch) {
                    return res.status(422).json({ message: 'La connexion a échoué. Mot de passe incorrect.' });
                }
                // Create token if the password matched and no error was thrown
                const token = jwt.sign({ sub: user._id, iat: timestamp }, config.secret, {
                    expiresIn: '1h', // in seconds
                });
                return res.status(200).json({ message: 'Authentication sucessful', token: `JWT ${token}` });
            });
        } catch (error) {
            console.log(error);
            return res.status(500).json({ message: 'Error with server' });
        }
    },
    retrieve: async (req, res) => {
        try {
            const user = await User.findById(req.params.userId);
            if (!user) {
                return res.status(404).json({ message: 'User not found' });
            }
            return res.status(200).json({ user });
        } catch (error) {
            console.log(error);
            return res.status(500).json({ message: 'Error with server' });
        }
    },
};
