Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

Restore the fediverse code-entry step after a page reload (#4986)

Gabe Kangasc7b7362da517
diff --git a/web/components/common/UserDropdown/UserDropdown.tsx b/web/components/common/UserDropdown/UserDropdown.tsx
index 022de25985..70c12b9796 100644
--- a/web/components/common/UserDropdown/UserDropdown.tsx
+++ b/web/components/common/UserDropdown/UserDropdown.tsx
@@ -2,7 +2,7 @@ import { MenuProps, Dropdown, Button } from 'antd';
import classnames from 'classnames';
import { useRecoilState, useRecoilValue } from 'recoil';
-import { FC, useState } from 'react';
+import { FC, useEffect, useState } from 'react';
import { useHotkeys } from 'react-hotkeys-hook';
import dynamic from 'next/dynamic';
import { ErrorBoundary } from 'react-error-boundary';
@@ -15,6 +15,7 @@ import {
import styles from './UserDropdown.module.scss';
import { AppStateOptions } from '../../stores/application-state';
import { ComponentError } from '../../ui/ComponentError/ComponentError';
+import { getPendingFediverseAuth } from '../../../utils/fediverseAuthSession';
// Lazy loaded components
@@ -83,6 +84,15 @@ export const UserDropdown: FC<UserDropdownProps> = ({
const [popupWindow, setPopupWindow] = useState<Window>(null);
const appState = useRecoilValue<AppStateOptions>(appStateAtom);
+ // Reopen the auth modal on load if a fediverse verification was still in
+ // progress, so a viewer who reloaded mid-flow (issue #4887) lands back on the
+ // code-entry step instead of losing it.
+ useEffect(() => {
+ if (getPendingFediverseAuth()) {
+ setShowAuthModal(true);
+ }
+ }, []);
+
const toggleChatVisibility = () => {
// If we don't support the hide chat option then don't do anything.
if (!showHideChatOption) {
diff --git a/web/components/modals/AuthModal/AuthModal.tsx b/web/components/modals/AuthModal/AuthModal.tsx
index 2e78229c54..8c164b41ac 100644
--- a/web/components/modals/AuthModal/AuthModal.tsx
+++ b/web/components/modals/AuthModal/AuthModal.tsx
@@ -4,6 +4,7 @@ import { FC } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
import { IndieAuthModal } from '../IndieAuthModal/IndieAuthModal';
import { FediAuthModal } from '../FediAuthModal/FediAuthModal';
+import { getPendingFediverseAuth } from '../../../utils/fediverseAuthSession';
import styles from './AuthModal.module.scss';
import {
@@ -67,6 +68,11 @@ export const AuthModal: FC<AuthModalProps> = ({ forceTabs }) => {
{ label: fediAuthTabTitle, key: '2', children: fediAuthTab },
];
+ // If a fediverse verification is still in progress (restored after a reload),
+ // open straight to that tab so the recovered code-entry step is visible. Only
+ // when federation is enabled, since the FediAuth tab is unreachable otherwise.
+ const defaultActiveKey = fediverseEnabled && getPendingFediverseAuth() ? '2' : '1';
+
return (
<ErrorBoundary
// eslint-disable-next-line react/no-unstable-nested-components
@@ -80,7 +86,7 @@ export const AuthModal: FC<AuthModalProps> = ({ forceTabs }) => {
>
<div>
<Tabs
- defaultActiveKey="1"
+ defaultActiveKey={defaultActiveKey}
items={items}
type="card"
size="small"
diff --git a/web/components/modals/FediAuthModal/FediAuthModal.tsx b/web/components/modals/FediAuthModal/FediAuthModal.tsx
index df242917a1..0636ad91fd 100644
--- a/web/components/modals/FediAuthModal/FediAuthModal.tsx
+++ b/web/components/modals/FediAuthModal/FediAuthModal.tsx
@@ -1,8 +1,13 @@
import { Alert, Button, Input, Space, Spin, Collapse } from 'antd';
-import React, { FC, useState } from 'react';
+import React, { FC, useEffect, useState } from 'react';
import dynamic from 'next/dynamic';
import styles from './FediAuthModal.module.scss';
import { isValidFediverseAccount } from '../../../utils/validators';
+import {
+ getPendingFediverseAuth,
+ setPendingFediverseAuth,
+ clearPendingFediverseAuth,
+} from '../../../utils/fediverseAuthSession';
const { Panel } = Collapse;
@@ -30,6 +35,17 @@ export const FediAuthModal: FC<FediAuthModalProps> = ({
const [code, setCode] = useState('');
const [verifyingCode, setVerifyingCode] = useState(false);
+ // Restore an in-progress verification if the viewer left and came back (e.g.
+ // backgrounded the tab to copy their code) within the OTP lifetime.
+ useEffect(() => {
+ const pending = getPendingFediverseAuth();
+ if (pending) {
+ setAccount(pending.account);
+ setValid(isValidFediverseAccount(pending.account));
+ setVerifyingCode(true);
+ }
+ }, []);
+
const message = !authenticated ? (
<span>
Receive a direct message on the Fediverse to link your account to{' '}
@@ -94,7 +110,8 @@ export const FediAuthModal: FC<FediAuthModalProps> = ({
try {
await makeRequest(url, data);
- // Success. Reload the page.
+ // Verified. Drop the persisted state and reload the page.
+ clearPendingFediverseAuth();
window.location.href = '/';
} catch (e) {
console.error(e);
@@ -116,6 +133,7 @@ export const FediAuthModal: FC<FediAuthModalProps> = ({
try {
await makeRequest(url, data);
+ setPendingFediverseAuth(normalizedAccount);
setVerifyingCode(true);
} catch (e) {
console.error(e);
diff --git a/web/tests/fediverseAuthSession.test.ts b/web/tests/fediverseAuthSession.test.ts
new file mode 100644
index 0000000000..808992af5d
--- /dev/null
+++ b/web/tests/fediverseAuthSession.test.ts
@@ -0,0 +1,39 @@
+import {
+ parsePendingFediverseAuth,
+ PENDING_FEDIVERSE_AUTH_EXPIRY_MS,
+} from '../utils/fediverseAuthSession';
+
+describe('parsePendingFediverseAuth', () => {
+ const now = 1_700_000_000_000;
+ const account = '[email protected]';
+
+ test('returns the account for a fresh entry', () => {
+ const raw = JSON.stringify({ account, ts: now });
+ expect(parsePendingFediverseAuth(raw, now)).toEqual({ account, ts: now });
+ });
+
+ test('keeps an entry right at the expiry boundary', () => {
+ const raw = JSON.stringify({ account, ts: now - PENDING_FEDIVERSE_AUTH_EXPIRY_MS });
+ expect(parsePendingFediverseAuth(raw, now)).toEqual({
+ account,
+ ts: now - PENDING_FEDIVERSE_AUTH_EXPIRY_MS,
+ });
+ });
+
+ test('drops an entry past the OTP lifetime so the viewer is not stranded', () => {
+ const raw = JSON.stringify({ account, ts: now - PENDING_FEDIVERSE_AUTH_EXPIRY_MS - 1 });
+ expect(parsePendingFediverseAuth(raw, now)).toBeNull();
+ });
+
+ test('drops entries with a future or non-finite timestamp', () => {
+ expect(parsePendingFediverseAuth(JSON.stringify({ account, ts: now + 1 }), now)).toBeNull();
+ expect(parsePendingFediverseAuth(`{"account":"${account}","ts":1e999}`, now)).toBeNull();
+ });
+
+ test('returns null for missing or malformed data', () => {
+ expect(parsePendingFediverseAuth(null, now)).toBeNull();
+ expect(parsePendingFediverseAuth('not json', now)).toBeNull();
+ expect(parsePendingFediverseAuth(JSON.stringify({ ts: now }), now)).toBeNull();
+ expect(parsePendingFediverseAuth(JSON.stringify({ account }), now)).toBeNull();
+ });
+});
diff --git a/web/utils/fediverseAuthSession.ts b/web/utils/fediverseAuthSession.ts
new file mode 100644
index 0000000000..e43c9ecbca
--- /dev/null
+++ b/web/utils/fediverseAuthSession.ts
@@ -0,0 +1,83 @@
+// Persists an in-progress fediverse OTP verification across a page reload so a
+// viewer who backgrounds the tab to copy their code (issue #4887) returns to the
+// code-entry step instead of an empty account form.
+//
+// The window mirrors the backend OTP lifetime (auth/fediverse registrationTimeout
+// is 10 minutes). Past that the code the viewer received is dead, so we drop the
+// persisted state and send them back to the account step rather than stranding
+// them at a code box that can never succeed.
+
+const PENDING_FEDIVERSE_AUTH_KEY = 'pendingFediverseAuth';
+export const PENDING_FEDIVERSE_AUTH_EXPIRY_MS = 10 * 60 * 1000;
+
+export type PendingFediverseAuth = {
+ account: string;
+ ts: number;
+};
+
+// Pure decode + expiry check, kept separate from storage I/O so it can be tested
+// with a plain string and clock value.
+export function parsePendingFediverseAuth(
+ raw: string | null,
+ now: number,
+): PendingFediverseAuth | null {
+ if (!raw) {
+ return null;
+ }
+
+ let parsed: PendingFediverseAuth;
+ try {
+ parsed = JSON.parse(raw) as PendingFediverseAuth;
+ } catch {
+ return null;
+ }
+
+ if (!parsed || typeof parsed.account !== 'string' || typeof parsed.ts !== 'number') {
+ return null;
+ }
+
+ // Reject non-finite or future timestamps so a bad value can't pin the modal
+ // open forever (Infinity or a future ts never crosses the expiry check).
+ if (!Number.isFinite(parsed.ts) || parsed.ts > now) {
+ return null;
+ }
+
+ if (now - parsed.ts > PENDING_FEDIVERSE_AUTH_EXPIRY_MS) {
+ return null;
+ }
+
+ return parsed;
+}
+
+export function getPendingFediverseAuth(): PendingFediverseAuth | null {
+ try {
+ const result = parsePendingFediverseAuth(
+ sessionStorage.getItem(PENDING_FEDIVERSE_AUTH_KEY),
+ Date.now(),
+ );
+ if (!result) {
+ sessionStorage.removeItem(PENDING_FEDIVERSE_AUTH_KEY);
+ }
+ return result;
+ } catch (e) {
+ console.error(e);
+ return null;
+ }
+}
+
+export function setPendingFediverseAuth(account: string): void {
+ try {
+ const value: PendingFediverseAuth = { account, ts: Date.now() };
+ sessionStorage.setItem(PENDING_FEDIVERSE_AUTH_KEY, JSON.stringify(value));
+ } catch (e) {
+ console.error(e);
+ }
+}
+
+export function clearPendingFediverseAuth(): void {
+ try {
+ sessionStorage.removeItem(PENDING_FEDIVERSE_AUTH_KEY);
+ } catch (e) {
+ console.error(e);
+ }
+}