Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.
feat(auth): return to the requested page after signing in
spacesdrive7c1ed91e7766
diff --git a/frontend/src/components/ProtectedRoute.tsx b/frontend/src/components/ProtectedRoute.tsxindex ae4959b..21386d2 100644--- a/frontend/src/components/ProtectedRoute.tsx+++ b/frontend/src/components/ProtectedRoute.tsx@@ -1,6 +1,7 @@import type React from 'react';-import { Navigate } from 'react-router-dom';+import { Navigate, useLocation } from 'react-router-dom';import { useAuth } from '../context/AuthContext';+import { signInPathFor } from '../lib/signInPage';import { LoadingState } from './StateViews';export interface ProtectedRouteProps {@@ -9,8 +10,10 @@ export interface ProtectedRouteProps {export default function ProtectedRoute({ children }: ProtectedRouteProps) {const { session } = useAuth();+ const location = useLocation();// the saved session is still being read; say so rather than show a blank pageif (session === undefined) return <LoadingState label="Checking your sign-in" className="min-h-dvh" />;- if (!session) return <Navigate to="/login" replace />;+ // the hash is left behind: it can carry a one-time code that belongs to the page, not to the sign-in+ if (!session) return <Navigate to={signInPathFor(location.pathname + location.search)} replace />;return children;}diff --git a/frontend/src/lib/signInPage.ts b/frontend/src/lib/signInPage.tsindex a82cbdc..c5a0426 100644--- a/frontend/src/lib/signInPage.ts+++ b/frontend/src/lib/signInPage.ts@@ -7,3 +7,17 @@ export function signInPageOutcome(hasSession: boolean | undefined, signingIn: boif (hasSession === undefined) return 'wait';return hasSession && !signingIn ? 'dashboard' : 'form';}++// where to go after signing in: only a path on this site, so a crafted link cannot send a fresh session elsewhere+export function returnPath(next: string | null): string {+ // browsers read a leading // or /\ as another host+ if (!next || !next.startsWith('/') || next.startsWith('//') || next.startsWith('/\\')) return '/';+ // whitespace and control characters never appear in the app's own paths+ if (/[\s\p{Cc}]/u.test(next)) return '/';+ return next;+}++// the sign-in address that brings a person back to the page they asked for+export function signInPathFor(path: string): string {+ return path === '/' ? '/login' : `/login?next=${encodeURIComponent(path)}`;+}diff --git a/frontend/src/pages/Login.tsx b/frontend/src/pages/Login.tsxindex 3f83143..a767e12 100644--- a/frontend/src/pages/Login.tsx+++ b/frontend/src/pages/Login.tsx@@ -13,7 +13,7 @@ import type { AuthErrorLike } from '@/lib/authErrors';import { PASSWORD_MIN_LENGTH, newPasswordProblem } from '@/lib/passwords';import { CODE_LENGTH } from '@/lib/authErrors';import { looksLikeEmail } from '@/lib/emailAddress';-import { signInPageOutcome } from '@/lib/signInPage';+import { returnPath, signInPageOutcome } from '@/lib/signInPage';import { FieldRow } from '@/components/DetailPanel';import { useDocumentTitle } from '@/hooks/use-document-title';import { useCountdown } from './useCountdown';@@ -104,6 +104,8 @@ export default function Login() {const navigate = useNavigate();const [params] = useSearchParams();const signingUp = params.get('mode') === 'signup';+ // a page that needed a sign-in, such as the Claude consent page, gets the person back after it+ const returnTo = returnPath(params.get('next'));const [view, setView] = useState<LoginView>('start');const [email, setEmail] = useState('');const [password, setPassword] = useState('');@@ -188,7 +190,7 @@ export default function Login() {async function submitCode() {const { error } = await verifySignInCode(email.trim(), code);if (error) throw error;- navigate('/');+ navigate(returnTo);}async function submitPassword() {@@ -202,7 +204,7 @@ export default function Login() {return;}if (error) throw error;- navigate('/');+ navigate(returnTo);}async function submitSignUp() {@@ -210,7 +212,7 @@ export default function Login() {if (error) throw error;// with Confirm email off the account is ready at onceif (data.session) {- navigate('/');+ navigate(returnTo);return;}if (isExistingAccount(data.user)) {@@ -225,7 +227,7 @@ export default function Login() {const { error } = await verifySignUpCode(email.trim(), code);if (error) throw error;toast.success('Email confirmed');- navigate('/');+ navigate(returnTo);}// Supabase answers the same whether or not the address has an account, so the message cannot say which@@ -250,7 +252,7 @@ export default function Login() {return;}toast.success('Password saved');- navigate('/');+ navigate(returnTo);}const SUBMIT: Record<LoginView, () => Promise<void>> = {@@ -310,7 +312,7 @@ export default function Login() {try {const { error } = await signInWithGoogle(idToken, nonce);if (error) throw error;- navigate('/');+ navigate(returnTo);} catch (err) {setProblem({ field: 'email', message: `Couldn't sign in with Google. ${(err as Error).message}` });} finally {@@ -321,7 +323,7 @@ export default function Login() {// the public site's Sign in and Get started buttons land here, so a stored session opens the dashboardconst outcome = signInPageOutcome(session === undefined ? undefined : !!session, signingIn);if (outcome === 'wait') return <p role="status" className="sr-only">Checking whether you are signed in</p>;- if (outcome === 'dashboard') return <Navigate to="/" replace />;+ if (outcome === 'dashboard') return <Navigate to={returnTo} replace />;return (<div className="relative min-h-svh bg-surface">diff --git a/frontend/tests/lib/signInPage.test.ts b/frontend/tests/lib/signInPage.test.tsindex 31153c4..e9492ba 100644--- a/frontend/tests/lib/signInPage.test.ts+++ b/frontend/tests/lib/signInPage.test.ts@@ -1,6 +1,6 @@import { test } from 'node:test';import assert from 'node:assert/strict';-import { signInPageOutcome } from '../../src/lib/signInPage.ts';+import { returnPath, signInPageOutcome, signInPathFor } from '../../src/lib/signInPage.ts';test('the page waits until the stored session has loaded', () => {assert.equal(signInPageOutcome(undefined, false), 'wait');@@ -18,3 +18,19 @@ test('someone signed out sees the form', () => {test('a session made by a sign-in on the page is left to finish, such as a reset saving the password', () => {assert.equal(signInPageOutcome(true, true), 'form');});++test('after signing in, a path on this site is kept', () => {+ assert.equal(returnPath('/oauth/consent?authorization_id=abc'), '/oauth/consent?authorization_id=abc');+ assert.equal(returnPath('/settings#claude'), '/settings#claude');+});++test('after signing in, anything that could leave the site goes to the dashboard', () => {+ for (const next of [null, '', 'https://evil.example', '//evil.example', '/\\evil.example', 'javascript:alert(1)', '/ok\npath', '/ok\tpath', '/a b']) {+ assert.equal(returnPath(next), '/', String(next));+ }+});++test('the sign-in address carries the page to return to', () => {+ assert.equal(signInPathFor('/'), '/login');+ assert.equal(signInPathFor('/oauth/consent?authorization_id=a1'), '/login?next=%2Foauth%2Fconsent%3Fauthorization_id%3Da1');+});