fix: auto-detect all LAN IPs for dev auth and HMR

No more manual LAN_HOST or BETTER_AUTH_TRUSTED_ORIGINS for dev.
Uses os.networkInterfaces() to allow all machine IPs automatically.
This commit is contained in:
2026-03-24 03:35:02 +03:00
parent 4cae7b48ef
commit 0e53fba726
3 changed files with 22 additions and 21 deletions

View File

@@ -1,24 +1,29 @@
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { prisma } from "./prisma";
import os from "os";
function getTrustedOrigins(): string[] {
// Prod: явный список origins
if (process.env.BETTER_AUTH_TRUSTED_ORIGINS) {
return process.env.BETTER_AUTH_TRUSTED_ORIGINS.split(",").map((o) => o.trim());
}
// Без явной конфигурации — разрешаем localhost на типичных портах Next.js
const origins: string[] = [];
for (let port = 3000; port <= 3010; port++) {
origins.push(`http://localhost:${port}`);
origins.push(`http://127.0.0.1:${port}`);
// Dev: автоопределение всех IP машины — работает с любого адреса без настройки
const hosts = new Set(["localhost", "127.0.0.1"]);
for (const ifaces of Object.values(os.networkInterfaces())) {
for (const iface of ifaces ?? []) {
if (!iface.internal && iface.family === "IPv4") {
hosts.add(iface.address);
}
}
}
// LAN-доступ: если задан LAN_HOST (IP сервера в локальной сети)
const lanHost = process.env.LAN_HOST;
if (lanHost) {
const origins: string[] = [];
for (const host of hosts) {
for (let port = 3000; port <= 3010; port++) {
origins.push(`http://${lanHost}:${port}`);
origins.push(`http://${host}:${port}`);
}
}