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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions apps/mocksi-lite-next/src/pages/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
console.log("background script loaded");
const MOCKSI_AUTH = "mocksi-auth";

const getAuth = async (): Promise<null | {
accessToken: string;
email: string;
}> => {
const MOCKSI_AUTH = "mocksi-auth";
try {
const storageAuth = await chrome.storage.local.get(MOCKSI_AUTH);
const mocksiAuth = JSON.parse(storageAuth[MOCKSI_AUTH]);
Expand All @@ -15,6 +15,36 @@ const getAuth = async (): Promise<null | {
}
};

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


const showAuthTab = async (): Promise<void> => {
return new Promise((resolve) => {
chrome.tabs.query({}, function(tabs) {
let tabExists = false;
for (let tab of tabs) {
const loadUrl = new URL(import.meta.env.VITE_NEST_APP);
const tabUrl = new URL(tab.url || tab.pendingUrl);
if (loadUrl.href === tabUrl.href) {
tabExists = true;
break;
}
}
if (!tabExists) {
chrome.tabs.create({ url: import.meta.env.VITE_NEST_APP }, resolve);
} else {
resolve();
}
});
})
}

addEventListener("install", () => {
// TODO test if this works on other browsers
chrome.tabs.create({
Expand Down Expand Up @@ -56,14 +86,19 @@ chrome.runtime.onMessageExternal.addListener(
status: "ok",
});
} else {
chrome.tabs.create({
url: import.meta.env.VITE_NEST_APP,
});
await showAuthTab();
sendResponse({
message: "authenticating",
status: "ok",
});
}
} else if (request.message === "AUTH_ERROR") {
await clearAuth();
await showAuthTab();
sendResponse({
message: "authenticating",
status: "ok",
});
} else {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs[0].id) {
Expand Down
Loading