From 3d219d068fb24a59c2c649afbbe23b636f8531c8 Mon Sep 17 00:00:00 2001 From: Gabriel Matte Date: Mon, 7 Oct 2024 20:13:15 -0400 Subject: [PATCH] Move auth login to simpleauth Co-authored-by: roesnerb --- server/auth/modules/simpleauth.js | 134 ++++++++++++++++++++++++++++++ server/controllers/users.js | 104 ----------------------- server/routers/users.js | 4 - 3 files changed, 134 insertions(+), 108 deletions(-) create mode 100644 server/auth/modules/simpleauth.js diff --git a/server/auth/modules/simpleauth.js b/server/auth/modules/simpleauth.js new file mode 100644 index 0000000..56f7b93 --- /dev/null +++ b/server/auth/modules/simpleauth.js @@ -0,0 +1,134 @@ +var authprovider = require('../../models/authProvider.js') +var users = require('../../app.js') +const jwt = require('../../middleware/jwtToken.js'); +const emailer = require('../../config/email.js'); + +const model = require('../../models/users.js'); +const AppError = require('../../middleware/AppError.js'); +const { MISSING_REQUIRED_PARAMETER, LOGIN_CREDENTIALS_ERROR, GENERATE_PASSWORD_ERROR, UPDATE_PASSWORD_ERROR, DELETE_USER_ERROR } = require('../../constants/errorCodes'); + +class SimpleAuth{ + constructor(authmanager,settings){ + this.authmanager = authmanager + this.providers = settings + this.endpoint = "/api/users" + } + + async registerAuth(expressapp){ + try{ + expressapp.post(`${this.endpoint}/register`, this.register); + expressapp.post(`${this.endpoint}/login`, this.authenticate); + expressapp.post(`${this.endpoint}/reset-password`, this.resetPassword); + expressapp.post(`${this.endpoint}/change-password`, jwt.authenticate, this.changePassword); + } catch(error){ + console.error(`La connexion ${name} de type ${provider.type} n'as pu être chargé.`) + } + } + + async register(req, res, next) { + try { + const { email, password } = req.body; + + if (!email || !password) { + throw new AppError(MISSING_REQUIRED_PARAMETER); + } + + await model.register(email, password); + + emailer.registerConfirmation(email) + + return res.status(200).json({ + message: 'Utilisateur créé avec succès.' + }); + + } + catch (error) { + return next(error); + } + } + + async authenticate(req, res, next) { + try { + const { email, password } = req.body; + + if (!email || !password) { + throw new AppError(MISSING_REQUIRED_PARAMETER); + } + + const user = await model.login(email, password); + + if (!user) { + throw new AppError(LOGIN_CREDENTIALS_ERROR); + } + + const token = jwt.create(user.email, user._id); + + return res.status(200).json({ + token: token, + id: user.email + }); + + } + catch (error) { + return next(error); + } + } + + async resetPassword(req, res, next) { + try { + const { email } = req.body; + + if (!email) { + throw new AppError(MISSING_REQUIRED_PARAMETER); + } + + const newPassword = await model.resetPassword(email); + + if (!newPassword) { + throw new AppError(GENERATE_PASSWORD_ERROR); + } + + emailer.newPasswordConfirmation(email, newPassword); + + return res.status(200).json({ + message: 'Nouveau mot de passe envoyé par courriel.' + }); + } + catch (error) { + return next(error); + } + } + + async changePassword(req, res, next) { + try { + const { email, oldPassword, newPassword } = req.body; + + if (!email || !oldPassword || !newPassword) { + throw new AppError(MISSING_REQUIRED_PARAMETER); + } + + // verify creds first + const user = await model.login(email, oldPassword); + + if (!user) { + throw new AppError(LOGIN_CREDENTIALS_ERROR); + } + + const password = await model.changePassword(email, newPassword) + + if (!password) { + throw new AppError(UPDATE_PASSWORD_ERROR); + } + + return res.status(200).json({ + message: 'Mot de passe changé avec succès.' + }); + } + catch (error) { + return next(error); + } + } + +} + +module.exports = SimpleAuth; \ No newline at end of file diff --git a/server/controllers/users.js b/server/controllers/users.js index 4494f1d..097fcdc 100644 --- a/server/controllers/users.js +++ b/server/controllers/users.js @@ -7,110 +7,6 @@ const { MISSING_REQUIRED_PARAMETER, LOGIN_CREDENTIALS_ERROR, GENERATE_PASSWORD_E class UsersController { - async register(req, res, next) { - try { - const { email, password } = req.body; - - if (!email || !password) { - throw new AppError(MISSING_REQUIRED_PARAMETER); - } - - await model.register(email, password); - - emailer.registerConfirmation(email) - - return res.status(200).json({ - message: 'Utilisateur créé avec succès.' - }); - - } - catch (error) { - return next(error); - } - } - - async login(req, res, next) { - try { - const { email, password } = req.body; - - if (!email || !password) { - throw new AppError(MISSING_REQUIRED_PARAMETER); - } - - const user = await model.login(email, password); - - if (!user) { - throw new AppError(LOGIN_CREDENTIALS_ERROR); - } - - const token = jwt.create(user.email, user._id); - - return res.status(200).json({ - token: token, - id: user.email - }); - - } - catch (error) { - return next(error); - } - } - - async resetPassword(req, res, next) { - try { - const { email } = req.body; - - if (!email) { - throw new AppError(MISSING_REQUIRED_PARAMETER); - } - - const newPassword = await model.resetPassword(email); - - if (!newPassword) { - throw new AppError(GENERATE_PASSWORD_ERROR); - } - - emailer.newPasswordConfirmation(email, newPassword); - - return res.status(200).json({ - message: 'Nouveau mot de passe envoyé par courriel.' - }); - } - catch (error) { - return next(error); - } - } - - async changePassword(req, res, next) { - try { - const { email, oldPassword, newPassword } = req.body; - - if (!email || !oldPassword || !newPassword) { - throw new AppError(MISSING_REQUIRED_PARAMETER); - } - - // verify creds first - const user = await model.login(email, oldPassword); - - if (!user) { - throw new AppError(LOGIN_CREDENTIALS_ERROR); - } - - const password = await model.changePassword(email, newPassword) - - if (!password) { - throw new AppError(UPDATE_PASSWORD_ERROR); - } - - return res.status(200).json({ - message: 'Mot de passe changé avec succès.' - }); - } - catch (error) { - return next(error); - } - } - async delete(req, res, next) { try { const { email, password } = req.body; diff --git a/server/routers/users.js b/server/routers/users.js index 4e43ca5..1ab9e4b 100644 --- a/server/routers/users.js +++ b/server/routers/users.js @@ -4,10 +4,6 @@ const router = express.Router(); const jwt = require('../middleware/jwtToken.js'); const usersController = require('../controllers/users.js') -router.post("/register", usersController.register); -router.post("/login", usersController.login); -router.post("/reset-password", usersController.resetPassword); -router.post("/change-password", jwt.authenticate, usersController.changePassword); router.post("/delete-user", jwt.authenticate, usersController.delete); module.exports = router; \ No newline at end of file