Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

fix(auth): open the dashboard from the sign-in page when signed in

spacesdriveac42b8987656
diff --git a/frontend/src/lib/signInPage.ts b/frontend/src/lib/signInPage.ts
new file mode 100644
index 0000000..a82cbdc
--- /dev/null
+++ b/frontend/src/lib/signInPage.ts
@@ -0,0 +1,9 @@
+// what the sign-in page does: wait while the stored session loads, open the dashboard for a session that was
+// already there, and otherwise show the form; a session made by a sign-in on the page is left to that sign-in,
+// so a password reset saves the new password before it leaves
+export type SignInPageOutcome = 'wait' | 'dashboard' | 'form';
+
+export function signInPageOutcome(hasSession: boolean | undefined, signingIn: boolean): SignInPageOutcome {
+ if (hasSession === undefined) return 'wait';
+ return hasSession && !signingIn ? 'dashboard' : 'form';
+}
diff --git a/frontend/src/pages/Login.tsx b/frontend/src/pages/Login.tsx
index 6358ddc..6752051 100644
--- a/frontend/src/pages/Login.tsx
+++ b/frontend/src/pages/Login.tsx
@@ -1,6 +1,6 @@
import { Suspense, lazy, useState } from 'react';
import type { FormEvent } from 'react';
-import { useNavigate, useSearchParams } from 'react-router-dom';
+import { Navigate, useNavigate, useSearchParams } from 'react-router-dom';
import { toast } from 'sonner';
import { ArrowLeft, MailWarning } from 'lucide-react';
import { useAuth } from '../context/AuthContext';
@@ -11,6 +11,7 @@ import { GOOGLE_CLIENT_ID } from '@/lib/googleIdentity';
import { emailErrorNotice, isExistingAccount, passwordErrorMessage } from '@/lib/authErrors';
import type { AuthErrorLike } from '@/lib/authErrors';
import { PASSWORD_MIN_LENGTH, newPasswordProblem } from '@/lib/passwords';
+import { signInPageOutcome } from '@/lib/signInPage';
import { FieldRow } from '@/components/DetailPanel';
import { useCountdown } from './useCountdown';
import LoginEmailStart from './LoginEmailStart';
@@ -64,7 +65,7 @@ const QUIET_LINK =
// then an email that receives a sign-in code, with the password behind a quiet link
export default function Login() {
const {
- signIn, signUp, verifySignUpCode, resendSignUpCode, sendSignInCode, verifySignInCode, sendPasswordReset, verifyPasswordResetCode,
+ session, signIn, signUp, verifySignUpCode, resendSignUpCode, sendSignInCode, verifySignInCode, sendPasswordReset, verifyPasswordResetCode,
updatePassword, signInWithGoogle,
} = useAuth();
const navigate = useNavigate();
@@ -79,6 +80,7 @@ export default function Login() {
const [resending, setResending] = useState(false);
const [resendIn, startResendWait] = useCountdown();
const [emailLimitMessage, setEmailLimitMessage] = useState<string | null>(null);
+ const [signingIn, setSigningIn] = useState(false);
const copy = LOGIN_COPY[view];
const title = view === 'start' && signingUp ? SIGN_UP_TITLE : copy.title;
const back = BACK_TO[view];
@@ -191,6 +193,7 @@ export default function Login() {
async function handleSubmit(e: FormEvent<HTMLFormElement>) {
e.preventDefault();
+ setSigningIn(true);
setLoading(true);
try {
await SUBMIT[view]();
@@ -222,6 +225,7 @@ export default function Login() {
}
async function handleGoogleCredential(idToken: string, nonce: string) {
+ setSigningIn(true);
setLoading(true);
try {
const { error } = await signInWithGoogle(idToken, nonce);
@@ -234,6 +238,11 @@ export default function Login() {
}
}
+ // the public site's Sign in and Get started buttons land here, so a stored session opens the dashboard
+ const outcome = signInPageOutcome(session === undefined ? undefined : !!session, signingIn);
+ if (outcome === 'wait') return null;
+ if (outcome === 'dashboard') return <Navigate to="/" replace />;
+
return (
<div className="relative min-h-svh bg-surface">
<Suspense fallback={null}>
diff --git a/frontend/tests/lib/signInPage.test.ts b/frontend/tests/lib/signInPage.test.ts
new file mode 100644
index 0000000..31153c4
--- /dev/null
+++ b/frontend/tests/lib/signInPage.test.ts
@@ -0,0 +1,20 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { signInPageOutcome } from '../../src/lib/signInPage.ts';
+
+test('the page waits until the stored session has loaded', () => {
+ assert.equal(signInPageOutcome(undefined, false), 'wait');
+});
+
+test('someone already signed in goes straight to the dashboard', () => {
+ assert.equal(signInPageOutcome(true, false), 'dashboard');
+});
+
+test('someone signed out sees the form', () => {
+ assert.equal(signInPageOutcome(false, false), 'form');
+ assert.equal(signInPageOutcome(false, true), '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');
+});