mirror of
https://github.com/bitbaum/heidi.git
synced 2026-12-04 14:57:06 +01:00
Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T Co-authored-by: Claude <noreply@anthropic.com>
102 lines
3.4 KiB
TypeScript
102 lines
3.4 KiB
TypeScript
import { headers } from "next/headers";
|
|
import { redirect } from "next/navigation";
|
|
import { createAuthRuntime } from "@bitbaum/accountkit/auth/next";
|
|
import { isDeliverable } from "@bitbaum/accountkit/auth";
|
|
import { dbConfigured, getDb } from "../db/index.ts";
|
|
import { authSite, heidiAuthMailer } from "./mail.ts";
|
|
import { signInPath } from "./paths.ts";
|
|
|
|
/**
|
|
* Accounts for Heidi — the same account system as every fleet product
|
|
* (@bitbaum/accountkit/auth). A learner registers here with an email and a
|
|
* password, a sign-in link or a passkey; "Continue with OrangeCat" is one
|
|
* button among them, never a requirement.
|
|
*
|
|
* Heidi's rows were keyed on the OrangeCat actor id before it had accounts.
|
|
* `legacyIdentityProvider` keeps it that way for everyone whose first way in
|
|
* was OrangeCat, so their groups, conversations and progress stay theirs with
|
|
* no data migration. Everyone else is their account id.
|
|
*/
|
|
export { authSite };
|
|
export const authMail = heidiAuthMailer();
|
|
export const mailConfigured = () => authMail.configured;
|
|
|
|
export const { GET, POST, getSession, requireUser, authStatus, getAuth } =
|
|
createAuthRuntime({
|
|
site: authSite,
|
|
mail: authMail,
|
|
isDatabaseConfigured: () => dbConfigured(),
|
|
getDatabase: () => getDb(),
|
|
legacyIdentityProvider: "orangecat",
|
|
});
|
|
|
|
/** Kept for the route file: `export const { GET, POST } = handlers`. */
|
|
export const handlers = { GET, POST };
|
|
|
|
/**
|
|
* False when this deployment has no database or no session secret. The UI
|
|
* then hides the sign-in control rather than offering one that cannot work.
|
|
*/
|
|
export const authEnabled = authStatus().ready;
|
|
|
|
/**
|
|
* The signed-in person as the rest of Heidi reads them. `actorId` is the id
|
|
* every row uses. An address that cannot receive mail (one invented for a
|
|
* person who came in through OrangeCat) is never passed on as an email.
|
|
*/
|
|
type Signed = {
|
|
actorId: string;
|
|
user: { name: string | null; email: string | null; image: string | null };
|
|
};
|
|
|
|
/**
|
|
* ONE SESSION READ PER REQUEST. A route asks who is calling more than once
|
|
* (the rate limit, then the handler; the pass, then the handler), and each
|
|
* ask was a query. Next hands every `headers()` call of one request the same
|
|
* object, so the answer is kept against that object: no clock, no token, and
|
|
* nothing that outlives the request, so a sign-out is seen by the very next
|
|
* one.
|
|
*/
|
|
const SESSIONS = new WeakMap<object, Promise<Signed | null>>();
|
|
|
|
export async function auth(): Promise<Signed | null> {
|
|
const request = await headers();
|
|
let found = SESSIONS.get(request);
|
|
if (!found) {
|
|
found = readSession();
|
|
SESSIONS.set(request, found);
|
|
}
|
|
return found;
|
|
}
|
|
|
|
async function readSession(): Promise<Signed | 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,
|
|
},
|
|
};
|
|
}
|
|
|
|
export async function signIn(
|
|
_provider: string,
|
|
options: { redirectTo: string },
|
|
): Promise<never> {
|
|
redirect(signInPath(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);
|
|
}
|