You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4 KiB

const UserModel = require('../models/user.model');
const jwt = require('jsonwebtoken');
const { signUpErrors, signInErrors } = require('../utils/errors.utils');
const maxAge = 3 * 24 * 60 * 60 * 1000;
//expiresIn temps avant l'expiration du token
const createToken = (id) => {
return jwt.sign({id}, process.env.TOKEN_SECRET, {
expiresIn: maxAge
})
}
//gestion des erreurs
module.exports.signUp = async (req, res) => {
console.log(req.body);
//Attention à enlever
const {pseudo, email, password} = req.body
try {
const user = await UserModel.create({pseudo, email, password});
res.status(201).json({ user: user._id});
}
catch(err) {
const errors = signUpErrors(err);
res.status(200).send({ errors })
}
}
module.exports.signIn = async (req, res) => {
console.log(req.body);
const {email, password} = req.body
try {
const user = await UserModel.login({email, password});
//creation d'un token
const token = createToken(user._id);
res.cookie('jwt', token, { httpOnly: true, maxAge});
res.status(200).json({ user: user._id});
}
catch(err) {
const errors = signInErrors(err);
res.status(200).send({ errors });
}
}
module.exports.logout = async (req, res) => {
res.cookie('jwt', '', { maxAge: 1});
res.redirect('/');
}