videogen/apps/web/lib/scribase-auth.ts
caelum0x d56639a594
Some checks are pending
build-images / api (push) Waiting to run
build-images / worker-gpu (push) Waiting to run
build-images / web (push) Waiting to run
codeql / Analyze (javascript-typescript) (push) Waiting to run
codeql / Analyze (python) (push) Waiting to run
perf / API hot-path micro-smoke (budgeted) (push) Waiting to run
perf / Desktop engine crates (release build check) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Waiting to run
vision2026-ci / API phase modules (ruff + pytest) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Waiting to run
vision2026-ci / Python SDK resource tests (push) Waiting to run
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Waiting to run
vision2026-ci / Alembic upgrade + drift check (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Waiting to run
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

84 lines
2.8 KiB
TypeScript

/**
* Scribase Auth (email OTP), additive next to Clerk.
*
* Menivor's accounts and sessions stay on Clerk end to end — this module
* only uses Scribase's GoTrue instance (`ereboslab/menivor-auth`) as the
* email-code delivery + verification step. Once a code verifies, the caller
* (`app/api/auth/email-code/route.ts`) finds-or-creates the matching Clerk
* user by that now-*verified* email and hands the browser a Clerk sign-in
* ticket, so the rest of the app (which reads Clerk sessions everywhere)
* needs no changes and existing Clerk accounts keep working unchanged.
*
* `SCRIBASE_AUTH_URL`/`SCRIBASE_AUTH_ANON_KEY` are Worker vars (see
* `wrangler.jsonc`); both are the project's public, anon-role values (same
* trust level as a Supabase "anon key"), not secrets.
*/
const DEFAULT_URL = "https://ereboslab-menivor-auth-production-ce5ccd4e.scribase.com";
function baseUrl(): string {
return (process.env.SCRIBASE_AUTH_URL ?? DEFAULT_URL).replace(/\/+$/, "");
}
function anonKey(): string | null {
return process.env.SCRIBASE_AUTH_ANON_KEY ?? null;
}
/** True when this deployment has a Scribase Auth project configured. */
export function scribaseAuthConfigured(): boolean {
return Boolean(anonKey());
}
interface GoTrueError {
readonly error?: string;
readonly error_description?: string;
readonly msg?: string;
readonly code?: string;
}
async function gotrue<T>(path: string, body: Record<string, unknown>): Promise<T> {
const key = anonKey();
if (!key) {
throw new Error("scribase_auth_not_configured");
}
const response = await fetch(`${baseUrl()}${path}`, {
method: "POST",
headers: {
"content-type": "application/json",
apikey: key,
authorization: `Bearer ${key}`,
},
body: JSON.stringify(body),
});
const json = (await response.json().catch(() => ({}))) as GoTrueError & T;
if (!response.ok) {
const message = json.error_description ?? json.msg ?? json.error ?? "scribase_auth_error";
throw new Error(message);
}
return json;
}
/** Sends a 6-digit email code. Always succeeds for a valid address (GoTrue
* never reveals whether the account already existed). */
export async function sendEmailCode(email: string): Promise<void> {
await gotrue("/auth/v1/otp", { email, create_user: true });
}
interface VerifyResult {
readonly user?: { readonly email?: string; readonly id?: string };
}
/** Verifies the code and returns the now-confirmed email on success. */
export async function verifyEmailCode(email: string, code: string): Promise<string> {
const result = await gotrue<VerifyResult>("/auth/v1/verify", {
type: "email",
email,
token: code,
});
const verified = result.user?.email;
if (!verified) {
throw new Error("scribase_auth_verify_failed");
}
return verified;
}