]> PHS Git Server - phs-api.git/commitdiff
Cleaning up auth controller and routes.
authorcharleswrayjr <charleswrayjr@gmail.com>
Tue, 16 Sep 2025 19:04:35 +0000 (14:04 -0500)
committercharleswrayjr <charleswrayjr@gmail.com>
Tue, 16 Sep 2025 19:04:35 +0000 (14:04 -0500)
src/controllers/auth.controller.js

index a82fc86b681e25376538d40f9038ae83ca9a68b7..6f50eabc34814f3477969cf15e189bfa47abf063 100644 (file)
@@ -2,91 +2,34 @@
  * @file Authentication controller for handling authentication requests
  */
 
-const db = require('../models');
-const createError = require('http-errors');
+const db = require( '../models' );
+const createError = require( 'http-errors' );
 
-const updateUserAndReturn = async (validUser, res) => {
+const updateUserAndReturn = async ( validUser, res ) => {
   const token = await validUser.createToken();
-  res.status(200).send({ success: true, user: validUser.to_safe_json(), token });
+  res.status( 200 ).send( { success:true, user:validUser.to_safe_json(), token } );
 };
 
 module.exports = {
-  create: async (req, res, next) => {
-    const { user_id, password, password_confirmation } = req.body;
-    if (!user_id || !password || !password_confirmation || password !== password_confirmation) {
-      return next(createError(400, 'Invalid parameters: user_id, password, and password_confirmation are required and passwords must match.'));
-    }
-    try {
-      const user = await db.user.find_one({ id: user_id });
-      if (!user) {
-        return next(createError(400, 'There is no user with this id.'));
-      }
-      const existing = await db.authentication.find_by_user_id(user_id);
-      if (existing) {
-        return next(createError(400, 'Authentication for this user already exists.'));
-      }
-      await user.hashPassword(password);
-      const auth = await db.authentication.find_by_user_id(user_id);
-      res.status(200).send(auth.to_safe_json());
-    } catch (e) {
-      logger.error(`Create auth error: ${e.message}`);
-      next(e);
-    }
-  },
-
-  update: async (req, res, next) => {
-    try {
-      const auth = await db.authentication.find_one(req.params);
-      if (!auth) return next(createError(404, 'Authentication record not found'));
-      const updated = await auth.update(req.body);
-      res.status(200).send(updated.to_safe_json());
-    } catch (e) {
-      logger.error(`Update auth error: ${e.message}`);
-      next(createError(400, 'Invalid parameters.'));
-    }
-  },
-
-  show: async (req, res, next) => {
-    try {
-      const auth = await db.authentication.find_one(req.params);
-      if (!auth) return next(createError(404, 'Authentication record not found'));
-      res.status(200).send(auth.to_safe_json());
-    } catch (e) {
-      logger.error(`Show auth error: ${e.message}`);
-      next(e);
-    }
-  },
-
-  index: async (req, res, next) => {
-    try {
-      const auths = await db.authentication.find_many(req.query);
-      if (!auths.length) return next(createError(404, 'No authentication records found'));
-      res.status(200).send(auths.map(a => a.to_safe_json()));
-    } catch (e) {
-      logger.error(`Index auth error: ${e.message}`);
-      next(e);
-    }
-  },
-
-  authenticate: async (req, res, next) => {
+  authenticate:async ( req, res, next ) => {
     try {
       const { email, password } = req.body;
       if (!email || !password) {
-        return next(createError(400, !email ? 'You must provide an email to login.' : 'You must provide a password to login.'));
+        return next( createError( 400, !email ? 'You must provide an email to login.' : 'You must provide a password to login.' ) );
       }
-      const user = await db.user.find_one( { email });
+      const user = await db.user.find_one( { email } );
       if (!user || !user.is_active || user.is_deleted) {
-        return res.status(401).send({ success: false, user: null, token: null });
+        return res.status( 401 ).send( { success:false, user:null, token:null } );
       }
-      const isValid = await user.comparePassword(password);
+      const isValid = await user.comparePassword( password );
       if (!isValid) {
         await user.failLogin();
-        return res.status(401).send({ success: false, user: null, token: null });
+        return res.status( 401 ).send( { success:false, user:null, token:null } );
       }
-      return updateUserAndReturn(user, res);
+      return updateUserAndReturn( user, res );
     } catch (e) {
-      logger.error(`Authenticate error: ${e.message}`);
-      next(e);
+      logger.error( `Authenticate error: ${ e.message }` );
+      next( e );
     }
   },
 };
\ No newline at end of file