ai-cfia / membrane-backend

Membrane Backend: A centralized authentication service for Single Sign-On (SSO) enabling seamless token-based email verification across multiple client applications.
MIT License
1 stars 0 forks source link

Refactor authenticate Route for Improved Maintainability #22

Closed CFIALeronB closed 1 year ago

CFIALeronB commented 1 year ago

Description:

The existing authenticate function in the app contains a complex set of logic that handles various states of authentication, which makes the function lengthy and hard to follow. To improve the readability, maintainability, and testability of the code, we've decided to split the function into multiple smaller functions, each dedicated to a specific state or logic.

Changes Made:

  1. Extracted the logic for each state (INITIAL_STATE, AWAITING_EMAIL, and EMAIL_SENT) into its own function.
  2. Moved JWT-related utility functions, such as encode_email_token, decode_jwt_token, and others, to a new jwt_utils.py file to separate the concerns and improve the organization of the code.
  3. Added specific exceptions for JWT and authentication errors to improve error handling. This will also aid in providing better error responses to the clients.

Benefits:

  1. Readability: By splitting the monolithic function, each piece of logic is now encapsulated in its function, making the code easier to read.
  2. Maintainability: With smaller, focused functions, it's simpler to maintain and update the logic for each authentication state.
  3. Testability: Smaller functions allow for more straightforward unit testing, enabling better coverage and easier bug identification.

Next Steps:

  1. Add unit tests for each of the newly created functions to ensure they work as intended.
  2. Update the documentation to reflect the new structure and function responsibilities.
  3. Continuously monitor and test the authentication flow in staging to ensure there are no regressions.