Skip to content

Commit

Permalink
fix: env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Jun 4, 2024
1 parent 60d163f commit 46cda63
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
5 changes: 3 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
SUPABASE_URL=""
SUPABASE_ANON_KEY=""
SUPABASE_DB_PASSWORD=
SUPABASE_PROJECT_ID=
SUPABASE_ACCESS_TOKEN=
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
This is an up-to-date leaderboard of the top contributors to the Ubiquity ecosystem based on earnings from completed Devpool Directory bounties.

### TODO

- [ ] Pull data from Supabase instead of from static repo file
- [ ] Improve the hunter metadata/markers displayed in the additional details modal
- [ ] Add filters based on markers such as XP, Karma, Top n, etc.
- [ ] Add pagination and cap displayed entries?
- [ ] Add pagination and cap displayed entries?
11 changes: 6 additions & 5 deletions build/esbuild-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const esBuildContext: esbuild.BuildOptions = {
".json": "file",
},
outdir: "static/dist",
define: createEnvDefines(["SUPABASE_URL", "SUPABASE_ANON_KEY"], {
define: createEnvDefines(["SUPABASE_ACCESS_TOKEN", "SUPABASE_DB_PASSWORD", "SUPABASE_PROJECT_ID"], {
SUPABASE_STORAGE_KEY: generateSupabaseStorageKey(),
commitHash: execSync(`git rev-parse --short HEAD`).toString().trim(),
}),
Expand Down Expand Up @@ -54,13 +54,14 @@ function createEnvDefines(environmentVariables: string[], generatedAtBuild: Reco
}

export function generateSupabaseStorageKey(): string | null {
const SUPABASE_URL = process.env.SUPABASE_URL;
if (!SUPABASE_URL) {
console.error("SUPABASE_URL environment variable is not set");
const id = process.env.SUPABASE_PROJECT_ID;
if (!id) {
console.error("SUPABASE_PROJECT_ID environment variable is not set");
return null;
}
const url = `https://${id}.supabase.co`;

const urlParts = SUPABASE_URL.split(".");
const urlParts = url.split(".");
if (urlParts.length === 0) {
console.error("Invalid SUPABASE_URL environment variable");
return null;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@
"@commitlint/config-conventional"
]
}
}
}
7 changes: 3 additions & 4 deletions src/home/getters/get-github-access-token.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
declare const SUPABASE_STORAGE_KEY: string; // @DEV: passed in at build time check build/esbuild-build.ts
import { checkSupabaseSession } from "../rendering/render-github-login-button";
import { getLocalStore } from "./get-local-store";

declare const SUPABASE_PROJECT_ID: string; // @DEV: passed in at build time check build/esbuild-build.ts
export async function getGitHubAccessToken(): Promise<string | null> {
// better to use official function, looking up localstorage has flaws
const authToken = await checkSupabaseSession();

const expiresAt = authToken?.expires_at;
if (expiresAt && expiresAt < Date.now() / 1000) {
localStorage.removeItem(`sb-${SUPABASE_STORAGE_KEY}-auth-token`);
localStorage.removeItem(`sb-${SUPABASE_PROJECT_ID}-auth-token`);
return null;
}

return authToken?.provider_token ?? null;
}

export function getGitHubUserName(): string | null {
const authToken = getLocalStore(`sb-${SUPABASE_STORAGE_KEY}-auth-token`) as OauthToken | null;
const authToken = getLocalStore(`sb-${SUPABASE_PROJECT_ID}-auth-token`) as OauthToken | null;
return authToken?.user?.user_metadata?.user_name ?? null;
}

Expand Down
7 changes: 4 additions & 3 deletions src/home/rendering/render-github-login-button.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { createClient } from "@supabase/supabase-js";
import { toolbar } from "../ready-toolbar";

declare const SUPABASE_URL: string; // @DEV: passed in at build time check build/esbuild-build.ts
declare const SUPABASE_ANON_KEY: string; // @DEV: passed in at build time check build/esbuild-build.ts
declare const SUPABASE_PROJECT_ID: string; // @DEV: passed in at build time check build/esbuild-build.ts
declare const SUPABASE_DB_PASSWORD: string; // @DEV: passed in at build time check build/esbuild-build.ts

const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
const SUPABASE_URL = `https://${SUPABASE_PROJECT_ID}.supabase.co`;
const supabase = createClient(SUPABASE_URL, SUPABASE_DB_PASSWORD);

export function getSupabase() {
return supabase;
Expand Down

0 comments on commit 46cda63

Please sign in to comment.