Skip to content

Conversation

@ChingEnLin
Copy link
Owner

This pull request introduces a robust token renewal system for MSAL authentication, improving session persistence and user experience. It adds a service that proactively refreshes authentication tokens at regular intervals and recovers from authentication errors with interactive prompts when needed. Additionally, session persistence is enhanced by storing authentication state in cookies.

Authentication Token Renewal & Error Handling Improvements:

  • Introduced a new tokenRenewalService in frontend/services/tokenRenewalService.ts that periodically refreshes MSAL tokens every 30 minutes and handles silent/interactive renewal flows, ensuring users stay authenticated without manual intervention.
  • Added a useTokenRenewal React hook (frontend/hooks/useTokenRenewal.ts) that automatically starts or stops the token renewal service based on the user's authentication state, and integrated it into the main App component. [1] [2]
  • Improved error handling in getAccessToken (frontend/services/userDataService.ts) to attempt interactive token acquisition via popup if silent renewal fails, using a new utility isRecoverableAuthError for better recovery from authentication issues.

Session Persistence Enhancement:

  • Changed MSAL configuration to store authentication state in cookies (storeAuthStateInCookie: true) for improved session persistence across browser sessions.

Authentication Error Utilities:

  • Added isRecoverableAuthError utility to frontend/utils/authErrorHandler.ts to detect when interactive authentication is required and enable fallback to popup/redirect flows.

ChingEnLin and others added 2 commits October 1, 2025 12:33
Copilot AI review requested due to automatic review settings October 1, 2025 11:38
@ChingEnLin ChingEnLin merged commit 63011c4 into production Oct 1, 2025
2 checks passed
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request introduces a comprehensive token renewal system for MSAL authentication to improve session persistence and user experience. The implementation includes automatic token refresh capabilities, enhanced error recovery, and better session management.

Key changes:

  • Added automated token renewal service that proactively refreshes tokens every 30 minutes
  • Enhanced authentication error handling with interactive fallback for token acquisition failures
  • Improved session persistence by enabling cookie-based authentication state storage

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
frontend/utils/authErrorHandler.ts Adds utility function to identify recoverable authentication errors
frontend/services/userDataService.ts Enhances token acquisition with interactive popup fallback on silent renewal failure
frontend/services/tokenRenewalService.ts Implements comprehensive token renewal service with periodic refresh and error handling
frontend/hooks/useTokenRenewal.ts Provides React hook to automatically manage token renewal based on authentication state
frontend/authConfig.ts Enables cookie-based authentication state storage for better session persistence
frontend/App.tsx Integrates token renewal hook into main application component

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +101 to +105
private isTokenNearExpiry(_account: any): boolean {
// Always attempt renewal for proactive refreshing
// MSAL handles token expiry checks internally, so we'll rely on forceRefresh
return true;
}
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

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

This method always returns true, making it essentially a no-op. Consider either implementing actual token expiry logic or removing this method and directly using the boolean value where it's called.

Copilot uses AI. Check for mistakes.
/**
* Check if the current token is near expiry
*/
private isTokenNearExpiry(_account: any): boolean {
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

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

The parameter is prefixed with underscore but typed as any. Consider using a proper type from MSAL (e.g., AccountInfo) or if the parameter is truly unused, remove it entirely.

Copilot uses AI. Check for mistakes.
@github-actions
Copy link

github-actions bot commented Oct 1, 2025

🎉 This PR is included in version 2.3.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants