Actions
dev #2247
opendev #2190: [Module] Sprint - 02
[FE] Basic authentication and middleware handling
Start date:
04/09/2025
Due date:
04/11/2025 (about 12 months late)
% Done:
100%
Estimated time:
8:00 h
Spent time:
Description
Description:
Overview¶
The frontend application requires proper basic authentication handling and middleware integration to ensure secure navigation and form submission workflows. This issue addresses the implementation of authentication checks and middleware logic in the Navbar and MultiStepForm components.
Problem¶
- The
Navbarcurrently displays authentication states (Sign In/Sign Up or Sign Out) but lacks middleware to validate user sessions consistently on page reload or navigation. - The
MultiStepFormfetches user data viauseSWRand checks authentication withcheckAuth, but there’s no centralized middleware to handle redirects or session expiration gracefully. - On reload, form fields (e.g.,
toolName) may not populate correctly due to timing issues with authentication and data fetching.
Tasks¶
-
Navbar Authentication Handling
- Add middleware to validate the user session on each navigation or page load using
useUserAuth. - Ensure
isAuthenticatedanduserInfoare updated in real-time, avoiding stale states during loading. - Fix skeleton loading to persist only during initial auth checks, not on subsequent updates.
- Add middleware to validate the user session on each navigation or page load using
-
Middleware Integration
- Implement a reusable middleware function (e.g.,
authMiddleware) to:- Verify session validity via an API call (e.g.,
/api/auth/check). - Redirect to
/auth/signinif the session is invalid or expired, preserving the callback URL.
- Verify session validity via an API call (e.g.,
- Apply this middleware to protect routes like
/submit-aiand ensure seamless redirects.
- Implement a reusable middleware function (e.g.,
-
MultiStepForm Fixes
- Synchronize form initialization with authentication state, ensuring
latestSubmissiondata (e.g.,toolName) loads only after auth is confirmed. - Adjust
useSWRto wait forisAuthenticatedbefore fetching, preventing stale Redux state from overriding form values. - Update
form.resetlogic to conditionally load step-specific fields based onlatestSubmission.stepCompleted(Step 1, Step 2, or all from Redux).
- Synchronize form initialization with authentication state, ensuring
Acceptance Criteria¶
-
Navbarshows correct buttons (Sign In/Sign Up or Sign Out) based on real-time auth state, with no flicker on reload. -
Middleware redirects unauthenticated users to
/auth/signinwith a callback URL (e.g.,/submit-ai). -
MultiStepFormloadstoolNameand other fields correctly on reload whenlatestSubmissionexists, respectingstepCompletedlogic:- Step 1: Only Step 1 fields from
latestSubmission, rest from Redux. - Step 2: Step 1 and 2 fields from
latestSubmission, rest from Redux. - Step 3+: All fields from Redux.
- Step 1: Only Step 1 fields from
- No console errors related to authentication or data fetching.
- Skeleton loading appears only during initial auth/data fetch, not on subsequent updates.
Steps to Reproduce¶
- Open the app and navigate to
/submit-ai. - Reload the page while authenticated with a
latestSubmissioncontainingtoolName. - Observe
toolNamemissing in the form and inconsistentNavbarbutton states.
Technical Notes¶
- Use
useUserAuth’scheckAuthandexecuteWithAuthfor session validation. - Leverage
useSWR’sisLoadinganderrorstates to manage UI updates. - Persist Redux state with
localStorageto maintaintoolStateacross reloads (if not already implemented). - Test with edge cases: logged out, session expired, and partial form submissions.
Actions