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: Logout from the extension #138

Merged
merged 3 commits into from
May 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"48": "src/assets/os-icons/os-icon-48.png",
"128": "src/assets/os-icons/os-icon-128.png"
},
"host_permissions": ["<all_urls>"],
"host_permissions": ["https://github.com/*", "https://insights.opensauced.pizza/*"],
"permissions": ["storage", "tabs", "cookies"]
}
21 changes: 13 additions & 8 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
"format": "npm run lint -- --fix"
},
"dependencies": {
"@types/chrome": "^0.0.231",
"@types/node-emoji": "^1.8.2",
"date-fns": "^2.30.0",
"gpt-tokenizer": "^1.0.5",
"node-emoji": "^1.11.0",
Expand All @@ -26,7 +24,9 @@
},
"devDependencies": {
"@crxjs/vite-plugin": "^1.0.14",
"@types/chrome": "^0.0.237",
"@types/node": "^20.1.4",
"@types/node-emoji": "^1.8.2",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.59.1",
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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_OPTED_LOG_OUT_KEY = "opted-log-out";
export const OPEN_SAUCED_INSIGHTS_DOMAIN = "insights.opensauced.pizza";
export const AI_PR_DESCRIPTION_CONFIG_KEY = "ai-pr-description-config";

Expand Down
2 changes: 1 addition & 1 deletion src/pages/aiprdescription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const AIPRDescription = () => {
ref={setRefFromKey("length")}
className="text-black p-1.5 rounded-md mb-1 w-half accent-orange"
id="length"
max="600"
max="500"
min="100"
name="length"
type="range"
Expand Down
20 changes: 10 additions & 10 deletions src/pages/profile.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { useEffect, useState } from "react";
import { FaBrain, FaChevronLeft, FaRobot } from "react-icons/fa";
import { RiLinkedinFill, RiLinkM, RiTwitterFill } from "react-icons/ri";
import { AiOutlineReload } from "react-icons/ai";
import { SiC, SiCplusplus, SiCsharp, SiGoland, SiJavascript, SiPhp, SiPython, SiReact, SiRuby, SiRust, SiTypescript } from "react-icons/si";
import { DiJava } from "react-icons/di";
import OpenSaucedLogo from "../assets/opensauced-logo.svg";
import { getUserData, getUserPRData, getUserHighlightsData } from "../utils/fetchOpenSaucedApiData";
import { emojify } from "node-emoji";
import { goBack } from "react-chrome-extension-router";
import { goBack, goTo } from "react-chrome-extension-router";
import { getRelativeDays } from "../utils/dateUtils";
import { getUserPRVelocity } from "../utils/getUserPRVelocity";
import { BiExit } from "react-icons/bi";
import Start from "./start";
import { optLogOut } from "../utils/checkAuthentication";

const interestIcon = {
python: <SiPython />,
Expand Down Expand Up @@ -72,16 +74,14 @@ export const Profile = ({ username }: { username: string }) => {
</div>

<button
className="hover:text-orange text-lg"
title="Refresh user data"
onClick={async () => {
const [userData, userPRData] = await Promise.all([getUserData(username), getUserPRData(username)]);

setUser(userData);
setUserPR(userPRData);
className="flex items-center bg-slate-700 hover:bg-slate-700/70 hover:text-red-600 text-white gap-1 p-1.5 px-2 w-fit rounded-md font-medium text-sm"
onClick={() => {
optLogOut();
goTo(Start);
}}
>
<AiOutlineReload />
<BiExit />
Log Out
</button>
</header>

Expand Down
10 changes: 4 additions & 6 deletions src/pages/start.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import OpenSaucedLogo from "../assets/opensauced-logo.svg";
import { SUPABASE_LOGIN_URL } from "../constants";
import { optLogIn } from "../utils/checkAuthentication";

const Start = () => (
<div className="p-4 bg-slate-800">
Expand All @@ -23,15 +23,13 @@ const Start = () => (
browser extension.
</p>

<a
href={SUPABASE_LOGIN_URL}
rel="noopener noreferrer"
target="_blank"
<button
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={optLogIn}
>
Login
</a>
</button>
</div>
);

Expand Down
22 changes: 21 additions & 1 deletion src/utils/checkAuthentication.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import {
OPEN_SAUCED_AUTH_TOKEN_KEY,
OPEN_SAUCED_OPTED_LOG_OUT_KEY,
SUPABASE_AUTH_COOKIE_NAME,
OPEN_SAUCED_INSIGHTS_DOMAIN,
SUPABASE_LOGIN_URL,
} from "../constants";
import { checkTokenValidity } from "./fetchOpenSaucedApiData";
import setAccessTokenInChromeStorage from "../utils/setAccessToken";

export const checkAuthentication = () => {
export const checkAuthentication = async () => {
if (await hasOptedLogOut()) {
return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY);
}

chrome.cookies.get(
{
name: SUPABASE_AUTH_COOKIE_NAME,
Expand Down Expand Up @@ -36,4 +42,18 @@ export const isLoggedIn = async (): Promise<boolean> => Object.entries(await chr

export const getAuthToken = async (): Promise<string> => (await chrome.storage.sync.get(OPEN_SAUCED_AUTH_TOKEN_KEY))[OPEN_SAUCED_AUTH_TOKEN_KEY];

export const optLogOut = () => {
void chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY);
void chrome.storage.local.set({ [OPEN_SAUCED_OPTED_LOG_OUT_KEY]: true });
};

export const optLogIn = () => {
if (typeof window === "undefined") {
return;
}
void chrome.storage.local.set({ [OPEN_SAUCED_OPTED_LOG_OUT_KEY]: false });
window.open(SUPABASE_LOGIN_URL, "_blank");
};

const hasOptedLogOut = async (): Promise<boolean> => (await chrome.storage.local.get(OPEN_SAUCED_OPTED_LOG_OUT_KEY))[OPEN_SAUCED_OPTED_LOG_OUT_KEY] === true;

2 changes: 1 addition & 1 deletion src/worker/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ chrome.cookies.onChanged.addListener(changeInfo => {
changeInfo.cookie.name === SUPABASE_AUTH_COOKIE_NAME ||
changeInfo.cookie.domain === OPEN_SAUCED_INSIGHTS_DOMAIN
) {
checkAuthentication();
void checkAuthentication();
}
});

Expand Down