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

fix auth edge case #180

Merged
merged 3 commits into from
Sep 12, 2024
Merged

fix auth edge case #180

merged 3 commits into from
Sep 12, 2024

Conversation

elg0nz
Copy link
Contributor

@elg0nz elg0nz commented Sep 12, 2024

  • fix edge case where user is authed but with old credentials
  • handle edge case where auth works but getTabs doesn't

Summary by CodeRabbit

  • New Features

    • Enhanced user authentication handling in the Chrome extension.
    • Improved management of active tabs and user interactions.
  • Bug Fixes

    • Resolved issues related to missing tab IDs during authentication processes, ensuring clearer responses.
  • Chores

    • Removed the GitHub Actions workflow for Biome, streamlining the CI process.

@elg0nz elg0nz requested review from fitzk and jonathankap September 12, 2024 22:22
Copy link

coderabbitai bot commented Sep 12, 2024

Walkthrough

Walkthrough

The changes in this pull request enhance the authentication handling and message communication within the Chrome extension's background script. New functions for authentication management, including retrieving and clearing authentication data, have been added. Additionally, modifications to the message handling logic improve responses when a tab ID is unavailable, ensuring better management of authentication states.

Changes

Files Change Summary
apps/chrome-extension/src/pages/background/index.ts - Added functions: getAuth, clearAuth, getCurrentTab, and showAuthTab.
- Improved authentication handling and state management.
apps/mocksi-lite-next/src/pages/background/index.ts - Modified message handling to address scenarios with undefined tab.id.
- Adjusted response messages to enhance clarity regarding authentication status.
.github/workflows/biome.yml - Deleted the Biome GitHub Actions workflow that automated testing for the main branch.

Assessment against linked issues

Objective Addressed Explanation
Improve authentication handling (undefined)
Enhance message handling (undefined)

Possibly related issues

  • None identified.

Possibly related PRs

Poem

🐇 In the land of code where rabbits play,
New functions hop in, brightening the way.
Authentication's clear, messages flow,
With each little change, our extension will grow!
So here's to the scripts, both clever and spry,
A toast to the code, let our spirits fly! 🎉


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 49e52f6 and b05a6d8.

Files selected for processing (1)
  • .github/workflows/biome.yml (0 hunks)
Files not reviewed due to no reviewable changes (1)
  • .github/workflows/biome.yml

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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9f3f40d and 14dc39f.

Files selected for processing (2)
  • apps/chrome-extension/src/pages/background/index.ts (1 hunks)
  • apps/mocksi-lite-next/src/pages/background/index.ts (1 hunks)
Additional context used
Biome
apps/chrome-extension/src/pages/background/index.ts

[error] 53-74: Promise executor functions should not be async.

(lint/suspicious/noAsyncPromiseExecutor)

Additional comments not posted (6)
apps/mocksi-lite-next/src/pages/background/index.ts (1)

140-148: LGTM!

The new conditional block correctly handles the scenario where tab.id is undefined. It ensures that authentication is properly initiated by calling showAuthTab(true) and sends an appropriate response indicating that authentication is in progress.

This change improves the robustness of the authentication process and provides a clear response when a tab is not available.

apps/chrome-extension/src/pages/background/index.ts (5)

11-34: LGTM!

The function correctly handles the case where the access token is expired by clearing the authentication data. It also uses a try-catch block to handle errors and returns null if there is no authentication data or if an error occurs.


36-43: LGTM!

The function correctly clears the authentication data from Chrome storage and uses a try-catch block to handle errors.


45-50: LGTM!

The function correctly retrieves the current active tab using the chrome.tabs.query method and returns it.


85-106: LGTM!

The function correctly handles the user clicking the extension icon by sending a message to the current tab to mount the extension, sending a message with the previous request data and message if there is a previous request, and setting the extension icon to "play-icon.png" if the previous request message is "PLAY".


119-203: LGTM!

The function correctly handles the different types of messages received from an external source:

  • For "AUTH_ERROR" messages, it clears the authentication data and sends a response with the message "retry".
  • For "UNAUTHORIZED" messages, it retrieves the authentication data and sends a response with the access token, email, and current tab URL if the authentication data exists, or opens the authentication tab and sends a response with the message "authenticating" if the authentication data doesn't exist.
  • For other messages, it sends a message to the current tab with the request data and message and sets the extension icon based on the message.

It also correctly stores the last app state in the prevRequest variable if the message is not "MINIMIZED".

Comment on lines 140 to 141
// FIXME: there's duplicated code belows.
if (!tab.id) {
Copy link

Choose a reason for hiding this comment

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

Offer assistance to fix the duplicated code.

There is a TODO comment indicating that there is duplicated code that needs to be fixed. I can help you refactor the code to remove the duplication.

Do you want me to suggest a refactoring or open a GitHub issue to track this task?

Comment on lines 52 to 75
async function showAuthTab(force?: boolean) {
return new Promise(async (resolve: (value?: unknown) => void) => {
chrome.tabs.query({}, function (tabs) {
let tabExists = false;
if (!force) {
for (const tab of tabs) {
const tabUrlStr = tab.url || tab.pendingUrl || "";
const loadUrl = new URL(import.meta.env.VITE_NEST_APP);
const tabUrl = new URL(tabUrlStr);
if (loadUrl.href === tabUrl.href) {
tabExists = true;
break;
}
}
}

if (!tabExists) {
chrome.tabs.create({ url: import.meta.env.VITE_NEST_APP }, resolve);
} else {
resolve();
}
});
});
}
Copy link

Choose a reason for hiding this comment

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

Fix the async promise executor function.

The static analysis tool has flagged that the promise executor function should not be async. This is because the async function will return a promise, which is not the expected return type for a promise executor function.

To fix this, you can remove the async keyword from the promise executor function and use .then() to handle the asynchronous code instead:

-  return new Promise(async (resolve: (value?: unknown) => void) => {
+  return new Promise((resolve: (value?: unknown) => void) => {
     chrome.tabs.query({}, function (tabs) {
       // ...
-      if (!tabExists) {
-        chrome.tabs.create({ url: import.meta.env.VITE_NEST_APP }, resolve);
-      } else {
-        resolve();
-      }
+      chrome.tabs.create({ url: import.meta.env.VITE_NEST_APP }).then(resolve);
     });
   });

Committable suggestion was skipped due to low confidence.

Tools
Biome

[error] 53-74: Promise executor functions should not be async.

(lint/suspicious/noAsyncPromiseExecutor)

@elg0nz elg0nz merged commit b17e803 into main Sep 12, 2024
2 checks passed
@elg0nz elg0nz deleted the fix-auth-edge-case branch September 12, 2024 23:02
This was referenced Sep 12, 2024
elg0nz added a commit that referenced this pull request Sep 13, 2024
fitzk pushed a commit that referenced this pull request Sep 14, 2024
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.

1 participant