From ec6722c360dfcdb5cc10cb2300448e1656d58456 Mon Sep 17 00:00:00 2001 From: Anush Date: Wed, 26 Apr 2023 07:34:35 +0530 Subject: [PATCH] feat: Added a configuration file (#32) * chore: Created constants.ts for config literals * feat: Created configuration file --- src/App.tsx | 12 ++++++------ src/constants.ts | 8 ++++++++ src/content-scripts/hotOSHomePage.ts | 5 +++-- src/content-scripts/profileScreen.ts | 4 ++-- src/pages/start.tsx | 3 ++- src/utils/dom-utils/inviteToOpenSauced.ts | 3 ++- src/utils/dom-utils/viewOnOpenSauced.ts | 3 ++- src/utils/fetchOpenSaucedApiData.ts | 9 +++++---- src/utils/getAccessToken.ts | 4 +++- src/utils/setAccessToken.ts | 4 +++- src/worker/background.ts | 10 +++++----- 11 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 src/constants.ts diff --git a/src/App.tsx b/src/App.tsx index 1f300517..9a79174d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,9 +1,8 @@ import { useState, useEffect } from "react"; - +import { OPEN_SAUCED_AUTH_TOKEN_KEY } from "./constants"; import Start from "./pages/start"; import Home from "./pages/home"; import Loading from "./pages/loading"; - import { checkTokenValidity } from "./utils/fetchOpenSaucedApiData"; function App() { @@ -12,14 +11,15 @@ function App() { const [renderedPage, setRenderedPage] = useState("loading"); useEffect(() => { - chrome.storage.sync.get(["os-access-token"], (result) => { - if (result["os-access-token"]) { - checkTokenValidity(result["os-access-token"]).then((valid) => { + chrome.storage.sync.get([OPEN_SAUCED_AUTH_TOKEN_KEY], (result) => { + const authToken: string | undefined = result[OPEN_SAUCED_AUTH_TOKEN_KEY]; + if (authToken) { + checkTokenValidity(authToken).then((valid) => { if (!valid) { setOsAccessToken(""); setRenderedPage("signin"); } else { - setOsAccessToken(result["os-access-token"]); + setOsAccessToken(authToken); setRenderedPage("home"); } }); diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 00000000..5fec56c3 --- /dev/null +++ b/src/constants.ts @@ -0,0 +1,8 @@ +export const SUPABASE_LOCAL_STORAGE_KEY = "supabase.auth.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" +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 GITHUB_PROFILE_MENU_SELECTOR = ".p-nickname.vcard-username.d-block" +export const GITHUB_PROFILE_EDIT_MENU_SELECTOR = "button.js-profile-editable-edit-button" \ No newline at end of file diff --git a/src/content-scripts/hotOSHomePage.ts b/src/content-scripts/hotOSHomePage.ts index 0ebf3e9e..18b0e17d 100644 --- a/src/content-scripts/hotOSHomePage.ts +++ b/src/content-scripts/hotOSHomePage.ts @@ -1,10 +1,11 @@ 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(["os-access-token"]); - if (data["os-access-token"]) return; + 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; diff --git a/src/content-scripts/profileScreen.ts b/src/content-scripts/profileScreen.ts index ffce56e7..558a4b30 100644 --- a/src/content-scripts/profileScreen.ts +++ b/src/content-scripts/profileScreen.ts @@ -1,12 +1,12 @@ import { getGithubUsername } from "../utils/urlMatchers"; -import { getOpenSaucedUser } from "../utils/fetchOpenSaucedApiData"; +import { isOpenSaucedUser } from "../utils/fetchOpenSaucedApiData"; import injectViewOnOpenSauced from "../utils/dom-utils/viewOnOpenSauced"; import injectInviteToOpenSauced from "../utils/dom-utils/inviteToOpenSauced"; const processProfilePage = async () => { const username = getGithubUsername(window.location.href); if (username != null) { - const openSaucedUser = await getOpenSaucedUser(username); + const openSaucedUser = await isOpenSaucedUser(username); if (openSaucedUser) injectViewOnOpenSauced(username); else injectInviteToOpenSauced(username); } diff --git a/src/pages/start.tsx b/src/pages/start.tsx index 007b03e1..7506e033 100644 --- a/src/pages/start.tsx +++ b/src/pages/start.tsx @@ -1,4 +1,5 @@ import OpenSaucedLogo from "../assets/opensauced-logo.svg"; +import { SUPABASE_LOGIN_URL } from "../constants"; interface StartProps { setRenderedPage: (page: string) => void; @@ -21,7 +22,7 @@ function Start({ setRenderedPage }: StartProps) { diff --git a/src/utils/dom-utils/inviteToOpenSauced.ts b/src/utils/dom-utils/inviteToOpenSauced.ts index c2201424..c5e16e63 100644 --- a/src/utils/dom-utils/inviteToOpenSauced.ts +++ b/src/utils/dom-utils/inviteToOpenSauced.ts @@ -1,3 +1,4 @@ +import { GITHUB_PROFILE_MENU_SELECTOR } from "../../constants"; import { InviteToOpenSaucedButton } from "../../content-scripts/components/InviteToOpenSauced/InviteToOpenSaucedButton"; import { InviteToOpenSaucedModal } from "../../content-scripts/components/InviteToOpenSauced/InviteToOpenSaucedModal"; import { getTwitterUsername, getLinkedInUsername } from "../urlMatchers"; @@ -23,7 +24,7 @@ const injectOpenSaucedInviteButton = (username: string) => { linkedInUsername, }, inviteToOpenSaucedButton); - const userBio = document.querySelector(".p-nickname.vcard-username.d-block"); + const userBio = document.querySelector(GITHUB_PROFILE_MENU_SELECTOR); if (!userBio || !userBio.parentNode) return; userBio.parentNode.replaceChild(inviteToOpenSaucedButton, userBio); document.body.appendChild(inviteToOpenSaucedModal); diff --git a/src/utils/dom-utils/viewOnOpenSauced.ts b/src/utils/dom-utils/viewOnOpenSauced.ts index d920f972..186a87b2 100644 --- a/src/utils/dom-utils/viewOnOpenSauced.ts +++ b/src/utils/dom-utils/viewOnOpenSauced.ts @@ -1,10 +1,11 @@ +import { GITHUB_PROFILE_MENU_SELECTOR, GITHUB_PROFILE_EDIT_MENU_SELECTOR } from "../../constants"; import { ViewOnOpenSaucedButton } from "../../content-scripts/components/ViewOnOpenSaucedButton/ViewOnOpenSaucedButton"; const injectViewOnOpenSaucedButton = (username: string) => { const viewOnOpenSaucedButton = ViewOnOpenSaucedButton(username); const userBio = document.querySelector( - ".p-nickname.vcard-username.d-block, button.js-profile-editable-edit-button" + `${GITHUB_PROFILE_MENU_SELECTOR}, ${GITHUB_PROFILE_EDIT_MENU_SELECTOR}` ); if (!userBio || !userBio.parentNode) return; userBio.parentNode.replaceChild(viewOnOpenSaucedButton, userBio); diff --git a/src/utils/fetchOpenSaucedApiData.ts b/src/utils/fetchOpenSaucedApiData.ts index 10838092..ee3965e4 100644 --- a/src/utils/fetchOpenSaucedApiData.ts +++ b/src/utils/fetchOpenSaucedApiData.ts @@ -1,7 +1,9 @@ -export const getOpenSaucedUser = async (username: string) => { +import { OPEN_SAUCED_USERS_ENDPOINT, OPEN_SAUCED_SESSION_ENDPOINT } from "../constants"; + +export const isOpenSaucedUser = async (username: string) => { try { const response = await fetch( - `https://api.opensauced.pizza/v1/users/${username}` + `${OPEN_SAUCED_USERS_ENDPOINT}/${username}` ); return response.status === 200; } catch (error) { @@ -10,8 +12,7 @@ export const getOpenSaucedUser = async (username: string) => { }; export const checkTokenValidity = async (token: string) => { - const url = "https://api.opensauced.pizza/v1/auth/session"; - const response = await fetch(url, { + const response = await fetch(OPEN_SAUCED_SESSION_ENDPOINT, { method: "GET", headers: { Authorization: `Bearer ${token}`, diff --git a/src/utils/getAccessToken.ts b/src/utils/getAccessToken.ts index 44b2a40d..0d427fb0 100644 --- a/src/utils/getAccessToken.ts +++ b/src/utils/getAccessToken.ts @@ -1,5 +1,7 @@ +import { SUPABASE_LOCAL_STORAGE_KEY } from "../constants"; + const getAccessToken = (): string | null => { - const localStore = window.localStorage.getItem("supabase.auth.token"); + const localStore = window.localStorage.getItem(SUPABASE_LOCAL_STORAGE_KEY); if (localStore === null) return null; return JSON.parse(localStore)?.currentSession?.access_token; }; diff --git a/src/utils/setAccessToken.ts b/src/utils/setAccessToken.ts index 9f35d935..7f3ac5cb 100644 --- a/src/utils/setAccessToken.ts +++ b/src/utils/setAccessToken.ts @@ -1,6 +1,8 @@ +import { OPEN_SAUCED_AUTH_TOKEN_KEY } from "../constants"; + const setAccessTokenInChromeStorage = (accessToken: string): Promise => { return new Promise((resolve, reject) => { - chrome.storage.sync.set({ "os-access-token": accessToken }, () => { + chrome.storage.sync.set({ [OPEN_SAUCED_AUTH_TOKEN_KEY]: accessToken }, () => { if (chrome.runtime.lastError) { reject(chrome.runtime.lastError); } else { diff --git a/src/worker/background.ts b/src/worker/background.ts index c2f0e827..3e9f8919 100644 --- a/src/worker/background.ts +++ b/src/worker/background.ts @@ -1,8 +1,8 @@ +import { SUPABASE_LOGOUT_URL, OPEN_SAUCED_AUTH_TOKEN_KEY } from "../constants"; + chrome.webRequest.onCompleted.addListener( (details) => { - chrome.storage.sync.remove("os-access-token"); + chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); }, - { urls: ["https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/logout"] } - ); - -export {}; \ No newline at end of file + { urls: [SUPABASE_LOGOUT_URL] } + ); \ No newline at end of file