MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / buildBetterAuth

Function buildBetterAuth

apps/host-selfhost/src/auth/better-auth.ts:323–355  ·  view source on GitHub ↗
(client: Client)

Source from the content-addressed store, hash-verified

321 * idempotency reads against the auth tables Better Auth just migrated.
322 */
323export const buildBetterAuth = async (client: Client): Promise<BetterAuthHandle> => {
324 const config = loadConfig();
325
326 // The org id is resolved by the seed below, AFTER this instance is built; the
327 // session-pin hook and the gate read it through these late-bound accessors
328 // (no session is created during the seed, so the empty initial id is never
329 // observed). `getAuth` resolves to this very instance, so the gate's `after`
330 // hook can call `auth.api.addMember` once a code is redeemed.
331 let auth: Auth | null = null;
332 const orgRef = { id: "" };
333 const gate: SignupGate = {
334 client,
335 get organizationId() {
336 return orgRef.id;
337 },
338 getAuth: () => auth,
339 };
340
341 auth = createAuthInstance(client, () => orgRef.id, gate);
342 // `runMigrations()` flows through the LibsqlDialect and is idempotent.
343 await (await auth.$context).runMigrations();
344 await ensureInviteCodeTable(client);
345 const { organizationId, organizationName } = await seedOrgAndAdmin(auth, client, config);
346 orgRef.id = organizationId;
347
348 return {
349 auth,
350 organizationId,
351 organizationName,
352 organizationSlug: config.orgSlug,
353 handler: auth.handler,
354 };
355};

Callers 1

resolveAuthProvidersFunction · 0.90

Calls 4

loadConfigFunction · 0.90
ensureInviteCodeTableFunction · 0.90
seedOrgAndAdminFunction · 0.90
createAuthInstanceFunction · 0.85

Tested by

no test coverage detected