Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added a configuration file #32

Merged
merged 3 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 6 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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");
}
});
Expand Down
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 3 additions & 2 deletions src/content-scripts/hotOSHomePage.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/content-scripts/profileScreen.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/pages/start.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import OpenSaucedLogo from "../assets/opensauced-logo.svg";
import { SUPABASE_LOGIN_URL } from "../constants";

interface StartProps {
setRenderedPage: (page: string) => void;
Expand All @@ -21,7 +22,7 @@ function Start({ setRenderedPage }: StartProps) {
<a
target="_blank"
rel="noopener noreferrer"
href="https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/authorize?provider=github&redirect_to=https://hot.opensauced.pizza/"
href={SUPABASE_LOGIN_URL}
className="bg-orange no-underline border-none rounded-md text-white font-bold py-2 px-4 cursor-pointer
bg-gradient-to-r from-[#e67e22] to-[#d35400]"
>
Expand Down
3 changes: 2 additions & 1 deletion src/utils/dom-utils/inviteToOpenSauced.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { InviteToOpenSaucedButton } from "../../components/InviteToOpenSauced/InviteToOpenSaucedButton";
import { InviteToOpenSaucedModal } from "../../components/InviteToOpenSauced/InviteToOpenSaucedModal";
import { GITHUB_PROFILE_MENU_SELECTOR } from "../../constants";
import { getTwitterUsername, getLinkedInUsername } from "../urlMatchers";

const injectOpenSaucedInviteButton = (username: string) => {
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/utils/dom-utils/viewOnOpenSauced.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ViewOnOpenSaucedButton } from "../../components/ViewOnOpenSaucedButton/ViewOnOpenSaucedButton";
import { GITHUB_PROFILE_MENU_SELECTOR, GITHUB_PROFILE_EDIT_MENU_SELECTOR } from "../../constants";

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);
Expand Down
9 changes: 5 additions & 4 deletions src/utils/fetchOpenSaucedApiData.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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}`,
Expand Down
4 changes: 3 additions & 1 deletion src/utils/getAccessToken.ts
Original file line number Diff line number Diff line change
@@ -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;
};
Expand Down
4 changes: 3 additions & 1 deletion src/utils/setAccessToken.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { OPEN_SAUCED_AUTH_TOKEN_KEY } from "../constants";

const setAccessTokenInChromeStorage = (accessToken: string): Promise<void> => {
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 {
Expand Down
10 changes: 5 additions & 5 deletions src/worker/background.ts
Original file line number Diff line number Diff line change
@@ -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 {};
{ urls: [SUPABASE_LOGOUT_URL] }
);