scaffolds.ts86 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 | import { brivenEngineProxyTarget } from './index.js'; |
| 7 | |
| 8 | export const BRIVEN_ENGINE_SCAFFOLDS = { |
| 9 | engine: 'briven-engine' as const, |
| 10 | |
| 11 | nextAppRouterProxy: ` |
| 12 | // app/api/auth/[...path]/route.ts |
| 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, // optional default |
| 18 | }); |
| 19 | |
| 20 | export const GET = handler; |
| 21 | export const POST = handler; |
| 22 | export const PUT = handler; |
| 23 | export const DELETE = handler; |
| 24 | export const PATCH = handler; |
| 25 | `.trim(), |
| 26 | |
| 27 | nextClientInit: ` |
| 28 | // lib/auth.ts |
| 29 | import { createBrivenEngineClient } from '@briven/auth/engine'; |
| 30 | |
| 31 | export const auth = createBrivenEngineClient({ |
| 32 | projectId: process.env.NEXT_PUBLIC_BRIVEN_PROJECT_ID!, |
| 33 | apiBasePath: '/api/auth', // first-party proxy — cookies on your domain |
| 34 | }); |
| 35 | `.trim(), |
| 36 | |
| 37 | expressProxy: ` |
| 38 | // Express first-party proxy → briven-engine FDI |
| 39 | import express from 'express'; |
| 40 | |
| 41 | const TARGET = '${brivenEngineProxyTarget('https://api.briven.tech')}'; |
| 42 | const app = express(); |
| 43 | |
| 44 | app.use('/api/auth', async (req, res) => { |
| 45 | const dest = TARGET + req.url; |
| 46 | const headers = { ...req.headers, host: undefined, 'x-briven-engine': 'briven-engine' }; |
| 47 | const r = await fetch(dest, { |
| 48 | method: req.method, |
| 49 | headers: headers as HeadersInit, |
| 50 | body: ['GET', 'HEAD'].includes(req.method) ? undefined : req, |
| 51 | duplex: 'half', |
| 52 | } as RequestInit); |
| 53 | res.status(r.status); |
| 54 | r.headers.forEach((v, k) => res.setHeader(k, v)); |
| 55 | const buf = Buffer.from(await r.arrayBuffer()); |
| 56 | res.send(buf); |
| 57 | }); |
| 58 | `.trim(), |
| 59 | |
| 60 | vanillaSignIn: ` |
| 61 | import { createBrivenEngineClient } from '@briven/auth/engine'; |
| 62 | |
| 63 | const auth = createBrivenEngineClient({ |
| 64 | projectId: 'p_YOUR_PROJECT', |
| 65 | apiBasePath: '/api/auth', |
| 66 | }); |
| 67 | |
| 68 | await auth.signInEmailPassword({ |
| 69 | email: 'you@example.com', |
| 70 | password: '…', |
| 71 | }); |
| 72 | `.trim(), |
| 73 | |
| 74 | passwordlessSms: ` |
| 75 | // SMS OTP is included in briven-engine |
| 76 | const code = await auth.createPasswordlessCode({ |
| 77 | phoneNumber: '+15551234567', |
| 78 | }); |
| 79 | // show user the SMS, then: |
| 80 | // await auth.consumePasswordlessCode({ preAuthSessionId, deviceId, userInputCode }); |
| 81 | `.trim(), |
| 82 | } as const; |
| 83 | |
| 84 | export function listBrivenEngineScaffolds(): string[] { |
| 85 | return Object.keys(BRIVEN_ENGINE_SCAFFOLDS).filter((k) => k !== 'engine'); |
| 86 | } |