Skip to content

Commit

Permalink
feat: oauth login
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Mar 8, 2024
1 parent 899bd93 commit 5451a5b
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@octokit/core": "^5.1.0",
"@octokit/plugin-create-or-update-text-file": "^4.0.1",
"@octokit/rest": "^20.0.2",
"@supabase/supabase-js": "2.39.7",
"@types/libsodium-wrappers": "^0.7.13",
"@ubiquibot/configuration": "1.1.0",
"@uniswap/permit2-sdk": "^1.2.0",
Expand Down
5 changes: 0 additions & 5 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ <h1 class="title">Onboarding</h1>
<input type="text" class="form-control" id="walletPrivateKey" />
<span class="status-log"></span>
</div>
<div class="mb-3">
<label for="githubPat" class="form-label">GITHUB_PAT</label>
<input type="text" class="form-control" id="githubPat" />
<span class="status-log"></span>
</div>
<div class="mb-3">
<label for="orgName" class="form-label">ORG_NAME</label>
<input type="text" class="form-control" id="orgName" />
Expand Down
16 changes: 16 additions & 0 deletions static/scripts/onboarding/get-local-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function getLocalStore<T>(key: string): T | null {
const cachedIssues = localStorage.getItem(key);
if (cachedIssues) {
try {
return JSON.parse(cachedIssues); // as OAuthToken;
} catch (error) {
console.error(error);
}
}
return null;
}

export function setLocalStore<T>(key: string, value: T) {
// remove state from issues before saving to local storage
localStorage[key] = JSON.stringify(value);
}
117 changes: 117 additions & 0 deletions static/scripts/onboarding/github-login-button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { createClient } from "@supabase/supabase-js";
import { getLocalStore } from "./get-local-store";

declare const SUPABASE_URL: string;
declare const SUPABASE_ANON_KEY: string;

const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);

export interface OAuthToken {
provider_token: string;
access_token: string;
expires_in: number;
expires_at: number;
refresh_token: string;
token_type: string;
user: {
id: string;
aud: string;
role: string;
email: string;
email_confirmed_at: string;
phone: string;
confirmed_at: string;
last_sign_in_at: string;
app_metadata: { provider: string; providers: string[] };
user_metadata: {
avatar_url: string;
email: string;
email_verified: boolean;
full_name: string;
iss: string;
name: string;
phone_verified: boolean;
preferred_username: string;
provider_id: string;
sub: string;
user_name: string;
};
identities: [
{
id: string;
user_id: string;
identity_data: {
avatar_url: string;
email: string;
email_verified: boolean;
full_name: string;
iss: string;
name: string;
phone_verified: boolean;
preferred_username: string;
provider_id: string;
sub: string;
user_name: string;
};
provider: string;
last_sign_in_at: string;
created_at: string;
updated_at: string;
},
];
created_at: string;
updated_at: string;
};
}

async function gitHubLoginButtonHandler() {
const { error } = await supabase.auth.signInWithOAuth({
provider: "github",
options: {
redirectTo: "http://localhost:8080",
},
});
if (error) {
console.error("Error logging in:", error);
}
}
const gitHubLoginButton = document.createElement("button");
export async function renderGitHubLoginButton() {
// No need to show the OAuth button if we are already logged in
if (getSessionToken()) {
return;
}

const stepContainer = document.getElementById("step1");

gitHubLoginButton.id = "github-login-button";
gitHubLoginButton.innerHTML = "<span>Connect</span><span class='full'>&nbsp;To GitHub</span>";
gitHubLoginButton.addEventListener("click", gitHubLoginButtonHandler);
if (stepContainer) {
stepContainer.insertBefore(gitHubLoginButton, stepContainer.firstChild);
}
}

function getNewSessionToken() {
const hash = window.location.hash;
const params = new URLSearchParams(hash.substring(1)); // remove the '#' and parse
const providerToken = params.get("provider_token");
if (!providerToken) {
return null;
}
return providerToken;
}

function getSessionToken() {
const cachedSessionToken = getLocalStore<OAuthToken>("sb-wfzpewmlyiozupulbuur-auth-token");
if (cachedSessionToken) {
return cachedSessionToken.provider_token;
}
const newSessionToken = getNewSessionToken();
if (newSessionToken) {
return newSessionToken;
}
return null;
}

export { gitHubLoginButton, getSessionToken };
9 changes: 3 additions & 6 deletions static/scripts/onboarding/onboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { ethers } from "ethers";
import { parseUnits } from "ethers/lib/utils";
import _sodium from "libsodium-wrappers";
import YAML from "yaml";
import { erc20Abi } from "../rewards/abis/erc20Abi";
import { erc20Abi } from "../rewards/abis";
import { getNetworkName, NetworkIds, Tokens } from "../rewards/constants";
import { renderGitHubLoginButton } from "./github-login-button";

const classes = ["error", "warn", "success"];
const inputClasses = ["input-warn", "input-error", "input-success"];
Expand Down Expand Up @@ -70,7 +71,6 @@ function getTextBox(text: string) {

function resetToggle() {
(walletPrivateKey.parentNode?.querySelector(STATUS_LOG) as HTMLElement).innerHTML = "";
(githubPAT.parentNode?.querySelector(STATUS_LOG) as HTMLElement).innerHTML = "";
(orgName.parentNode?.querySelector(STATUS_LOG) as HTMLElement).innerHTML = "";
}

Expand Down Expand Up @@ -394,10 +394,6 @@ async function step1Handler() {
singleToggle("warn", `Warn: Org Name is not set.`, orgName);
return;
}
if (githubPAT.value === "") {
singleToggle("warn", `Warn: GitHub PAT is not set.`, githubPAT);
return;
}

await sodiumEncryptedSeal(X25519_KEY, `${KEY_PREFIX}${walletPrivateKey.value}`);
setConfig().catch((error) => {
Expand Down Expand Up @@ -472,6 +468,7 @@ async function init() {
try {
defaultConf.keys[PRIVATE_ENCRYPTED_KEY_NAME] = undefined;
setInputListeners();
await renderGitHubLoginButton();

setBtn.addEventListener("click", async () => {
if (currentStep === 1) {
Expand Down
92 changes: 92 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,63 @@
ignore "^5.1.8"
p-map "^4.0.0"

"@supabase/[email protected]":
version "2.1.5"
resolved "https://registry.yarnpkg.com/@supabase/functions-js/-/functions-js-2.1.5.tgz#ed1b85f499dfda21d40fe39b86ab923117cb572b"
integrity sha512-BNzC5XhCzzCaggJ8s53DP+WeHHGT/NfTsx2wUSSGKR2/ikLFQTBCDzMvGz/PxYMqRko/LwncQtKXGOYp1PkPaw==
dependencies:
"@supabase/node-fetch" "^2.6.14"

"@supabase/[email protected]":
version "2.62.2"
resolved "https://registry.yarnpkg.com/@supabase/gotrue-js/-/gotrue-js-2.62.2.tgz#9f15a451559d71475c953aa0027e1248b0210196"
integrity sha512-AP6e6W9rQXFTEJ7sTTNYQrNf0LCcnt1hUW+RIgUK+Uh3jbWvcIST7wAlYyNZiMlS9+PYyymWQ+Ykz/rOYSO0+A==
dependencies:
"@supabase/node-fetch" "^2.6.14"

"@supabase/[email protected]", "@supabase/node-fetch@^2.6.14":
version "2.6.15"
resolved "https://registry.yarnpkg.com/@supabase/node-fetch/-/node-fetch-2.6.15.tgz#731271430e276983191930816303c44159e7226c"
integrity sha512-1ibVeYUacxWYi9i0cf5efil6adJ9WRyZBLivgjs+AUpewx1F3xPi7gLgaASI2SmIQxPoCEjAsLAzKPgMJVgOUQ==
dependencies:
whatwg-url "^5.0.0"

"@supabase/[email protected]":
version "1.9.2"
resolved "https://registry.yarnpkg.com/@supabase/postgrest-js/-/postgrest-js-1.9.2.tgz#39c839022ce73f4eb5da6fb7724fb6a6392150c7"
integrity sha512-I6yHo8CC9cxhOo6DouDMy9uOfW7hjdsnCxZiaJuIVZm1dBGTFiQPgfMa9zXCamEWzNyWRjZvupAUuX+tqcl5Sw==
dependencies:
"@supabase/node-fetch" "^2.6.14"

"@supabase/[email protected]":
version "2.9.3"
resolved "https://registry.yarnpkg.com/@supabase/realtime-js/-/realtime-js-2.9.3.tgz#f822401aed70883dca5d538179b11089d6d1b6ed"
integrity sha512-lAp50s2n3FhGJFq+wTSXLNIDPw5Y0Wxrgt44eM5nLSA3jZNUUP3Oq2Ccd1CbZdVntPCWLZvJaU//pAd2NE+QnQ==
dependencies:
"@supabase/node-fetch" "^2.6.14"
"@types/phoenix" "^1.5.4"
"@types/ws" "^8.5.10"
ws "^8.14.2"

"@supabase/[email protected]":
version "2.5.5"
resolved "https://registry.yarnpkg.com/@supabase/storage-js/-/storage-js-2.5.5.tgz#2958e2a2cec8440e605bb53bd36649288c4dfa01"
integrity sha512-OpLoDRjFwClwc2cjTJZG8XviTiQH4Ik8sCiMK5v7et0MDu2QlXjCAW3ljxJB5+z/KazdMOTnySi+hysxWUPu3w==
dependencies:
"@supabase/node-fetch" "^2.6.14"

"@supabase/[email protected]":
version "2.39.7"
resolved "https://registry.yarnpkg.com/@supabase/supabase-js/-/supabase-js-2.39.7.tgz#61c3277a94bd9fd0574b39ecdf4fecffd73a139c"
integrity sha512-1vxsX10Uhc2b+Dv9pRjBjHfqmw2N2h1PyTg9LEfICR3x2xwE24By1MGCjDZuzDKH5OeHCsf4it6K8KRluAAEXA==
dependencies:
"@supabase/functions-js" "2.1.5"
"@supabase/gotrue-js" "2.62.2"
"@supabase/node-fetch" "2.6.15"
"@supabase/postgrest-js" "1.9.2"
"@supabase/realtime-js" "2.9.3"
"@supabase/storage-js" "2.5.5"

"@types/[email protected]":
version "0.5.5"
resolved "https://registry.yarnpkg.com/@types/adm-zip/-/adm-zip-0.5.5.tgz#4588042726aa5f351d7ea88232e4a952f60e7c1a"
Expand Down Expand Up @@ -1592,6 +1649,11 @@
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.4.tgz#56e2cc26c397c038fab0e3a917a12d5c5909e901"
integrity sha512-37i+OaWTh9qeK4LSHPsyRC7NahnGotNuZvjLSgcPzblpHB3rrCJxAOgI5gCdKm7coonsaX1Of0ILiTcnZjbfxA==

"@types/phoenix@^1.5.4":
version "1.6.4"
resolved "https://registry.yarnpkg.com/@types/phoenix/-/phoenix-1.6.4.tgz#cceac93a827555473ad38057d1df7d06eef1ed71"
integrity sha512-B34A7uot1Cv0XtaHRYDATltAdKx0BvVKNgYNqE4WjtPUa4VQJM7kxeXcVKaH+KS+kCmZ+6w+QaUdcljiheiBJA==

"@types/[email protected]":
version "2.3.3"
resolved "https://registry.yarnpkg.com/@types/picomatch/-/picomatch-2.3.3.tgz#be60498568c19e989e43fb39aa84be1ed3655e92"
Expand All @@ -1612,6 +1674,13 @@
resolved "https://registry.yarnpkg.com/@types/sizzle/-/sizzle-2.3.8.tgz#518609aefb797da19bf222feb199e8f653ff7627"
integrity sha512-0vWLNK2D5MT9dg0iOo8GlKguPAU02QjmZitPEsXRuJXU/OGIOt9vT9Fc26wtYuavLxtO45v9PGleoL9Z0k1LHg==

"@types/ws@^8.5.10":
version "8.5.10"
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.10.tgz#4acfb517970853fa6574a3a6886791d04a396787"
integrity sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A==
dependencies:
"@types/node" "*"

"@types/yauzl@^2.9.1":
version "2.10.3"
resolved "https://registry.yarnpkg.com/@types/yauzl/-/yauzl-2.10.3.tgz#e9b2808b4f109504a03cda958259876f61017999"
Expand Down Expand Up @@ -5700,6 +5769,11 @@ tough-cookie@^4.1.3:
universalify "^0.2.0"
url-parse "^1.5.3"

tr46@~0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==

trim-newlines@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-3.0.1.tgz#260a5d962d8b752425b32f3a7db0dcacd176c144"
Expand Down Expand Up @@ -5971,6 +6045,19 @@ wcwidth@^1.0.1:
dependencies:
defaults "^1.0.3"

webidl-conversions@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==

whatwg-url@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"
integrity sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==
dependencies:
tr46 "~0.0.3"
webidl-conversions "^3.0.0"

which-boxed-primitive@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz#13757bc89b209b049fe5d86430e21cf40a89a8e6"
Expand Down Expand Up @@ -6070,6 +6157,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.6.tgz#5654ca8ecdeee47c33a9a4bf6d28e2be2980377c"
integrity sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A==

ws@^8.14.2:
version "8.16.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.16.0.tgz#d1cd774f36fbc07165066a60e40323eab6446fd4"
integrity sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==

xdg-basedir@^5.0.1:
version "5.1.0"
resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-5.1.0.tgz#1efba19425e73be1bc6f2a6ceb52a3d2c884c0c9"
Expand Down

0 comments on commit 5451a5b

Please sign in to comment.