Skip to content

Commit

Permalink
feat: Auto PAT authentication (#29)
Browse files Browse the repository at this point in the history
* chore: Added worker script

* feat: auto-pat-authentication

* chore: Added empty export from the worker file to build as module

* fix: Added * in the manifest to resolve prod build error
  • Loading branch information
Anush008 authored Apr 24, 2023
1 parent 2af7aad commit f4f8cf6
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 79 deletions.
12 changes: 11 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@
{
"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": {
"service_worker": "src/worker/background.ts",
"type": "module"
},
"icons": {
"16": "src/assets/os-icons/os-icon-16.png",
"32": "src/assets/os-icons/os-icon-32.png",
"48": "src/assets/os-icons/os-icon-48.png",
"128": "src/assets/os-icons/os-icon-128.png"
},
"permissions": ["storage"]
"host_permissions": ["<all_urls>"],
"permissions": ["storage","webRequest"]
}
5 changes: 1 addition & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { useState, useEffect } from "react";

import Start from "./pages/start";
import SignIn from "./pages/signin";
import Home from "./pages/home";
import Loading from "./pages/loading";

import { checkTokenValidity } from "./utils/fetchOpenSaucedApiData";

function App() {
const [osAccessToken, setOsAccessToken] = useState("");
// renderedPage can be either "start", "home", "signin" or "loading"
// renderedPage can be either "start", "home" or "loading"
const [renderedPage, setRenderedPage] = useState("loading");

useEffect(() => {
Expand All @@ -36,8 +35,6 @@ function App() {
<Start setRenderedPage={setRenderedPage} />
) : renderedPage === "home" ? (
<Home osAccessToken={osAccessToken} setRenderedPage={setRenderedPage} />
) : renderedPage === "signin" ? (
<SignIn setRenderedPage={setRenderedPage} />
) : (
<Loading />
)}
Expand Down
22 changes: 22 additions & 0 deletions src/content-scripts/hotOSHomePage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { checkTokenValidity } from "../utils/fetchOpenSaucedApiData";
import getAccessToken from "../utils/getAccessToken";
import setAccessTokenInChromeStorage from "../utils/setAccessToken";

const processHotOSHomePage = async () => {
const data = await chrome.storage.sync.get(["os-access-token"]);
if (data["os-access-token"]) 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);
69 changes: 0 additions & 69 deletions src/pages/signin.tsx

This file was deleted.

12 changes: 7 additions & 5 deletions src/pages/start.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ function Start({ setRenderedPage }: StartProps) {
</a>{" "}
browser extension.
</p>
<button
className="bg-orange border-none rounded-md text-white font-bold py-2 px-4 cursor-pointer
<a
target="_blank"
rel="noopener noreferrer"
href="https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/authorize?provider=github&redirect_to=https://hot.opensauced.pizza/"
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]"
onClick={() => setRenderedPage("signin")}
>
Get Started
</button>
Login!
</a>
</div>
);
}
Expand Down
8 changes: 8 additions & 0 deletions src/utils/getAccessToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const getAccessToken = (): string | null => {
const localStore = window.localStorage.getItem("supabase.auth.token");
if (localStore === null) return null;
return JSON.parse(localStore)?.currentSession?.access_token;
};

export default getAccessToken;

14 changes: 14 additions & 0 deletions src/utils/setAccessToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const setAccessTokenInChromeStorage = (accessToken: string): Promise<void> => {
return new Promise((resolve, reject) => {
chrome.storage.sync.set({ "os-access-token": accessToken }, () => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve();
}
});
});
};

export default setAccessTokenInChromeStorage;

8 changes: 8 additions & 0 deletions src/worker/background.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
chrome.webRequest.onCompleted.addListener(
(details) => {
chrome.storage.sync.remove("os-access-token");
},
{ urls: ["https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/logout"] }
);

export {};

0 comments on commit f4f8cf6

Please sign in to comment.