Skip to content

Fix access token not persisting on hard reload - #11

Merged
ibadia merged 13 commits into
ibadia:mainfrom
MuhammadSadiqAli-EQ:add-login-signup-healthcheck-pages
Aug 18, 2026
Merged

Fix access token not persisting on hard reload#11
ibadia merged 13 commits into
ibadia:mainfrom
MuhammadSadiqAli-EQ:add-login-signup-healthcheck-pages

Conversation

@MuhammadSadiqAli-EQ

Copy link
Copy Markdown
Contributor

Problem

Hard reload was logging the user out even when refresh token cookie was still valid. In-memory access token gets wiped on reload, but the code never tried the refresh token as fallback.

Fix

  • Added getValidAccessToken() in lib/auth.ts, checks memory first, falls back to refreshAccessToken() if empty
  • Added ProtectedLayout (loader-based route guard) for the new /auth page, redirects to /login if no valid token found

Testing

  • Confirmed /auth redirects to /login when no refresh cookie present (tested by manually deleting it from web devtool)
  • Confirmed hard reload on /auth keeps user logged in when refresh cookie is valid
  • Confirmed manually deleting memoryRefreshToken cookie triggers redirect correctly

Fixes #8

MuhammadSadiqAli-EQ and others added 11 commits July 27, 2026 20:52
- Add LoginForm, SignupForm, HealthCheck components
- Add CenteredPageLayout and TextField reusable UI
- Add lib/api.ts, auth.ts, config.ts, health.ts, types.ts
- Use clientAction for login/signup, clientLoader for health check
- Update routes to use React Router data APIs
Removed docstring explaining public access for HealthCheckAPI and simplified response structure.
Refactor authentication views to remove AuthenticationService calls and directly handle user authentication and password management. Update token handling and response structure for login and signup endpoints.
@ibadia

ibadia commented Aug 14, 2026

Copy link
Copy Markdown
Owner

@redsteadz
can you please review this as this is purely frontend

Comment thread frontend/app/lib/auth.ts
* Returns a valid access token, trying memory first then refresh token.
* Used by route loaders to check auth status.
*/
export async function getValidAccessToken(): Promise<string | null> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a valid access token means a token that is actually functioning, this function is only checking whether the token is present or no

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try checking if the token is actually valid through a quick backend call

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a small function called isTokenExpired that decodes the JWT and compares the exp field to the current time. This gives us the same result as calling the backend would, but we skip the extra network call on every route load, so it's faster. Backend still does its own validation on real API calls, so nothing is less secure, we're just avoiding an unnecessary check upfront.

@redsteadz

Copy link
Copy Markdown
Collaborator

approved

@ibadia

ibadia commented Aug 18, 2026

Copy link
Copy Markdown
Owner

Merging it.

@ibadia
ibadia merged commit b22b06c into ibadia:main Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

frontend: Session storage fix for auth make sure that cookie base auth is working correctly

4 participants