45 lines
1 KiB
JavaScript
45 lines
1 KiB
JavaScript
|
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;
|