diff --git a/frontend/lib/auth.ts b/frontend/lib/auth.ts index 1233dc50..cb2119e0 100644 --- a/frontend/lib/auth.ts +++ b/frontend/lib/auth.ts @@ -1,9 +1,7 @@ import { GitHubMeta, JWTPayload } from "./types"; import { apiEndpoint } from "./const"; -export let githubMeta: GitHubMeta; - -export const fetchGitHubMeta = async () => { +export const fetchGitHubMeta = async (): Promise => { const res = await fetch(`${apiEndpoint}meta/github-app`, { method: "GET", }); @@ -12,7 +10,7 @@ export const fetchGitHubMeta = async () => { throw res; } - if (!githubMeta) githubMeta = (await res.json()).data; + return (await res.json()).data; }; export const handleOAuthToken = () => { diff --git a/frontend/lib/dom/comment.ts b/frontend/lib/dom/comment.ts index 15d2c392..c3886d8a 100644 --- a/frontend/lib/dom/comment.ts +++ b/frontend/lib/dom/comment.ts @@ -4,7 +4,7 @@ import iconEdit from "iconify/edit"; import iconDelete from "iconify/delete"; import iconInfoOutlineRounded from "iconify/info-outline-rounded"; import { Comment } from "../types"; -import { getJWT, decodeJWT, logout, githubMeta } from "../auth"; +import { getJWT, decodeJWT, logout, fetchGitHubMeta } from "../auth"; import { apiEndpoint } from "../const"; import { groupBy, dateTimeFormatter } from "../util"; @@ -691,10 +691,7 @@ export const renderComments = async (comments: Comment[]) => { break; } case "login": { - if (!githubMeta) { - console.log("githubMeta not ready"); - return; - } + const githubMeta = await fetchGitHubMeta(); window.location.href = `https://github.com/login/oauth/authorize?client_id=${githubMeta.client_id}&state=${encodeURIComponent(JSON.stringify({ redirect: window.location.href }))}`; break; } diff --git a/frontend/lib/main.ts b/frontend/lib/main.ts index cbe41958..00df6ec7 100644 --- a/frontend/lib/main.ts +++ b/frontend/lib/main.ts @@ -2,7 +2,7 @@ import "./style.css"; import iconComment from "iconify/comment-outline-rounded"; import iconClose from "iconify/close"; import { setApiEndpoint } from "./const"; -import { fetchGitHubMeta, handleOAuthToken } from "./auth"; +import { handleOAuthToken } from "./auth"; import { closeContextMenu, createContextMenu, @@ -71,7 +71,6 @@ export function setupReview( resetCommentsCache(); updateAvailableComments(); - fetchGitHubMeta(); if (globalInitialized) { closeCommentsPanel();