Skip to content

Commit

Permalink
chore: Added auth-cookie-change handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Anush008 committed Apr 26, 2023
1 parent 6751fa5 commit d4aa979
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
"128": "src/assets/os-icons/os-icon-128.png"
},
"host_permissions": ["<all_urls>"],
"permissions": ["storage","webRequest", "activeTab", "tabs"]
"permissions": ["storage","webRequest", "activeTab", "tabs", "cookies"]
}
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const SUPABASE_LOCAL_STORAGE_KEY = "supabase.auth.token"
export const SUPABASE_AUTH_DOMAIN = "ibcwmlhcimymasokhgvn.supabase.co"
export const SUPABASE_COOKIE_NAME = "sb-access-token"
export const SUPABASE_LOGIN_URL = "https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/authorize?provider=github&redirect_to=https://hot.opensauced.pizza/"
export const SUPABASE_LOGOUT_URL = "https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/logout"
export const OPEN_SAUCED_AUTH_TOKEN_KEY = "os-access-token"
Expand Down
18 changes: 17 additions & 1 deletion src/worker/background.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { SUPABASE_LOGOUT_URL, OPEN_SAUCED_AUTH_TOKEN_KEY } from "../constants";
import { SUPABASE_LOGOUT_URL, SUPABASE_AUTH_DOMAIN, SUPABASE_COOKIE_NAME, OPEN_SAUCED_AUTH_TOKEN_KEY } from "../constants";
import { checkTokenValidity } from "../utils/fetchOpenSaucedApiData";
import setAccessTokenInChromeStorage from "../utils/setAccessToken";

chrome.webRequest.onCompleted.addListener(
(details) => {
Expand All @@ -12,3 +14,17 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) {
chrome.tabs.sendMessage(tabId, { message: "GITHUB_URL_CHANGED" });
}
});

chrome.cookies.onChanged.addListener(async (changeInfo) => {
try {
console.log("cookie changed")
if (changeInfo.cookie.name != SUPABASE_COOKIE_NAME || changeInfo.cookie.domain != SUPABASE_AUTH_DOMAIN) return;
console.log("ITS OUR COOKIE!")
if (changeInfo.removed) return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY);
const isValidToken = await checkTokenValidity(changeInfo.cookie.value)
if (!isValidToken) return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY);
setAccessTokenInChromeStorage(changeInfo.cookie.value);
} catch(error) {
console.error("Error processing cookie update:", error);
}
})

0 comments on commit d4aa979

Please sign in to comment.