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: Auto PAT authentication #29

Merged
merged 4 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Anush008 marked this conversation as resolved.
Show resolved Hide resolved
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"] }
Anush008 marked this conversation as resolved.
Show resolved Hide resolved
);

export {};
Anush008 marked this conversation as resolved.
Show resolved Hide resolved