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): mark the shared domain while a session exists

spacesdrivead9ef46f7f49
diff --git a/frontend/src/context/AuthContext.tsx b/frontend/src/context/AuthContext.tsx
index e28e692..5edca80 100644
--- a/frontend/src/context/AuthContext.tsx
+++ b/frontend/src/context/AuthContext.tsx
@@ -2,6 +2,7 @@ import { createContext, useContext, useEffect, useState } from 'react';
import type { ReactNode } from 'react';
import type { Session, User } from '@supabase/supabase-js';
import { supabase } from '../lib/supabase';
+import { signedInCookie } from '../lib/signedInCookie';
type Auth = typeof supabase.auth;
@@ -34,6 +35,13 @@ export function AuthProvider({ children }: { children: ReactNode }) {
return () => subscription.unsubscribe();
}, []);
+ // tells the public site whether to open the dashboard; written only on the project's own domain
+ useEffect(() => {
+ if (session === undefined) return;
+ const cookie = signedInCookie(window.location.hostname, !!session);
+ if (cookie) document.cookie = cookie;
+ }, [session]);
+
const signIn = (email: string, password: string) =>
supabase.auth.signInWithPassword({ email, password });
diff --git a/frontend/src/lib/signedInCookie.ts b/frontend/src/lib/signedInCookie.ts
new file mode 100644
index 0000000..798ece5
--- /dev/null
+++ b/frontend/src/lib/signedInCookie.ts
@@ -0,0 +1,13 @@
+// the public site is another origin and cannot read the app's session, so the app leaves a cookie on the shared
+// domain saying only that someone is signed in, never a token; site/index.html then opens the dashboard
+export const SIGNED_IN_COOKIE = 'twiligent_signed_in';
+const SHARED_DOMAIN = 'twiligent.ujjwal.fyi';
+// renewed on every load while signed in, and cleared as soon as the session ends
+const MAX_AGE_SECONDS = 30 * 24 * 60 * 60;
+
+// the text to assign to document.cookie, or null when the app runs anywhere else (self-hosted, local)
+export function signedInCookie(hostname: string, signedIn: boolean): string | null {
+ if (hostname !== SHARED_DOMAIN && !hostname.endsWith(`.${SHARED_DOMAIN}`)) return null;
+ const value = signedIn ? `1; Max-Age=${MAX_AGE_SECONDS}` : '; Max-Age=0';
+ return `${SIGNED_IN_COOKIE}=${value}; Domain=${SHARED_DOMAIN}; Path=/; SameSite=Lax; Secure`;
+}
diff --git a/frontend/tests/lib/signedInCookie.test.ts b/frontend/tests/lib/signedInCookie.test.ts
new file mode 100644
index 0000000..d02a422
--- /dev/null
+++ b/frontend/tests/lib/signedInCookie.test.ts
@@ -0,0 +1,23 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { signedInCookie } from '../../src/lib/signedInCookie.ts';
+
+test('the app on the project domain marks the shared domain as signed in, without any token', () => {
+ assert.equal(
+ signedInCookie('app.twiligent.ujjwal.fyi', true),
+ 'twiligent_signed_in=1; Max-Age=2592000; Domain=twiligent.ujjwal.fyi; Path=/; SameSite=Lax; Secure',
+ );
+});
+
+test('signing out clears the mark at once', () => {
+ assert.equal(
+ signedInCookie('app.twiligent.ujjwal.fyi', false),
+ 'twiligent_signed_in=; Max-Age=0; Domain=twiligent.ujjwal.fyi; Path=/; SameSite=Lax; Secure',
+ );
+});
+
+test('a self-hosted or local app writes nothing', () => {
+ for (const hostname of ['localhost', '127.0.0.1', 'analytics.example.com', 'twiligent.ujjwal.fyi.example.com', 'nottwiligent.ujjwal.fyi']) {
+ assert.equal(signedInCookie(hostname, true), null, hostname);
+ }
+});