From: charleswrayjr Date: Fri, 12 Sep 2025 03:28:24 +0000 (-0500) Subject: Adding authentication routes and controllers. X-Git-Url: https://git.phasecustomsoft.com/static/git-logo.png?a=commitdiff_plain;h=af66b9dff0fc5884f5b9915d78966ac850983be8;p=phs-api.git Adding authentication routes and controllers. --- diff --git a/src/controllers/authentication.controller.js b/src/controllers/authentication.controller.js new file mode 100644 index 0000000..5838010 --- /dev/null +++ b/src/controllers/authentication.controller.js @@ -0,0 +1,160 @@ +/** + * @file Authentication controller for handling authentication-related API requests + */ + +const db = require( '../models' ); +const createError = require( 'http-errors' ); + +/** + * Authentication controller + * @type {Object} + */ +module.exports = { + /** + * Create a new authentication record + * @param {Object} req - Express request object + * @param {Object} res - Express response object + * @param {Function} next - Express next middleware function + * @returns {Promise} + */ + async create( req, res, next ) { + try { + const auth_data = req.body; + const auth = await db.authentication.create( auth_data ); + res.json( auth.to_safe_json() ); + } catch (error) { + logger.error( `Create authentication error: ${ error.message }` ); + next( createError( error.status || 400, error.message ) ); + } + }, + + /** + * Find authentication record by user ID + * @param {Object} req - Express request object + * @param {Object} res - Express response object + * @param {Function} next - Express next middleware function + * @returns {Promise} + */ + async find_by_user_id( req, res, next ) { + try { + const { user_id } = req.params; + const auth = await db.authentication.find_by_user_id( parseInt( user_id ) ); + if (!auth) return next( createError( 404, 'Authentication record not found' ) ); + res.json( auth.to_safe_json() ); + } catch (error) { + logger.error( `Find authentication by user ID error: ${ error.message }` ); + next( createError( error.status || 500, error.message ) ); + } + }, + + /** + * Find authentication record by reset token + * @param {Object} req - Express request object + * @param {Object} res - Express response object + * @param {Function} next - Express next middleware function + * @returns {Promise} + */ + async find_by_reset_token( req, res, next ) { + try { + const { token } = req.params; + const auth = await db.authentication.find_by_reset_token( token ); + if (!auth) return next( createError( 404, 'Authentication record not found' ) ); + res.json( auth.to_safe_json() ); + } catch (error) { + logger.error( `Find authentication by reset token error: ${ error.message }` ); + next( createError( error.status || 500, error.message ) ); + } + }, + + /** + * Find one authentication record by ID + * @param {Object} req - Express request object + * @param {Object} res - Express response object + * @param {Function} next - Express next middleware function + * @returns {Promise} + */ + async find_one( req, res, next ) { + try { + const { id } = req.params; + const auth = await db.authentication.find_one( { id:parseInt( id ) } ); + if (!auth) return next( createError( 404, 'Authentication record not found' ) ); + res.json( auth.to_safe_json() ); + } catch (error) { + logger.error( `Find authentication error: ${ error.message }` ); + next( createError( error.status || 500, error.message ) ); + } + }, + + /** + * Find many authentication records + * @param {Object} req - Express request object + * @param {Object} res - Express response object + * @param {Function} next - Express next middleware function + * @returns {Promise} + */ + async find_many( req, res, next ) { + try { + const { limit = '100', offset = '0', ...where } = req.query; + const auths = await db.authentication.find_many( where, [], null, parseInt( limit ), parseInt( offset ) ); + res.json( auths.map( auth => auth.to_safe_json() ) ); + } catch (error) { + logger.error( `Find many authentication records error: ${ error.message }` ); + next( createError( error.status || 500, error.message ) ); + } + }, + + /** + * Lock an authentication account + * @param {Object} req - Express request object + * @param {Object} res - Express response object + * @param {Function} next - Express next middleware function + * @returns {Promise} + */ + async lock_account( req, res, next ) { + try { + const { id } = req.params; + const auth = await db.authentication.lock_account( id ); + res.json( auth.to_safe_json() ); + } catch (error) { + logger.error( `Lock account error: ${ error.message }` ); + next( createError( error.status || 400, error.message ) ); + } + }, + + /** + * Unlock an authentication account + * @param {Object} req - Express request object + * @param {Object} res - Express response object + * @param {Function} next - Express next middleware function + * @returns {Promise} + */ + async unlock_account( req, res, next ) { + try { + const { id } = req.params; + const auth = await db.authentication.unlock_account( id ); + res.json( auth.to_safe_json() ); + } catch (error) { + logger.error( `Unlock account error: ${ error.message }` ); + next( createError( error.status || 400, error.message ) ); + } + }, + + /** + * Soft delete an authentication record + * @param {Object} req - Express request object + * @param {Object} res - Express response object + * @param {Function} next - Express next middleware function + * @returns {Promise} + */ + async soft_delete( req, res, next ) { + try { + const { id } = req.params; + const { deleted_by_id } = req.body; + const auth = await db.authentication.soft_delete( id, deleted_by_id ); + res.json( auth.to_safe_json() ); + } catch (error) { + logger.error( `Soft delete authentication error: ${ error.message }` ); + next( createError( error.status || 400, error.message ) ); + } + } +}; \ No newline at end of file diff --git a/src/routes/authentication.routes.js b/src/routes/authentication.routes.js new file mode 100644 index 0000000..5b78f0b --- /dev/null +++ b/src/routes/authentication.routes.js @@ -0,0 +1,25 @@ +/** + * @file Authentication routes configuration + */ + +const express = require( 'express' ); +const router = express.Router(); +const { validate_auth } = require( '../middleware/routeHelpers' ); +const authentication_controller = require( '../controllers/authentication.controller' ); + +/** + * Configure authentication routes + * @param {Object} passport - Passport instance for authentication + * @returns {Object} Express router with authentication routes + */ +module.exports = ( passport ) => { + router.post( '/create', validate_auth( passport ), authentication_controller.create ); + router.get( '/user/:user_id', validate_auth( passport ), authentication_controller.find_by_user_id ); + router.get( '/reset/:token', validate_auth( passport ), authentication_controller.find_by_reset_token ); + router.get( '/:id', validate_auth( passport ), authentication_controller.find_one ); + router.get( '/', validate_auth( passport ), authentication_controller.find_many ); + router.put( '/:id/lock', validate_auth( passport ), authentication_controller.lock_account ); + router.put( '/:id/unlock', validate_auth( passport ), authentication_controller.unlock_account ); + router.put( '/:id/soft_delete', validate_auth( passport ), authentication_controller.soft_delete ); + return router; +}; \ No newline at end of file