Skip to content

Conversation

@WojciechMat
Copy link
Collaborator

@WojciechMat WojciechMat commented Jun 16, 2025

JWT Token Persistence with localStorage

Problem: Users were logged out after every page refresh because JWT tokens were only stored in memory.

Solution:

  • Added localStorage support to persist JWT tokens across browser sessions
  • Implemented automatic token validation and expiry checking on app initialization
  • Enhanced AuthService to load stored tokens on startup and clear expired tokens
  • Added web-only conditional imports to support localStorage functionality

Key Changes:

  • AuthService: Added token persistence methods with automatic loading/clearing
  • ApiClient: Enhanced to use stored tokens and sync with localStorage
  • UserModel: Added UserModel class and UserRole enum
  • main.dart: Added initialization flow to wait for token loading
  • jwt_decoder.dart: Added token expiry validation utilities

Benefits:

  • Users stay logged in after page refresh
  • Automatic cleanup of expired tokens
  • Better user experience with persistent authentication
  • Web-safe implementation with fallback for non-web platforms

@WojciechMat WojciechMat changed the base branch from main to dev June 16, 2025 10:30
@WojciechMat WojciechMat force-pushed the WojciechMat/token-persistance branch from 7f8bf59 to 92fc905 Compare June 16, 2025 10:50
@WojciechMat WojciechMat force-pushed the WojciechMat/token-persistance branch from 39a4b67 to b5ccbe1 Compare June 16, 2025 11:09
@WojciechMat WojciechMat requested review from KwiatkowskiML and removed request for KwiatkowskiML June 16, 2025 11:28
@WojciechMat WojciechMat force-pushed the WojciechMat/token-persistance branch from 65fe2e8 to eca5ccf Compare June 16, 2025 11:33
@WojciechMat WojciechMat merged commit 273298f into dev Jun 16, 2025
2 checks passed
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.

2 participants