Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Handle auth errors, make sure to only open auth tab once #157

Merged
merged 1 commit into from
Aug 23, 2024

Conversation

jonathankap
Copy link
Contributor

@jonathankap jonathankap commented Aug 23, 2024

Handle auth errors by showing auth tab
Check that we don't open the auth tab multiple times

Summary by CodeRabbit

  • New Features

    • Introduced clearAuth function to clear stored authentication data.
    • Added showAuthTab function to manage tab creation and display based on authentication status.
  • Improvements

    • Enhanced handling of authentication errors by integrating tab management into the response process.
    • Improved code organization and readability for better maintainability.

@jonathankap jonathankap requested a review from fitzk August 23, 2024 05:29
Copy link

coderabbitai bot commented Aug 23, 2024

Walkthrough

Walkthrough

The changes involve enhancements to the background script in index.ts, where a new constant MOCKSI_AUTH is declared for broader accessibility. Two asynchronous functions, clearAuth and showAuthTab, are introduced to manage authentication data and tab interactions more effectively. The onMessageExternal listener is modified to address authentication errors by integrating calls to showAuthTab, reorganizing the code for improved clarity and functionality.

Changes

File Change Summary
apps/mocksi-lite-next/src/pages/background/index.ts Introduced MOCKSI_AUTH constant, added clearAuth and showAuthTab functions, modified onMessageExternal listener.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant BackgroundScript
    participant ChromeStorage
    participant TabManager

    User->>BackgroundScript: Request authentication
    BackgroundScript->>ChromeStorage: Get auth data
    alt Auth data exists
        ChromeStorage-->>BackgroundScript: Return auth data
        BackgroundScript-->>User: Provide access
    else Auth data missing
        BackgroundScript->>TabManager: Show auth tab
        TabManager-->>BackgroundScript: Open new tab
        BackgroundScript-->>User: Prompt for login
    end
Loading

🐰 In the land of code where rabbits play,
A constant was born, brightening the way.
With functions to clear and tabs to show,
Authentication troubles, we swiftly forego.
Hops of improvement, so clear and neat,
In our script, joys of change are sweet! 🌟


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.

Copy link

@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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 91e8ced and b575eb0.

Files selected for processing (1)
  • apps/mocksi-lite-next/src/pages/background/index.ts (3 hunks)
Additional comments not posted (2)
apps/mocksi-lite-next/src/pages/background/index.ts (2)

89-101: LGTM! The changes to handle authentication errors are well-structured.

The modifications to the onMessageExternal listener effectively handle authentication errors by using clearAuth and showAuthTab.


27-46: Verify the environment variable VITE_NEST_APP.

Ensure that import.meta.env.VITE_NEST_APP is defined and correctly set in the environment variables.

Run the following script to verify the presence of the environment variable in the codebase:

Verification successful

Environment variable VITE_NEST_APP is correctly defined.

The variable is set in both .env.production and .env.development files, ensuring it is available for different environments.

  • apps/mocksi-lite-next/.env.production: VITE_NEST_APP=https://nest-auth-ts-merge.onrender.com
  • apps/mocksi-lite-next/.env.development: VITE_NEST_APP=http://localhost:3030
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `VITE_NEST_APP` in the codebase.

# Test: Search for the environment variable usage. Expect: At least one occurrence.
rg --type js --type ts 'import\.meta\.env\.VITE_NEST_APP'

Length of output: 528


Script:

#!/bin/bash
# Description: Check for the definition of `VITE_NEST_APP` in environment configuration files.

# Test: Search for the definition of `VITE_NEST_APP` in common environment files.
rg 'VITE_NEST_APP' --glob '**/.env*'

Length of output: 203

Comment on lines +18 to +25
const clearAuth = async (): Promise<void> => {
try {
const storageAuth = await chrome.storage.local.get(MOCKSI_AUTH);
storageAuth[MOCKSI_AUTH] = null;
} catch (err) {
console.error(err);
}
};
Copy link

Choose a reason for hiding this comment

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

Ensure authentication data is cleared from storage.

The function sets the authentication data to null but does not save the changes back to Chrome's local storage.

Apply this diff to fix the function:

 const clearAuth = async (): Promise<void> => {
   try {
     const storageAuth = await chrome.storage.local.get(MOCKSI_AUTH);
     storageAuth[MOCKSI_AUTH] = null;
+    await chrome.storage.local.set({ [MOCKSI_AUTH]: null });
   } catch (err) {
     console.error(err);
   }
 }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const clearAuth = async (): Promise<void> => {
try {
const storageAuth = await chrome.storage.local.get(MOCKSI_AUTH);
storageAuth[MOCKSI_AUTH] = null;
} catch (err) {
console.error(err);
}
};
const clearAuth = async (): Promise<void> => {
try {
const storageAuth = await chrome.storage.local.get(MOCKSI_AUTH);
storageAuth[MOCKSI_AUTH] = null;
await chrome.storage.local.set({ [MOCKSI_AUTH]: null });
} catch (err) {
console.error(err);
}
};

Copy link
Contributor

@elg0nz elg0nz left a comment

Choose a reason for hiding this comment

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

LGTM

@jonathankap jonathankap merged commit 858c670 into main Aug 23, 2024
3 checks passed
@jonathankap jonathankap deleted the auth_error branch August 23, 2024 16:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants