auth-core-loginmethods.ts72 lines · main
1/**
2 * briven-engine login methods summary for apps / dashboard.
3 *
4 * GET /v1/auth-core/loginmethods
5 * GET /v1/auth-core/projects/:projectId/loginmethods
6 */
7
8import { Hono } from 'hono';
9
10import {
11 BRIVEN_ENGINE_ID,
12 getAuthCoreRecipeMeta,
13 isAuthCoreInitialized,
14} from '../services/auth-core/engine.js';
15import { getBrivenEngineProjectConfig } from '../services/auth-core/project-config.js';
16import type { AppEnv } from '../types/app-env.js';
17
18export const authCoreLoginMethodsRouter = new Hono<AppEnv>();
19
20function globalMethods() {
21 const meta = getAuthCoreRecipeMeta();
22 const names = new Set(meta.names);
23 return {
24 engine: BRIVEN_ENGINE_ID,
25 sdkInitialized: isAuthCoreInitialized(),
26 emailPassword: names.has('emailpassword'),
27 passwordlessEmail: names.has('passwordless'),
28 passwordlessSms: names.has('passwordless'),
29 thirdParty: names.has('thirdparty'),
30 webauthn: names.has('webauthn'),
31 mfa: names.has('multifactorauth'),
32 totp: names.has('totp'),
33 };
34}
35
36authCoreLoginMethodsRouter.get('/v1/auth-core/loginmethods', (c) => {
37 return c.json(globalMethods());
38});
39
40authCoreLoginMethodsRouter.get(
41 '/v1/auth-core/projects/:projectId/loginmethods',
42 async (c) => {
43 const projectId = c.req.param('projectId');
44 const base = globalMethods();
45 try {
46 const config = await getBrivenEngineProjectConfig(projectId);
47 const configuredProviders = config.providers
48 .filter((p) => p.configured)
49 .map((p) => p.thirdPartyId);
50 return c.json({
51 ...base,
52 projectId,
53 tenantId: config.tenantId,
54 thirdPartyProvidersConfigured: configuredProviders,
55 smsProviderConfigured: config.delivery.sms.configured,
56 emailProviderConfigured: config.delivery.email.configured,
57 /** Operator method flags (project-level). */
58 methods: config.methods,
59 /** True only when SMS method is on AND Twilio secrets are saved. */
60 passwordlessSmsReady:
61 config.methods.passwordlessSms && config.delivery.sms.configured,
62 passwordlessSmsEnabled: config.methods.passwordlessSms,
63 });
64 } catch (err) {
65 return c.json({
66 ...base,
67 projectId,
68 error: err instanceof Error ? err.message : String(err),
69 });
70 }
71 },
72);