diff --git a/manifest.json b/manifest.json index 087bbd3f..19b7ed98 100644 --- a/manifest.json +++ b/manifest.json @@ -7,11 +7,6 @@ { "js": ["src/content-scripts/profileScreen.ts"], "matches": ["https://github.com/*"] - }, - { - "js": ["src/content-scripts/hotOSHomePage.ts"], - "matches": ["https://hot.opensauced.pizza/*"], - "run_at": "document_end" } ], "background": { @@ -25,5 +20,5 @@ "128": "src/assets/os-icons/os-icon-128.png" }, "host_permissions": [""], - "permissions": ["storage","webRequest", "tabs", "cookies"] + "permissions": ["storage", "tabs", "cookies"] } diff --git a/src/constants.ts b/src/constants.ts index 1209642e..80bff6d7 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,10 +1,8 @@ -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 SUPABASE_LOGIN_URL = "https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/authorize?provider=github&redirect_to=https://insights.opensauced.pizza/"; +export const SUPABASE_AUTH_COOKIE_NAME = "supabase-auth-token"; export const OPEN_SAUCED_AUTH_TOKEN_KEY = "os-access-token"; export const OPEN_SAUCED_USERS_ENDPOINT = "https://api.opensauced.pizza/v1/users"; export const OPEN_SAUCED_SESSION_ENDPOINT = "https://api.opensauced.pizza/v1/auth/session"; +export const OPEN_SAUCED_INSIGHTS_DOMAIN = "insights.opensauced.pizza"; export const GITHUB_PROFILE_MENU_SELECTOR = ".p-nickname.vcard-username.d-block"; export const GITHUB_PROFILE_EDIT_MENU_SELECTOR = "button.js-profile-editable-edit-button"; diff --git a/src/content-scripts/hotOSHomePage.ts b/src/content-scripts/hotOSHomePage.ts deleted file mode 100644 index 4d1a2ab5..00000000 --- a/src/content-scripts/hotOSHomePage.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { checkTokenValidity } from "../utils/fetchOpenSaucedApiData"; -import getAccessToken from "../utils/getAccessToken"; -import setAccessTokenInChromeStorage from "../utils/setAccessToken"; -import { OPEN_SAUCED_AUTH_TOKEN_KEY } from "../constants"; - -const processHotOSHomePage = async () => { - const data = await chrome.storage.sync.get([OPEN_SAUCED_AUTH_TOKEN_KEY]); - - if (data[OPEN_SAUCED_AUTH_TOKEN_KEY]) { - return; -} - try { - const accessToken = getAccessToken(); - - if (!accessToken) { - return; -} - - const isValid = await checkTokenValidity(accessToken); - - if (!isValid) { - return; -} - - await setAccessTokenInChromeStorage(accessToken); - } catch (error) { - console.error("Error processing Hot home page:", error); - } -}; - -// the local storage takes time to set after the document is idle -setTimeout(processHotOSHomePage, 1000); diff --git a/src/pages/profile.tsx b/src/pages/profile.tsx index 36d50697..c16abef6 100644 --- a/src/pages/profile.tsx +++ b/src/pages/profile.tsx @@ -80,6 +80,12 @@ export const Profile = () => {
+ User avatar { @ {page.props.userName}

+
{(user?.linkedin_url || user?.twitter_username) &&
{user.linkedin_url && { {user.twitter_username && { + chrome.cookies.get( + { + name: SUPABASE_AUTH_COOKIE_NAME, + url: `https://${OPEN_SAUCED_INSIGHTS_DOMAIN}`, + }, + async cookie => { + if (!cookie) { + return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); + } + try { + const authCookie = JSON.parse(decodeURIComponent(cookie.value))[0]; + const isValidToken = await checkTokenValidity(authCookie); + + if (!isValidToken) { + return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); + } + void setAccessTokenInChromeStorage(authCookie); + } catch (error) { + void chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); + console.error("Error processing cookie:", error); + } + }, + ); +}; diff --git a/src/utils/getAccessToken.ts b/src/utils/getAccessToken.ts deleted file mode 100644 index 951433eb..00000000 --- a/src/utils/getAccessToken.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { SUPABASE_LOCAL_STORAGE_KEY } from "../constants"; - -const getAccessToken = (): string | null => { - const localStore = window.localStorage.getItem(SUPABASE_LOCAL_STORAGE_KEY); - - if (localStore === null) { - return null; -} - return JSON.parse(localStore)?.currentSession?.access_token; - }; - - export default getAccessToken; - diff --git a/src/worker/background.ts b/src/worker/background.ts index 2977019d..82033f53 100644 --- a/src/worker/background.ts +++ b/src/worker/background.ts @@ -1,13 +1,5 @@ -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( - () => { - void chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); - }, - { urls: [SUPABASE_LOGOUT_URL] }, -); +import { checkAuthentication } from "../utils/checkAuthentication"; +import { SUPABASE_AUTH_COOKIE_NAME, OPEN_SAUCED_INSIGHTS_DOMAIN } from "../constants"; chrome.tabs.onUpdated.addListener((tabId, changeInfo) => { if (changeInfo.url?.includes("github.com")) { @@ -15,21 +7,14 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo) => { } }); -chrome.cookies.onChanged.addListener(async changeInfo => { - try { - if (changeInfo.cookie.name !== SUPABASE_COOKIE_NAME || changeInfo.cookie.domain !== SUPABASE_AUTH_DOMAIN) { - return; - } - 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); +chrome.cookies.onChanged.addListener(changeInfo => { + if ( + changeInfo.cookie.name === SUPABASE_AUTH_COOKIE_NAME || + changeInfo.cookie.domain === OPEN_SAUCED_INSIGHTS_DOMAIN + ) { + checkAuthentication(); } - void setAccessTokenInChromeStorage(changeInfo.cookie.value); - } catch (error) { - console.error("Error processing cookie update:", error); - } }); + +chrome.runtime.onInstalled.addListener(checkAuthentication); +chrome.runtime.onStartup.addListener(checkAuthentication);