Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: where pages could not be refreshed when login expired #4204

Merged
merged 1 commit into from
Aug 20, 2024
Merged

Conversation

anncwb
Copy link
Collaborator

@anncwb anncwb commented Aug 20, 2024

Description

fixed #4188

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Checklist

ℹ️ Check all checkboxes - this will indicate that you have done everything in accordance with the rules in CONTRIBUTING.

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs:dev command.
  • Run the tests with pnpm test.
  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Summary by CodeRabbit

  • New Features

    • Extended the access token validity from 2 hours to 1 day, improving user session longevity.
    • Updated login expiration handling to redirect users to a dedicated page instead of displaying a modal dialog.
  • Enhancements

    • Improved access management by implementing a new isAccessChecked property to track access permissions more effectively.
    • Refined logic in access control to minimize premature logouts and enhance user experience during session management.
  • Bug Fixes

    • Adjusted conditions for setting login expiration, ensuring proper access checks are performed before marking sessions as expired.

@anncwb anncwb added the bug Something isn't working label Aug 20, 2024
@anncwb anncwb requested review from vince292007 and a team as code owners August 20, 2024 14:27
Copy link

changeset-bot bot commented Aug 20, 2024

⚠️ No Changeset found

Latest commit: 24e7873

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

coderabbitai bot commented Aug 20, 2024

Walkthrough

The recent updates enhance the authentication and access management system across multiple components. Key changes include extending the validity of access tokens from 2 hours to 1 day, refining the logic that determines login expiration based on access checks, and introducing a new state to track whether access permissions have been verified. These adjustments streamline user session management and improve overall system robustness.

Changes

Files Change Summary
apps/backend-mock/utils/jwt-utils.ts Modified generateAccessToken to extend token expiration from 2 hours to 1 day.
apps/web-antd/src/api/request.ts Enhanced createRequestClient to check both loginExpiredMode and accessStore.isAccessChecked before setting login expiration.
apps/web-antd/src/router/guard.ts Updated setupAccessGuard to rely on accessStore.isAccessChecked for access checks, improving flow and clarity.
apps/web-ele/src/api/request.ts Similar updates to createRequestClient as in web-antd, refining login expiration logic with additional condition.
apps/web-ele/src/router/guard.ts Streamlined access check in setupAccessGuard, removing reliance on accessRoutes.
apps/web-naive/src/api/request.ts Added condition for checking accessStore.isAccessChecked in createRequestClient for login expiration handling.
apps/web-naive/src/router/guard.ts Modified setupAccessGuard to streamline access check logic using isAccessChecked.
docs/src/guide/essentials/server.md Adjusted control flow in createRequestClient to include a condition checking accessStore.isAccessChecked.
packages/@core/preferences/src/config.ts Changed loginExpiredMode from 'modal' to 'page', altering user session expiration handling.
packages/stores/src/modules/access.ts Added isAccessChecked property to AccessState, with a method to update its value, enhancing state management.
playground/src/api/request.ts Refined login expiration logic in createRequestClient by adding an access check condition.
playground/src/router/guard.ts Updated setupAccessGuard to use isAccessChecked instead of accessRoutes for access validation.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant AuthStore
    participant AccessStore

    User->>AuthStore: Login attempt
    AuthStore-->>User: Returns access token
    User->>AccessStore: Check access state
    AccessStore-->>User: Access check completed
    User->>AuthStore: Refresh token if needed
Loading

🐰 In the meadow, where bunnies play,
Tokens now last for a whole day!
Access checks are clearer too,
Making sure we know what to do.
With every hop and joyful cheer,
Our sessions stay longer, never fear! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@anncwb anncwb merged commit 2288827 into main Aug 20, 2024
17 checks passed
@anncwb anncwb deleted the bugfix/#4188 branch August 20, 2024 14:33
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fecf551 and 24e7873.

Files selected for processing (12)
  • apps/backend-mock/utils/jwt-utils.ts (1 hunks)
  • apps/web-antd/src/api/request.ts (1 hunks)
  • apps/web-antd/src/router/guard.ts (2 hunks)
  • apps/web-ele/src/api/request.ts (1 hunks)
  • apps/web-ele/src/router/guard.ts (2 hunks)
  • apps/web-naive/src/api/request.ts (1 hunks)
  • apps/web-naive/src/router/guard.ts (2 hunks)
  • docs/src/guide/essentials/server.md (1 hunks)
  • packages/@core/preferences/src/config.ts (1 hunks)
  • packages/stores/src/modules/access.ts (3 hunks)
  • playground/src/api/request.ts (1 hunks)
  • playground/src/router/guard.ts (3 hunks)
Additional comments not posted (17)
apps/backend-mock/utils/jwt-utils.ts (1)

17-17: Consider the security implications of extending token expiration.

The access token expiration time has been extended from 2 hours to 1 day. While this change improves user convenience by reducing the frequency of re-authentication, it may also increase security risks if tokens are compromised. Ensure that this change aligns with your security policies and consider implementing additional security measures, such as token revocation or monitoring for unusual activity.

packages/stores/src/modules/access.ts (3)

28-28: LGTM! Ensure isAccessChecked is utilized correctly.

The addition of isAccessChecked to track access verification status is a good enhancement. Make sure that this property is correctly used throughout the application to control access-related logic.


56-58: LGTM! Verify the usage of setIsAccessChecked.

The setIsAccessChecked method is a useful addition for updating the access check status. Ensure that it is invoked appropriately in the application flow to maintain accurate state management.


75-75: LGTM! Default state initialization is appropriate.

The default initialization of isAccessChecked to false ensures that access permissions are not assumed to be verified upon store initialization. This is a good practice for ensuring security and correctness in access management.

packages/@core/preferences/src/config.ts (1)

21-21: Consider the impact on user experience when changing loginExpiredMode.

The loginExpiredMode has been changed from 'modal' to 'page'. This shift to redirecting users to a dedicated page can provide a clearer user experience but may also disrupt the user's workflow. Ensure that this change aligns with the overall user experience strategy and consider user feedback on this interaction.

apps/web-naive/src/api/request.ts (1)

33-36: LGTM! Verify the logic for setting loginExpired.

The addition of the isAccessChecked condition ensures that loginExpired is only set when appropriate. This change can help prevent premature session expiration handling. Verify that this logic is correctly integrated into the application flow.

apps/web-antd/src/api/request.ts (1)

34-37: LGTM! Verify the logic for setting loginExpired.

The updated condition for setting loginExpired enhances the robustness of session management. Ensure that this logic is correctly applied and tested within the application to avoid unintended session handling.

playground/src/api/request.ts (1)

34-37: Approved: Refined condition for setting login expired state.

The changes ensure that the login expired state is only set after access checks are completed, preventing premature state changes related to user authentication.

However, verify that the accessStore.isAccessChecked is correctly set in all relevant scenarios.

apps/web-ele/src/api/request.ts (1)

34-37: Approved: Refined condition for setting login expired state.

The changes ensure that the login expired state is only set after access checks are completed, preventing premature state changes related to user authentication.

However, verify that the accessStore.isAccessChecked is correctly set in all relevant scenarios.

Verification successful

Verification Successful: accessStore.isAccessChecked is correctly set in relevant scenarios.

The property isAccessChecked is actively used and set within the codebase, confirming that the condition in the review comment is valid. The code ensures that accessStore.isAccessChecked is managed appropriately.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that `accessStore.isAccessChecked` is correctly set in all relevant scenarios.

# Test: Search for all occurrences of `setIsAccessChecked` to ensure it's set appropriately.
rg --type javascript --word-regexp 'setIsAccessChecked'

Length of output: 90


Script:

#!/bin/bash
# Description: Verify that `accessStore.isAccessChecked` is correctly set in all relevant scenarios.

# Find all JavaScript files and search for `setIsAccessChecked` within them.
fd --extension js --exec rg --word-regexp 'setIsAccessChecked'

Length of output: 62


Script:

#!/bin/bash
# Description: Search for occurrences of `isAccessChecked` to verify its usage and setting.

# Find all JavaScript files and search for `isAccessChecked` within them.
fd --extension js --exec rg --word-regexp 'isAccessChecked'

Length of output: 59


Script:

#!/bin/bash
# Description: Search for occurrences of `accessStore` to understand its structure and usage.

# Find all JavaScript files and search for `accessStore` within them.
fd --extension js --exec rg --word-regexp 'accessStore'

Length of output: 55


Script:

#!/bin/bash
# Description: Search for occurrences of `accessStore` in both JavaScript and TypeScript files to understand its structure and usage.

# Find all JavaScript and TypeScript files and search for `accessStore` within them.
fd --extension js --extension ts --exec rg --word-regexp 'accessStore'

Length of output: 4444

apps/web-naive/src/router/guard.ts (2)

115-115: Approved: Set access check flag after route generation.

Setting isAccessChecked to true after generating access menus and routes ensures subsequent calls recognize completed access checks.


96-96: Approved: Streamlined access check logic.

The changes replace the accessRoutes variable with isAccessChecked, improving clarity and efficiency in access management.

However, verify that accessStore.isAccessChecked is correctly set after access checks are performed.

Verification successful

Verified: accessStore.isAccessChecked is correctly set after access checks.

The method setIsAccessChecked is used to update the isAccessChecked property in several parts of the codebase, confirming its proper implementation.

  • Calls to setIsAccessChecked found in:
    • playground/src/router/guard.ts
    • apps/web-naive/src/router/guard.ts
    • apps/web-ele/src/router/guard.ts
    • apps/web-antd/src/router/guard.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that `accessStore.isAccessChecked` is correctly set after access checks.

# Test: Search for all occurrences of `setIsAccessChecked` to ensure it's set appropriately.
rg --type javascript --word-regexp 'setIsAccessChecked'

Length of output: 90


Script:

#!/bin/bash
# Description: Search for occurrences of `isAccessChecked` to verify where it is set in the codebase.
rg 'isAccessChecked'

Length of output: 949


Script:

#!/bin/bash
# Description: Search for calls to `setIsAccessChecked` to verify its usage in the codebase.
rg 'setIsAccessChecked'

Length of output: 413

playground/src/router/guard.ts (2)

115-115: Approved: Set access check flag after route generation.

Setting isAccessChecked to true after generating access menus and routes ensures subsequent calls recognize completed access checks.


95-95: Approved: Simplified access validation logic.

The changes use isAccessChecked for access validation, enhancing clarity and efficiency.

However, verify that accessStore.isAccessChecked is correctly set after access checks are performed.

Verification successful

Verification Successful: accessStore.isAccessChecked is correctly set.

The setIsAccessChecked(true) method is invoked in playground/src/router/guard.ts, confirming that the isAccessChecked property is set appropriately after access checks. No further action is needed.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that `accessStore.isAccessChecked` is correctly set after access checks.

# Test: Search for all occurrences of `setIsAccessChecked` to ensure it's set appropriately.
rg --type javascript --word-regexp 'setIsAccessChecked'

Length of output: 90


Script:

#!/bin/bash
# Adjust the search to find occurrences of `setIsAccessChecked` without specifying a file type.
rg --word-regexp 'setIsAccessChecked'

Length of output: 427

apps/web-antd/src/router/guard.ts (2)

116-116: Approved: Set access check flag after route generation.

Setting isAccessChecked to true after generating access menus and routes ensures subsequent calls recognize completed access checks.


96-96: Approved: Simplified access validation logic.

The changes use isAccessChecked for access validation, enhancing clarity and efficiency.

However, verify that accessStore.isAccessChecked is correctly set after access checks are performed.

Verification successful

Access Check Confirmation: isAccessChecked is Set Correctly

The isAccessChecked flag is set using setIsAccessChecked(true) in apps/web-antd/src/router/guard.ts, confirming that it is correctly updated after access checks. This ensures that the access validation logic functions as intended.

  • setIsAccessChecked is defined in packages/stores/src/modules/access.ts.
  • It is used in apps/web-antd/src/router/guard.ts and other files, indicating consistent application.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that `accessStore.isAccessChecked` is correctly set after access checks.

# Test: Search for all occurrences of `setIsAccessChecked` to ensure it's set appropriately.
rg --type javascript --word-regexp 'setIsAccessChecked'

Length of output: 90


Script:

#!/bin/bash
# Description: Verify that `accessStore.isAccessChecked` is correctly set after access checks.

# Test: Search for all occurrences of `setIsAccessChecked` to ensure it's set appropriately.
rg 'setIsAccessChecked'

Length of output: 413

apps/web-ele/src/router/guard.ts (1)

Line range hint 96-116: LGTM! Verify isAccessChecked usage throughout the codebase.

The change to use isAccessChecked simplifies the logic and is a good improvement. Ensure that this property is correctly used and updated in all relevant parts of the codebase.

Verification successful

Usage of isAccessChecked is correct and consistent across the codebase.

The setIsAccessChecked method is appropriately called after setting routes and menus, ensuring the access state is updated correctly. No issues found with the current implementation.

  • apps/web-ele/src/router/guard.ts: Properly calls setIsAccessChecked(true) after setting routes and menus.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `isAccessChecked` across the codebase.

# Test: Search for all occurrences of `isAccessChecked`. Expect: Correct usage and updates.
rg --type ts 'isAccessChecked'

Length of output: 889


Script:

#!/bin/bash
# Description: Verify if `setIsAccessChecked` is called after setting routes and menus.

# Test: Search for occurrences of `setIsAccessChecked` in the codebase.
rg --type ts 'setIsAccessChecked'

Length of output: 423

docs/src/guide/essentials/server.md (1)

196-199: LGTM! Verify the conditions for login expiration logic.

The addition of accessStore.isAccessChecked to the login expiration logic is a sensible improvement. Ensure that this condition aligns with the intended behavior across the application.

Verification successful

Verification Successful: Consistent Use of isAccessChecked and loginExpiredMode

The conditions for login expiration logic, involving accessStore.isAccessChecked and preferences.app.loginExpiredMode, are consistently applied across different modules and applications. This ensures that access checks are completed before any login expiration logic is executed, aligning with the intended behavior.

  • accessStore.isAccessChecked is used in multiple locations to ensure access checks are completed.
  • preferences.app.loginExpiredMode is consistently checked for the 'modal' mode.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the conditions used in login expiration logic.

# Test: Search for all occurrences of `loginExpiredMode` and `isAccessChecked`. Expect: Correct usage and logic.
rg --type ts 'loginExpiredMode|isAccessChecked'

Length of output: 1406

@github-actions github-actions bot locked and limited conversation to collaborators Sep 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: 服务器生成的token过期后,前端页面刷新时,跳转至登录页面加载慢
1 participant