Repository navigation
fix: an expired IAP sign-in reloads the page instead of failing every background request #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { useEffect, useSyncExternalStore } from 'react'; | ||
| import { isSignedOut, reloadIfSafe, subscribeSignedOut } from '../../lib/sign-in'; | ||
|
|
||
| export function SignedOutBanner() { | ||
| const signedOut = useSyncExternalStore(subscribeSignedOut, isSignedOut, () => false); | ||
|
|
||
| useEffect(() => { | ||
| if (!signedOut) { | ||
| return; | ||
| } | ||
| reloadIfSafe(); | ||
| const onVisibility = () => { | ||
| if (document.visibilityState === 'visible') { | ||
| reloadIfSafe(); | ||
| } | ||
| }; | ||
| document.addEventListener('visibilitychange', onVisibility); | ||
| return () => document.removeEventListener('visibilitychange', onVisibility); | ||
| }, [signedOut]); | ||
|
|
||
| if (!signedOut) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <div | ||
| role="alert" | ||
| data-testid="signed-out-banner" | ||
| className="fixed top-0 inset-x-0 z-50 flex items-center gap-2 px-4 py-2 border-b border-deleted/40 bg-bg-secondary text-xs text-text" | ||
| > | ||
| <span className="font-medium">Your sign-in expired. Reload to sign in again.</span> | ||
| <button | ||
| type="button" | ||
| onClick={() => window.location.reload()} | ||
| className="px-2 py-0.5 rounded border border-accent/60 text-accent hover:bg-accent/15 cursor-pointer" | ||
| > | ||
| Reload | ||
| </button> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { markSignedOut, writeSettled, writeStarted } from './sign-in'; | ||
|
|
||
| /** | ||
| * The server never redirects these requests; a redirect is IAP sending an expired sign-in to Google, which | ||
| * a fetch cannot follow. A 401 is diffity's own sign-in gone. A write may hold the reader's only copy of their | ||
| * text, so while one is under way, or after one failed this way, the page must not reload on its own. | ||
| */ | ||
| export async function apiRequest(url: string, init?: RequestInit): Promise<Response> { | ||
| const write = (init?.method ?? 'GET').toUpperCase() !== 'GET'; | ||
| if (write) { | ||
| writeStarted(); | ||
| } | ||
| try { | ||
| const res = await fetch(url, { ...init, redirect: 'manual' }); | ||
| if (res.type === 'opaqueredirect' || res.status === 401) { | ||
| markSignedOut({ lostWrite: write }); | ||
| throw new Error('Signed out'); | ||
| } | ||
| return res; | ||
| } finally { | ||
| if (write) { | ||
| writeSettled(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| let signedOut = false; | ||
| let reloaded = false; | ||
| let lostWrite = false; | ||
| let writesInFlight = 0; | ||
| const listeners = new Set<() => void>(); | ||
|
|
||
| export function isSignedOut(): boolean { | ||
| return signedOut; | ||
| } | ||
|
|
||
| export function markSignedOut(cause: { lostWrite?: boolean } = {}): void { | ||
| lostWrite ||= !!cause.lostWrite; | ||
| if (signedOut) { | ||
| return; | ||
| } | ||
| signedOut = true; | ||
| for (const listener of listeners) { | ||
| listener(); | ||
| } | ||
| } | ||
|
|
||
| export function writeStarted(): void { | ||
| writesInFlight++; | ||
| } | ||
|
|
||
| export function writeSettled(): void { | ||
| writesInFlight--; | ||
| } | ||
|
|
||
| export function subscribeSignedOut(listener: () => void): () => void { | ||
| listeners.add(listener); | ||
| return () => { | ||
| listeners.delete(listener); | ||
| }; | ||
| } | ||
|
|
||
| export interface PageState { | ||
| visible: boolean; | ||
| /** Something the reader typed that a reload would throw away. */ | ||
| hasDraft: boolean; | ||
| editing: boolean; | ||
| /** A save is under way, or failed on the expired sign-in; its text may live only in memory. */ | ||
| unsavedWrite: boolean; | ||
| } | ||
|
|
||
| export function shouldAutoReload(state: PageState, alreadyReloaded: boolean): boolean { | ||
| return !alreadyReloaded && !state.unsavedWrite && state.visible && !state.hasDraft && !state.editing; | ||
| } | ||
|
|
||
| const TEXT_INPUT_TYPES = new Set(['', 'text', 'search', 'email', 'url', 'tel', 'password', 'number']); | ||
|
|
||
| export function readPageState(doc: Document = document): PageState { | ||
| const fields = [ | ||
| ...doc.querySelectorAll('textarea'), | ||
| ...[...doc.querySelectorAll('input')].filter(input => TEXT_INPUT_TYPES.has(input.getAttribute('type')?.toLowerCase() ?? '')), | ||
| ]; | ||
| const active = doc.activeElement; | ||
| const editing = active instanceof HTMLElement | ||
| && (active.isContentEditable || active instanceof HTMLInputElement || active instanceof HTMLTextAreaElement || active instanceof HTMLSelectElement); | ||
| return { | ||
| visible: doc.visibilityState === 'visible', | ||
| hasDraft: fields.some(field => field.value.trim() !== ''), | ||
| editing, | ||
| unsavedWrite: lostWrite || writesInFlight > 0, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * An expired IAP sign-in can only be renewed by loading the page again. Does that once per page load, and | ||
| * only when nothing the reader is in the middle of would be lost. | ||
| */ | ||
| export function reloadIfSafe(reload: () => void = () => window.location.reload(), state: PageState = readPageState()): boolean { | ||
| if (!shouldAutoReload(state, reloaded)) { | ||
| return false; | ||
| } | ||
| reloaded = true; | ||
| reload(); | ||
| return true; | ||
| } | ||
|
|
||
| export function resetSignInForTests(): void { | ||
| signedOut = false; | ||
| reloaded = false; | ||
| lostWrite = false; | ||
| writesInFlight = 0; | ||
| listeners.clear(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.