diff --git a/src/home/authentication.ts b/src/home/authentication.ts index bdac7ab7..1a263ceb 100644 --- a/src/home/authentication.ts +++ b/src/home/authentication.ts @@ -5,7 +5,7 @@ import { displayGitHubUserInformation } from "./rendering/display-github-user-in import { renderGitHubLoginButton } from "./rendering/render-github-login-button"; export async function authentication() { - const accessToken = getGitHubAccessToken(); + const accessToken = await getGitHubAccessToken(); if (!accessToken) { renderGitHubLoginButton(); } diff --git a/src/home/fetch-github/fetch-and-display-previews.ts b/src/home/fetch-github/fetch-and-display-previews.ts index c2c714c8..3718bace 100644 --- a/src/home/fetch-github/fetch-and-display-previews.ts +++ b/src/home/fetch-github/fetch-and-display-previews.ts @@ -1,3 +1,4 @@ +import { getGitHubAccessToken } from "../getters/get-github-access-token"; import { getImageFromCache } from "../getters/get-indexed-db"; import { getLocalStore } from "../getters/get-local-store"; import { GITHUB_TASKS_STORAGE_KEY, TaskStorageItems } from "../github-types"; @@ -16,11 +17,26 @@ export type Options = { export async function fetchAndDisplayPreviewsFromCache(sorting?: Sorting, options = { ordering: "normal" }) { let _cachedTasks = getLocalStore(GITHUB_TASKS_STORAGE_KEY) as TaskStorageItems; + const _accessToken = await getGitHubAccessToken(); + + // Refresh the storage if there is no logged-in object in cachedTasks but there is one now. + if (_cachedTasks && !_cachedTasks.loggedIn && _accessToken) { + localStorage.removeItem(GITHUB_TASKS_STORAGE_KEY); + return fetchAndDisplayPreviewsFromNetwork(sorting, options); + } + + // If previously logged in but not anymore, clear cache and fetch from network. + if (_cachedTasks && _cachedTasks.loggedIn && !_accessToken) { + localStorage.removeItem(GITHUB_TASKS_STORAGE_KEY); + return fetchAndDisplayPreviewsFromNetwork(sorting, options); + } + // makes sure tasks have a timestamp to know how old the cache is, or refresh if older than 15 minutes if (!_cachedTasks || !_cachedTasks.timestamp || _cachedTasks.timestamp + 60 * 1000 * 15 <= Date.now()) { _cachedTasks = { timestamp: Date.now(), tasks: [], + loggedIn: _accessToken !== null, }; } const cachedTasks = _cachedTasks.tasks.map((task) => ({ ...task, isNew: false, isModified: false })) as TaskMaybeFull[]; diff --git a/src/home/fetch-github/fetch-avatar.ts b/src/home/fetch-github/fetch-avatar.ts index 4e368ad7..0f04c081 100644 --- a/src/home/fetch-github/fetch-avatar.ts +++ b/src/home/fetch-github/fetch-avatar.ts @@ -19,7 +19,7 @@ export async function fetchAvatar(orgName: string) { } // If not in IndexedDB, fetch from network - const octokit = new Octokit({ auth: getGitHubAccessToken() }); + const octokit = new Octokit({ auth: await getGitHubAccessToken() }); try { const { data: { avatar_url: avatarUrl }, diff --git a/src/home/fetch-github/fetch-issues-full.ts b/src/home/fetch-github/fetch-issues-full.ts index f257f9a6..b10df1b4 100644 --- a/src/home/fetch-github/fetch-issues-full.ts +++ b/src/home/fetch-github/fetch-issues-full.ts @@ -8,7 +8,7 @@ import { TaskMaybeFull, TaskWithFull } from "./preview-to-full-mapping"; export const organizationImageCache = new Map(); export async function fetchIssuesFull(taskPreviews: TaskMaybeFull[]): Promise { - const octokit = new Octokit({ auth: getGitHubAccessToken() }); + const octokit = new Octokit({ auth: await getGitHubAccessToken() }); const urlPattern = /https:\/\/github\.com\/(?[^/]+)\/(?[^/]+)\/issues\/(?\d+)/; const fullTaskPromises = taskPreviews.map(async (task) => { diff --git a/src/home/fetch-github/fetch-issues-preview.ts b/src/home/fetch-github/fetch-issues-preview.ts index 5b67d4c2..21960918 100644 --- a/src/home/fetch-github/fetch-issues-preview.ts +++ b/src/home/fetch-github/fetch-issues-preview.ts @@ -6,7 +6,7 @@ import { displayPopupMessage } from "../rendering/display-popup-modal"; import { TaskNoFull } from "./preview-to-full-mapping"; async function checkPrivateRepoAccess(): Promise { - const octokit = new Octokit({ auth: getGitHubAccessToken() }); + const octokit = new Octokit({ auth: await getGitHubAccessToken() }); const username = getGitHubUserName(); try { @@ -34,7 +34,7 @@ async function checkPrivateRepoAccess(): Promise { } export async function fetchIssuePreviews(): Promise { - const octokit = new Octokit({ auth: getGitHubAccessToken() }); + const octokit = new Octokit({ auth: await getGitHubAccessToken() }); let freshIssues: GitHubIssue[] = []; let hasPrivateRepoAccess = false; // Flag to track access to the private repository diff --git a/src/home/getters/get-github-access-token.ts b/src/home/getters/get-github-access-token.ts index da4992c2..d02dd8d2 100644 --- a/src/home/getters/get-github-access-token.ts +++ b/src/home/getters/get-github-access-token.ts @@ -1,8 +1,10 @@ import { SUPABASE_STORAGE_KEY } from "../github-types"; +import { checkSupabaseSession } from "../rendering/render-github-login-button"; import { getLocalStore } from "./get-local-store"; -export function getGitHubAccessToken(): string | null { - const oauthToken = getLocalStore(`sb-${SUPABASE_STORAGE_KEY}-auth-token`) as OAuthToken | null; +export async function getGitHubAccessToken(): Promise { + // better to use official function, looking up localstorage has flaws + const oauthToken = await checkSupabaseSession(); const expiresAt = oauthToken?.expires_at; if (expiresAt) { diff --git a/src/home/github-types.ts b/src/home/github-types.ts index 29fe5192..76e791e1 100644 --- a/src/home/github-types.ts +++ b/src/home/github-types.ts @@ -73,4 +73,5 @@ export const SUPABASE_STORAGE_KEY = supabaseUrl.substring(supabaseUrl.lastIndexO export type TaskStorageItems = { timestamp: number; tasks: TaskNoState[]; + loggedIn: boolean; }; diff --git a/src/home/home.ts b/src/home/home.ts index 0f675100..541a1df1 100644 --- a/src/home/home.ts +++ b/src/home/home.ts @@ -25,7 +25,7 @@ void (async function home() { const fullTasks = await fetchIssuesFull(previews); taskManager.syncTasks(fullTasks); console.trace({ fullTasks }); - taskManager.writeToStorage(); + await taskManager.writeToStorage(); return fullTasks; } catch (error) { console.error(error); diff --git a/src/home/rendering/render-github-login-button.ts b/src/home/rendering/render-github-login-button.ts index 5c724440..08a22b80 100644 --- a/src/home/rendering/render-github-login-button.ts +++ b/src/home/rendering/render-github-login-button.ts @@ -12,6 +12,14 @@ export function getSupabase() { return supabase; } +export async function checkSupabaseSession() { + const { + data: { session }, + } = await supabase.auth.getSession(); + + return session; +} + async function gitHubLoginButtonHandler() { const { error } = await supabase.auth.signInWithOAuth({ provider: "github" }); if (error) { diff --git a/src/home/task-manager.ts b/src/home/task-manager.ts index 50db8177..59bde855 100644 --- a/src/home/task-manager.ts +++ b/src/home/task-manager.ts @@ -1,4 +1,5 @@ import { TaskMaybeFull } from "./fetch-github/preview-to-full-mapping"; +import { getGitHubAccessToken } from "./getters/get-github-access-token"; import { setLocalStore } from "./getters/get-local-store"; import { GITHUB_TASKS_STORAGE_KEY } from "./github-types"; @@ -50,7 +51,8 @@ export class TaskManager { return this._container; } - public writeToStorage() { - setLocalStore(GITHUB_TASKS_STORAGE_KEY, { timestamp: Date.now(), tasks: this._tasks }); + public async writeToStorage() { + const _accessToken = await getGitHubAccessToken(); + setLocalStore(GITHUB_TASKS_STORAGE_KEY, { timestamp: Date.now(), tasks: this._tasks, loggedIn: _accessToken !== null }); } }