diff --git a/build/plugins/invert-colors.ts b/build/plugins/invert-colors.ts index 7470b05f..a0b3edfb 100644 --- a/build/plugins/invert-colors.ts +++ b/build/plugins/invert-colors.ts @@ -16,7 +16,7 @@ export const invertColors: esbuild.Plugin = { if (color.length === 3) { color = color .split("") - .map((char) => char + char) + .map((char: string) => char + char) .join(""); } const r = parseInt(color.slice(0, 2), 16); diff --git a/src/home/authenticated-get-github-user.ts b/src/home/authenticated-get-github-user.ts index cda4751a..60545883 100644 --- a/src/home/authenticated-get-github-user.ts +++ b/src/home/authenticated-get-github-user.ts @@ -1,5 +1,5 @@ import { Octokit } from "@octokit/rest"; -import { getExistingSessionToken } from "./check-for-github-access-token"; +import { getLocalStoreOauth } from "./check-for-github-access-token"; export async function authenticatedGetGitHubUser(): Promise { const activeSessionToken = await getActiveSessionToken(); @@ -11,39 +11,28 @@ export async function authenticatedGetGitHubUser(): Promise { } async function getActiveSessionToken(): Promise { - let token = getExistingSessionToken(); - if (!token) { - token = await getNewSessionToken(); + const cachedSessionToken = getLocalStoreOauth(); + if (cachedSessionToken) { + return cachedSessionToken.provider_token; } - if (!token) { - console.error("No token found"); + + const newSessionToken = await getNewSessionToken(); + if (newSessionToken) { + return newSessionToken; } - return token; + + console.error("No session token found"); + + return null; } async function getNewSessionToken(): Promise { const hash = window.location.hash; const params = new URLSearchParams(hash.substr(1)); // remove the '#' and parse - // access_token=eyJhbGciOiJIUzI1NiIsImtpZCI6InJCQVV5bHBBeUN5Sk1LVUIiLCJ0eXAiOiJKV1QifQ.eyJhdWQiOiJhdXRoZW50aWNhdGVkIiwiZXhwIjoxNzAxMzQ0NjY0LCJpYXQiOjE3MDEzNDEwNjQsImlzcyI6Imh0dHBzOi8vd2Z6cGV3bWx5aW96dXB1bGJ1dXIuc3VwYWJhc2UuY28vYXV0aC92MSIsInN1YiI6IjE3NmRlZjk0LWNiMGEtNGNlYi1iMWMwLTg1ODJiMDlmZmE5ZCIsImVtYWlsIjoiZ2l0aHViQHBhdmxvdmNpay5jb20iLCJwaG9uZSI6IiIsImFwcF9tZXRhZGF0YSI6eyJwcm92aWRlciI6ImdpdGh1YiIsInByb3ZpZGVycyI6WyJnaXRodWIiXX0sInVzZXJfbWV0YWRhdGEiOnsiYXZhdGFyX3VybCI6Imh0dHBzOi8vYXZhdGFycy5naXRodWJ1c2VyY29udGVudC5jb20vdS80OTc1NjcwP3Y9NCIsImVtYWlsIjoiZ2l0aHViQHBhdmxvdmNpay5jb20iLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiZnVsbF9uYW1lIjoi44Ki44Os44Kv44K144Oz44OA44O8LmV0aCIsImlzcyI6Imh0dHBzOi8vYXBpLmdpdGh1Yi5jb20iLCJuYW1lIjoi44Ki44Os44Kv44K144Oz44OA44O8LmV0aCIsInBob25lX3ZlcmlmaWVkIjpmYWxzZSwicHJlZmVycmVkX3VzZXJuYW1lIjoicGF2bG92Y2lrIiwicHJvdmlkZXJfaWQiOiI0OTc1NjcwIiwic3ViIjoiNDk3NTY3MCIsInVzZXJfbmFtZSI6InBhdmxvdmNpayJ9LCJyb2xlIjoiYXV0aGVudGljYXRlZCIsImFhbCI6ImFhbDEiLCJhbXIiOlt7Im1ldGhvZCI6Im9hdXRoIiwidGltZXN0YW1wIjoxNzAxMzQxMDY0fV0sInNlc3Npb25faWQiOiI0YzBlNjA5MC0zZGJmLTQ4OTMtYTRjYi1lYTlmZTIwZDQ1YjcifQ.YgLDGqngdIBCO2o041Xv0UzymdMgYQlW8GLBmdfDKkM - // &expires_at=1701344664 - // &expires_in=3600 - // &provider_token=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 - // &refresh_token=Zi1ixXNvljvBkqexEriiVA - // &token_type=bearer const providerToken = params.get("provider_token"); - if (!providerToken) { - // throw new Error("Access token not found in URL fragment"); return null; } - - const expiresAt = params.get("expires_at"); - if (expiresAt && parseInt(expiresAt, 10) < Date.now() / 1000) { - localStorage.removeItem("provider_token"); - } else if (providerToken) { - localStorage.setItem("provider_token", providerToken); - } - return providerToken; } diff --git a/src/home/check-for-github-access-token.ts b/src/home/check-for-github-access-token.ts index f210e54f..afac6c37 100644 --- a/src/home/check-for-github-access-token.ts +++ b/src/home/check-for-github-access-token.ts @@ -1,26 +1,84 @@ import { renderGitHubLoginButton } from "./github-login-button"; -let gitHubAccessToken: null | string = null; - export function checkForGitHubAccessToken(): string | null { - const accessToken = localStorage.getItem("provider_token"); - const expiresAt = localStorage.getItem("expires_at"); + const oauthToken = getLocalStoreOauth(); - if (expiresAt && parseInt(expiresAt, 10) < Date.now() / 1000) { - // expired - localStorage.removeItem("provider_token"); - localStorage.removeItem("expires_at"); + const expiresAt = oauthToken?.expires_at; + if (expiresAt) { + if (expiresAt < Date.now() / 1000) { + localStorage.removeItem("sb-wfzpewmlyiozupulbuur-auth-token"); + } } + const accessToken = oauthToken?.provider_token; if (accessToken) { - gitHubAccessToken = accessToken; return accessToken; } else { renderGitHubLoginButton(); - return null; } + return null; +} + +export function getLocalStoreOauth(): ExampleAuthToken | null { + const oauthToken = localStorage.getItem("sb-wfzpewmlyiozupulbuur-auth-token"); + if (!oauthToken) return null; + return JSON.parse(oauthToken); } -export function getExistingSessionToken() { - return gitHubAccessToken; +interface ExampleAuthToken { + 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; + }; } diff --git a/src/home/display-github-issues.ts b/src/home/display-github-issues.ts index 5262f298..ce12cb12 100644 --- a/src/home/display-github-issues.ts +++ b/src/home/display-github-issues.ts @@ -1,6 +1,6 @@ import { Octokit } from "@octokit/rest"; -import { homeController } from "./home-controller"; import { GitHubIssue } from "./github-types"; +import { homeController } from "./home-controller"; export type GitHubIssueWithNewFlag = GitHubIssue & { isNew?: boolean }; @@ -70,21 +70,20 @@ async function fetchIssues(container: HTMLDivElement, accessToken: string | null console.error(error); } // Fetch fresh issues and mark them as new - const freshIssues: GitHubIssueWithNewFlag[] = ( - await octokit.paginate("GET /repos/ubiquity/devpool-directory/issues", { - state: "open", - }) - ).map((issue) => ({ ...issue, isNew: true })); + const freshIssues: GitHubIssue[] = await octokit.paginate("GET /repos/ubiquity/devpool-directory/issues", { + state: "open", + }); + const freshIssuesWithNewFlag = freshIssues.map((issue) => ({ ...issue, isNew: true })) as GitHubIssueWithNewFlag[]; // Sort the fresh issues - const sortedIssuesByTime = sortIssuesByTime(freshIssues); + const sortedIssuesByTime = sortIssuesByTime(freshIssuesWithNewFlag); const sortedIssuesByPriority = sortIssuesByPriority(sortedIssuesByTime); // Pass the fresh issues to the homeController await homeController(container, sortedIssuesByPriority); // Remove the 'isNew' flag before saving to localStorage - const issuesToSave = freshIssues.map(({ ...issue }) => { + const issuesToSave = freshIssuesWithNewFlag.map(({ ...issue }) => { delete issue.isNew; return issue; }); diff --git a/src/home/display-github-user-information.ts b/src/home/display-github-user-information.ts index 6dc6943c..ae2bd731 100644 --- a/src/home/display-github-user-information.ts +++ b/src/home/display-github-user-information.ts @@ -35,7 +35,7 @@ async function signOut() { console.error("Error logging out:", error); alert(error); } - localStorage.removeItem("provider_token"); - localStorage.removeItem("expires_at"); + // localStorage.removeItem("provider_token"); + // localStorage.removeItem("expires_at"); window.location.reload(); } diff --git a/src/home/github-login-button.ts b/src/home/github-login-button.ts index 35c8aafc..13aa3162 100644 --- a/src/home/github-login-button.ts +++ b/src/home/github-login-button.ts @@ -12,13 +12,7 @@ export function getSupabase() { } async function gitHubLoginButton() { - const { error } = await supabase.auth.signInWithOAuth({ - provider: "github", - options: { - redirectTo: "https://wfzpewmlyiozupulbuur.supabase.co/auth/v1/callback", - }, - }); - + const { error } = await supabase.auth.signInWithOAuth({ provider: "github" }); if (error) { console.error("Error logging in:", error); }