From 9191c264af2628b2752e366e5c20ef125de20c7b Mon Sep 17 00:00:00 2001 From: reliaaaa Date: Sat, 5 Dec 2026 15:40:55 +0000 Subject: [PATCH] Enable isolated Better Auth Dash ownership verification --- .env.example | 2 + README.md | 1 + package-lock.json | 58 +++++++++++++++++++++++++ package.json | 1 + src/app/api/auth/dash/validate/route.ts | 35 +++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 src/app/api/auth/dash/validate/route.ts diff --git a/.env.example b/.env.example index ad13863..a1d84db 100644 --- a/.env.example +++ b/.env.example @@ -6,5 +6,7 @@ CRON_SECRET=replace-with-a-long-random-string APP_URL=http://localhost:3000 # Generate a different value per deployment with: openssl rand -base64 32 BETTER_AUTH_SECRET= +# Optional server-side key for Dash ownership verification at /api/auth. +BETTER_AUTH_API_KEY= GOOGLE_CLIENT_ID= GOOGLE_CLIENT_SECRET= diff --git a/README.md b/README.md index f009540..d2096f2 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,7 @@ Buka [localhost:3000](http://localhost:3000). Aplikasi berjalan dengan penyimpan | `CRON_SECRET` | Mengamankan cron transaksi berulang (harian, 08.00 WIB) | | `APP_URL` | URL publik kanonis untuk Better Auth dan Google OAuth (wajib untuk mode akun, HTTPS di Vercel) | | `BETTER_AUTH_SECRET` | Kunci privat Better Auth, minimal 32 karakter (wajib untuk mode akun). Buat dengan `openssl rand -base64 32`. | +| `BETTER_AUTH_API_KEY` | Kunci server opsional untuk verifikasi kepemilikan Better Auth Dash. Tambahkan ke environment Production di Vercel, deploy ulang, lalu verifikasi menggunakan `APP_URL/api/auth`. | | `GOOGLE_CLIENT_ID`, `GOOGLE_CLIENT_SECRET` | Login Google (opsional) | | `GOLDPRICE_API_KEY` | Secret server untuk mengambil harga spot emas dari Goldprice (opsional; dibutuhkan untuk harga live) | diff --git a/package-lock.json b/package-lock.json index 980d358..efa6453 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "MIT", "dependencies": { + "@better-auth/infra": "0.4.34", "bcryptjs": "^3.0.3", "better-auth": "1.7.29", "jszip": "^3.10.2", @@ -73,6 +74,48 @@ } } }, + "node_modules/@better-auth/infra": { + "version": "0.4.34", + "resolved": "https://registry.npmjs.org/@better-auth/infra/-/infra-0.4.34.tgz", + "integrity": "sha512-+LYlbkD55PZMaDiIIh/dsoGE3Nq+9E6YOxh4W+FVUzYrwX2fy4Je7tdm6PypbJNNlMxdN7+q+lKXiybypi4TUw==", + "license": "MIT", + "dependencies": { + "@better-auth/utils": "0.5.1", + "@better-fetch/fetch": "1.3.3", + "@noble/hashes": "^2.2.0", + "@xmldom/xmldom": "^0.9.10", + "better-call": "1.4.6", + "jose": "^6.2.12", + "libphonenumber-js": "^1.13.14" + }, + "peerDependencies": { + "@better-auth/core": ">=1.4.0", + "@react-native-async-storage/async-storage": ">=1.21.0", + "better-auth": ">=1.4.0", + "expo-constants": ">=16.0.0", + "expo-crypto": ">=13.0.0", + "expo-device": ">=6.0.0", + "react-native": ">=0.74.0", + "zod": ">=4.1.12" + }, + "peerDependenciesMeta": { + "@react-native-async-storage/async-storage": { + "optional": true + }, + "expo-constants": { + "optional": true + }, + "expo-crypto": { + "optional": true + }, + "expo-device": { + "optional": true + }, + "react-native": { + "optional": true + } + } + }, "node_modules/@better-auth/kysely-adapter": { "version": "1.7.29", "resolved": "https://registry.npmjs.org/@better-auth/kysely-adapter/-/kysely-adapter-1.7.29.tgz", @@ -1460,6 +1503,15 @@ "integrity": "sha512-zFDAD+tlpf2r4asuHEj0XH6pY6i0g5NeAHPn+15wk3BV6JA69eERFXC1gyGThDkVa1zCyKr5jox1+2LbV/AMLg==", "license": "MIT" }, + "node_modules/@xmldom/xmldom": { + "version": "0.9.12", + "resolved": "https://registry.npmjs.org/@xmldom/xmldom/-/xmldom-0.9.12.tgz", + "integrity": "sha512-5AXjrcMClTryPe9LgZrygpB1lj7s0S9E0+W+AHaVKAVyHanafK86iPSvG5xHVSp/jC+VH1UXu0TAEmY279xH7A==", + "license": "MIT", + "engines": { + "node": ">=14.6" + } + }, "node_modules/bcryptjs": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-3.0.3.tgz", @@ -1954,6 +2006,12 @@ } } }, + "node_modules/libphonenumber-js": { + "version": "1.13.18", + "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.13.18.tgz", + "integrity": "sha512-2Llz3WJHYERXg0S23G9oSK2FX2eVDwjJlNiCaujI7CPedP44tFfChBv6f63hzuHQJnkIXoMOxQZ0M+XmEKjc3Q==", + "license": "MIT" + }, "node_modules/lie": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/lie/-/lie-3.3.0.tgz", diff --git a/package.json b/package.json index 3a1d37e..a3d5433 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "license": "MIT", "type": "commonjs", "dependencies": { + "@better-auth/infra": "0.4.34", "bcryptjs": "^3.0.3", "better-auth": "1.7.29", "jszip": "^3.10.2", diff --git a/src/app/api/auth/dash/validate/route.ts b/src/app/api/auth/dash/validate/route.ts new file mode 100644 index 0000000..b64b15f --- /dev/null +++ b/src/app/api/auth/dash/validate/route.ts @@ -0,0 +1,35 @@ +import { dash } from '@better-auth/infra'; +import { betterAuth } from 'better-auth'; +import { appOrigin } from '@/server/auth-providers'; + +export const runtime = 'nodejs'; +export const dynamic = 'force-dynamic'; + +// Keep Dash verification separate from account routes and their database. +function createDashAuth(baseURL: string, secret: string, apiKey: string) { + return betterAuth({ + baseURL, + basePath: '/api/auth', + secret, + telemetry: { enabled: false }, + plugins: [dash({ apiKey })], + }); +} + +let auth: ReturnType | undefined; + +export async function GET(request: Request) { + const apiKey = process.env.BETTER_AUTH_API_KEY?.trim(); + const secret = process.env.BETTER_AUTH_SECRET; + const baseURL = appOrigin(process.env.APP_URL); + + if (!apiKey || !baseURL || !secret || secret.length < 32) { + return new Response(null, { status: 404, headers: { 'Cache-Control': 'no-store' } }); + } + + auth ??= createDashAuth(baseURL, secret, apiKey); + const response = await auth.handler(request); + const headers = new Headers(response.headers); + headers.set('Cache-Control', 'no-store'); + return new Response(response.body, { status: response.status, headers }); +}