MusicTopus/src/middleware/Auth.js

45 lines
1 KiB
JavaScript
Raw Normal View History

2022-02-13 17:59:42 +01:00
import Pages from "./Pages";
import Users from "../models/users";
/**
* Classe permettant la gestion des utilisateurs
*/
class Auth extends Pages {
/**
* Méthode permettant de créer un nouvel utilisateur
* @param {Req} req
*
* @return {Object}
*/
static async register(req) {
try {
const { username, email, password } = req.body;
const user = new Users({
username,
email,
salt: password,
});
const resUser = await user.save();
await new Promise((resolve, reject) => {
req.login(resUser, (errLogin) => {
if (errLogin) {
return reject(errLogin);
}
return resolve(null);
});
});
return resUser;
} catch (err) {
req.flash("error", err.toString());
throw err;
}
}
}
export default Auth;