scaffolds.ts122 lines · main
| 1 | /** |
| 2 | * Framework scaffold snippets for briven-engine (Phase 8 pack). |
| 3 | * Copy-paste helpers — not runtime imports required by the client. |
| 4 | */ |
| 5 | |
| 6 | export const BRIVEN_ENGINE_SCAFFOLDS = { |
| 7 | engine: 'briven-engine' as const, |
| 8 | |
| 9 | nextAppRouterProxy: ` |
| 10 | // app/api/auth/[...path]/route.ts — briven-engine first-party proxy |
| 11 | // Gold path: browser → /api/auth/* → api.briven.tech/v1/auth-core/fdi/* |
| 12 | // Server injects project id + pk_briven_auth_ (keep secret-ish key off pure browser calls). |
| 13 | import { brivenEngineNextHandler } from '@briven/auth/engine'; |
| 14 | |
| 15 | const handler = brivenEngineNextHandler({ |
| 16 | apiOrigin: process.env.BRIVEN_API_ORIGIN ?? 'https://api.briven.tech', |
| 17 | projectId: process.env.BRIVEN_PROJECT_ID ?? process.env.NEXT_PUBLIC_BRIVEN_PROJECT_ID, |
| 18 | publicKey: process.env.BRIVEN_AUTH_PUBLIC_KEY, // pk_briven_auth_… |
| 19 | }); |
| 20 | |
| 21 | export const GET = handler; |
| 22 | export const POST = handler; |
| 23 | export const PUT = handler; |
| 24 | export const DELETE = handler; |
| 25 | export const PATCH = handler; |
| 26 | `.trim(), |
| 27 | |
| 28 | nextClientInit: ` |
| 29 | // lib/auth.ts — browser uses same-origin proxy (key injected server-side). |
| 30 | import { createBrivenEngineClient } from '@briven/auth/engine'; |
| 31 | |
| 32 | export const auth = createBrivenEngineClient({ |
| 33 | projectId: process.env.NEXT_PUBLIC_BRIVEN_PROJECT_ID!, |
| 34 | apiBasePath: '/api/auth', // first-party proxy — cookies on your domain |
| 35 | // publicKey only needed for direct API calls without the proxy: |
| 36 | // publicKey: process.env.NEXT_PUBLIC_BRIVEN_AUTH_PUBLIC_KEY, |
| 37 | }); |
| 38 | `.trim(), |
| 39 | |
| 40 | expressProxy: ` |
| 41 | // Express first-party proxy → briven-engine FDI |
| 42 | import express from 'express'; |
| 43 | import { proxyBrivenEngineAuth } from '@briven/auth/engine'; |
| 44 | |
| 45 | const app = express(); |
| 46 | |
| 47 | app.use('/api/auth', async (req, res) => { |
| 48 | const url = \`\${req.protocol}://\${req.get('host')}\${req.originalUrl}\`; |
| 49 | const headers = new Headers(); |
| 50 | for (const [k, v] of Object.entries(req.headers)) { |
| 51 | if (typeof v === 'string') headers.set(k, v); |
| 52 | } |
| 53 | const r = await proxyBrivenEngineAuth( |
| 54 | new Request(url, { method: req.method, headers, body: ['GET','HEAD'].includes(req.method) ? undefined : req }), |
| 55 | { |
| 56 | apiOrigin: process.env.BRIVEN_API_ORIGIN ?? 'https://api.briven.tech', |
| 57 | projectId: process.env.BRIVEN_PROJECT_ID, |
| 58 | publicKey: process.env.BRIVEN_AUTH_PUBLIC_KEY, |
| 59 | }, |
| 60 | ); |
| 61 | res.status(r.status); |
| 62 | r.headers.forEach((v, k) => res.setHeader(k, v)); |
| 63 | res.send(Buffer.from(await r.arrayBuffer())); |
| 64 | }); |
| 65 | `.trim(), |
| 66 | |
| 67 | vanillaSignIn: ` |
| 68 | import { createBrivenEngineClient } from '@briven/auth/engine'; |
| 69 | |
| 70 | const auth = createBrivenEngineClient({ |
| 71 | projectId: 'p_YOUR_PROJECT', |
| 72 | apiBasePath: '/api/auth', |
| 73 | // If calling API directly (no proxy): publicKey: 'pk_briven_auth_…', |
| 74 | }); |
| 75 | |
| 76 | await auth.signInEmailPassword({ |
| 77 | email: 'you@example.com', |
| 78 | password: '…', |
| 79 | // turnstileToken: '…', // when platform captcha is on |
| 80 | }); |
| 81 | `.trim(), |
| 82 | |
| 83 | honoProxy: ` |
| 84 | // Hono first-party proxy → briven-engine FDI |
| 85 | import { Hono } from 'hono'; |
| 86 | import { proxyBrivenEngineAuth } from '@briven/auth/engine'; |
| 87 | |
| 88 | const app = new Hono(); |
| 89 | |
| 90 | app.all('/api/auth/*', async (c) => { |
| 91 | return proxyBrivenEngineAuth(c.req.raw, { |
| 92 | apiOrigin: process.env.BRIVEN_API_ORIGIN ?? 'https://api.briven.tech', |
| 93 | projectId: process.env.BRIVEN_PROJECT_ID, |
| 94 | publicKey: process.env.BRIVEN_AUTH_PUBLIC_KEY, |
| 95 | }); |
| 96 | }); |
| 97 | `.trim(), |
| 98 | |
| 99 | passwordlessSms: ` |
| 100 | // SMS OTP is included in briven-engine |
| 101 | const code = await auth.createPasswordlessCode({ |
| 102 | phoneNumber: '+15551234567', |
| 103 | }); |
| 104 | // show user the SMS, then: |
| 105 | // await auth.consumePasswordlessCode({ preAuthSessionId, deviceId, userInputCode }); |
| 106 | `.trim(), |
| 107 | |
| 108 | passkeySignIn: ` |
| 109 | // Passkey sign-in (browser) — first-party proxy required |
| 110 | const start = await auth.passkeySignInOptions({ |
| 111 | rpId: window.location.hostname, |
| 112 | expectedOrigin: window.location.origin, |
| 113 | }); |
| 114 | // if start.ok: navigator.credentials.get({ publicKey: start.data.options }) |
| 115 | // then auth.passkeySignInFinish({ challengeId, credential, rpId, expectedOrigin }) |
| 116 | // First-time users: sign in with magic link/OTP, then register a passkey. |
| 117 | `.trim(), |
| 118 | } as const; |
| 119 | |
| 120 | export function listBrivenEngineScaffolds(): string[] { |
| 121 | return Object.keys(BRIVEN_ENGINE_SCAFFOLDS).filter((k) => k !== 'engine'); |
| 122 | } |