mirror of
https://github.com/bitbaum/substrata.git
synced 2026-12-04 04:47:30 +01:00
Substrata created its account runtime with `mail: { configured: false }`, so
the sign-in link, "Forgot your password?" and address confirmation could
never appear, and a forgotten password had no way back.
- lib/auth.ts sends account mail through AccountKit's `createAuthMailer`
(mail-kit, the fleet's RESEND_API_KEY). Without a key the options stay
hidden, as before; confirming an address is never a wall.
- The sign-in, sign-up and account pages pass `mailConfigured()` to
`signInMethods`, and a reset link that lands on /sign-in is no longer
redirected away for a signed-in reader.
- The account journey first asserts every way in the env switches on:
password and passkey always, the link only with mail, "Continue with
OrangeCat" and the shared sign-in button with their clients. CI sets
synthetic, unreachable OIDC clients so both buttons are checked.
- @bitbaum/mail-kit is a direct dependency (AccountKit's optional peer).
Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9
Co-authored-by: Claude <noreply@anthropic.com>
118 lines
4.7 KiB
TypeScript
118 lines
4.7 KiB
TypeScript
import { headers } from 'next/headers';
|
|
import { redirect } from 'next/navigation';
|
|
import { drizzle } from 'drizzle-orm/node-postgres';
|
|
import { authSchema } from '@bitbaum/accountkit/auth/schema';
|
|
import { isDeliverable } from '@bitbaum/accountkit/auth';
|
|
import { createAuthRuntime } from '@bitbaum/accountkit/auth/next';
|
|
import { createAuthMailer } from '@bitbaum/accountkit/auth/mail';
|
|
import { accountsDatabase } from './db';
|
|
import { within } from './within';
|
|
|
|
/** How long a page waits for a session before treating the reader as signed out. */
|
|
export const SESSION_READ_TIMEOUT_MS = 4000;
|
|
import { SITE } from './site';
|
|
import { signInHref } from './sign-in-href';
|
|
|
|
export { signInHref };
|
|
|
|
/**
|
|
* Accounts on Substrata — the fleet's one account system
|
|
* (@bitbaum/accountkit/auth). A reader signs up with an email and a password,
|
|
* a passkey, or "Continue with OrangeCat"; OrangeCat is one way in, never a
|
|
* requirement. The tables are scripts/db/017-accounts.sql.
|
|
*
|
|
* Desks, follows, saved AI keys and reviewer ids were keyed on the OrangeCat
|
|
* actor id before Substrata had accounts. `legacyIdentityProvider` keeps that
|
|
* id for everyone whose first way in was OrangeCat, so all of it stays theirs
|
|
* with no data migration (SUBSTRATA_REVIEWER_ACTOR_IDS included). Everyone
|
|
* else is their account id.
|
|
*/
|
|
const authMailer = createAuthMailer({ site: { url: SITE.url, title: SITE.name } });
|
|
|
|
/** Whether account mail can be sent; the screens offer the link and recovery only then. */
|
|
export const mailConfigured = () => authMailer.configured;
|
|
|
|
export const { GET, POST, getSession, authStatus, getAuth } = createAuthRuntime({
|
|
site: {
|
|
url: SITE.url,
|
|
title: SITE.name,
|
|
slug: 'substrata',
|
|
// AUTH_URL is where this server is reached (the public origin on the box,
|
|
// http://localhost:3000 in the browser-smoke journey). Trusting it keeps
|
|
// better-auth's origin check on in CI instead of switching it off.
|
|
trustedOrigins: process.env.AUTH_URL?.trim() ? [process.env.AUTH_URL.trim()] : [],
|
|
},
|
|
// Account mail (sign-in link, password recovery, address confirmation)
|
|
// through the fleet transport. Without RESEND_API_KEY those options stay
|
|
// hidden; a password account signs in at once either way.
|
|
mail: authMailer,
|
|
isDatabaseConfigured: () => Boolean(process.env.DATABASE_URL),
|
|
getDatabase: () => drizzle(accountsDatabase(), { schema: authSchema }),
|
|
legacyIdentityProvider: 'orangecat',
|
|
});
|
|
|
|
/** False without a database or a session secret: sign-in is then not offered. */
|
|
export const authEnabled = authStatus().ready;
|
|
|
|
export interface SubstrataSession {
|
|
/** The id every desk, follow and saved key uses (see above). */
|
|
actorId: string;
|
|
user: { name: string | null; email: string | null; image: string | null };
|
|
}
|
|
|
|
export function isReviewer(actorId: string | undefined) {
|
|
return !!actorId && (process.env.SUBSTRATA_REVIEWER_ACTOR_IDS ?? '').split(',').includes(actorId);
|
|
}
|
|
|
|
/** The signed-in reader, or null. */
|
|
export async function auth(): Promise<SubstrataSession | null> {
|
|
const session = await getSession();
|
|
if (!session) return null;
|
|
const { user } = session;
|
|
return {
|
|
actorId: user.actorId,
|
|
user: {
|
|
name: user.name || null,
|
|
email: isDeliverable(user.email) ? user.email : null,
|
|
image: user.image ?? null,
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* The session, or null when signed out. A failure to read one still answers
|
|
* "signed out" — the research is public, so a broken session must not take a
|
|
* page down — but it is LOGGED, so a misconfigured secret or a missing table
|
|
* does not look exactly like nobody being signed in.
|
|
*/
|
|
export async function currentSession(): Promise<SubstrataSession | null> {
|
|
if (!authEnabled) return null;
|
|
try {
|
|
// A session read that does not answer is "signed out" after a few seconds,
|
|
// not a page that hangs. On 2026-10-30 every page 502'd for hours while
|
|
// session reads hung and /api/health still answered; a restart cleared it.
|
|
return await within(auth(), SESSION_READ_TIMEOUT_MS, 'session read');
|
|
} catch (error) {
|
|
console.error(
|
|
'session read failed',
|
|
error instanceof Error ? `${error.name}: ${error.message}` : 'unknown',
|
|
);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** Send the reader to the sign-in screen, which offers every way in. */
|
|
export async function signIn(options: { redirectTo: string }): Promise<never> {
|
|
redirect(signInHref(options.redirectTo));
|
|
}
|
|
|
|
/** End the session and go where the caller asked. */
|
|
export async function signOut(options: { redirectTo: string }): Promise<never> {
|
|
try {
|
|
await getAuth().api.signOut({ headers: await headers() });
|
|
} catch {
|
|
// Already signed out, or accounts unavailable: either way, go on.
|
|
}
|
|
redirect(options.redirectTo);
|
|
}
|