fix: auth not working when accessed from non-localhost

- Remove hardcoded baseURL from auth-client.ts — better-auth now uses
  current window.location.origin, works from any IP/domain
- Add trustedOrigins config to auth.ts — allows requests from LAN IPs
- Add BETTER_AUTH_TRUSTED_ORIGINS env var for configuring allowed origins
- Improve error logging in login/register forms (was silent catch)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-24 02:54:56 +03:00
parent bd195bd735
commit cbdd2fb2e4
4 changed files with 11 additions and 7 deletions

View File

@@ -25,8 +25,9 @@ export default function LoginPage() {
} else {
router.push("/dashboard");
}
} catch {
setError("Произошла ошибка. Попробуйте ещё раз.");
} catch (err) {
console.error("Login error:", err);
setError(err instanceof Error ? err.message : "Произошла ошибка. Попробуйте ещё раз.");
} finally {
setLoading(false);
}

View File

@@ -26,8 +26,9 @@ export default function RegisterPage() {
} else {
router.push("/dashboard");
}
} catch {
setError("Произошла ошибка. Попробуйте ещё раз.");
} catch (err) {
console.error("Register error:", err);
setError(err instanceof Error ? err.message : "Произошла ошибка. Попробуйте ещё раз.");
} finally {
setLoading(false);
}

View File

@@ -1,7 +1,5 @@
import { createAuthClient } from "better-auth/react";
export const authClient = createAuthClient({
baseURL: process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000",
});
export const authClient = createAuthClient();
export const { signIn, signUp, signOut, useSession } = authClient;

View File

@@ -3,6 +3,10 @@ import { prismaAdapter } from "better-auth/adapters/prisma";
import { prisma } from "./prisma";
export const auth = betterAuth({
baseURL: process.env.BETTER_AUTH_URL,
trustedOrigins: process.env.BETTER_AUTH_TRUSTED_ORIGINS
? process.env.BETTER_AUTH_TRUSTED_ORIGINS.split(",")
: ["http://localhost:3000"],
database: prismaAdapter(prisma, {
provider: "postgresql",
}),