Log possibilities to enable Fail2ban #1814

Closed
opened 2026-03-13 09:05:17 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @Schlumpf9 on GitHub (Aug 31, 2025).

Is this suited for github?

  • Yes, this is suited for github

No response

Describe the solution you'd like

Hi together,

i am using fail2ban on my server and i am looking for a way to prevent brute-force attacks against the login of my application. I know better-auth already has some mechanisms to block ip addresses, nevertheless i would appreciate it to block these ip addresses generally over all applications. Is there a way to get more logs as the default ones to be able to receive more information about the client that tried to login?

2025-08-31T07:01:54.148Z ERROR [Better Auth]: User not found { email: 'test@test.com' }
POST /api/auth/sign-in/email 401 in 1180ms

Another question about security regarding trustedOrigins. I am wondering if there is a standard way to set them using environment variables? As the application development is normally independent of the deployd domain my question is if there are already build in environment variables that can be set? If not i assume this configuration part is serverside and it should be no problem to use a custom ENV here?

{
  trustedOrigins: [
    process.env.APP_TRUSTED_ORIGIN_DOMAIN,
    "https://app.example.com",
    "http://localhost:3000"
  ]
}

Thank you very much for this great library, i really appreciate it!

Describe alternatives you've considered

None

Additional context

No response

Originally created by @Schlumpf9 on GitHub (Aug 31, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. _No response_ ### Describe the solution you'd like Hi together, i am using fail2ban on my server and i am looking for a way to prevent brute-force attacks against the login of my application. I know better-auth already has some mechanisms to block ip addresses, nevertheless i would appreciate it to block these ip addresses generally over all applications. Is there a way to get more logs as the default ones to be able to receive more information about the client that tried to login? ``` 2025-08-31T07:01:54.148Z ERROR [Better Auth]: User not found { email: 'test@test.com' } POST /api/auth/sign-in/email 401 in 1180ms ``` Another question about security regarding trustedOrigins. I am wondering if there is a standard way to set them using environment variables? As the application development is normally independent of the deployd domain my question is if there are already build in environment variables that can be set? If not i assume this configuration part is serverside and it should be no problem to use a custom ENV here? ``` { trustedOrigins: [ process.env.APP_TRUSTED_ORIGIN_DOMAIN, "https://app.example.com", "http://localhost:3000" ] } ``` Thank you very much for this great library, i really appreciate it! ### Describe alternatives you've considered None ### Additional context _No response_
GiteaMirror added the security label 2026-03-13 09:05:17 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 31, 2025):

Hi @Schlumpf9! I'm Dosu and I’m helping the better-auth team.

Better Auth's default logs for failed login attempts (like "User not found") do not include client IP or user agent information, which limits their usefulness for Fail2ban. However, you can enhance logging by using a custom logger function in your Better Auth configuration. This lets you extract and log additional metadata from the request, such as IP and user agent, which Fail2ban can then parse and act on. The logger utility supports this via the log option—see the logger implementation for details.

To extract the client IP, make sure your proxy forwards headers like x-real-ip or x-forwarded-for, and configure Better Auth's ipAddressHeaders accordingly. You can then access these headers in your custom logger and include them in your log output.

For trustedOrigins, Better Auth supports both static arrays and dynamic functions, so you can use environment variables directly in your config, for example:

trustedOrigins: [
  process.env.APP_TRUSTED_ORIGIN_DOMAIN,
  "https://app.example.com",
  "http://localhost:3000"
]

Or dynamically:

trustedOrigins: async (request) => [
  process.env.APP_TRUSTED_ORIGIN_DOMAIN,
  "https://app.example.com"
]

Wildcard patterns are supported, but use "*" with caution for security reasons. More details are in this thread.

For full Fail2ban support, consider customizing your logger to output logs in a format Fail2ban can parse, including IP and user agent info. If you need more granular control, you may want to inject middleware or extend Better Auth to log richer metadata on failed authentication attempts.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Aug 31, 2025): <!-- Greeting --> Hi @Schlumpf9! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> Better Auth's default logs for failed login attempts (like "User not found") do not include client IP or user agent information, which limits their usefulness for Fail2ban. However, you can enhance logging by using a custom logger function in your Better Auth configuration. This lets you extract and log additional metadata from the request, such as IP and user agent, which Fail2ban can then parse and act on. The logger utility supports this via the `log` option—see the [logger implementation](https://github.com/better-auth/better-auth/blob/cf7f2f118147b6a79a99fcf48898408f038390f5/packages/better-auth/src/utils/logger.ts) for details. To extract the client IP, make sure your proxy forwards headers like `x-real-ip` or `x-forwarded-for`, and configure Better Auth's `ipAddressHeaders` accordingly. You can then access these headers in your custom logger and include them in your log output. For trustedOrigins, Better Auth supports both static arrays and dynamic functions, so you can use environment variables directly in your config, for example: ```js trustedOrigins: [ process.env.APP_TRUSTED_ORIGIN_DOMAIN, "https://app.example.com", "http://localhost:3000" ] ``` Or dynamically: ```js trustedOrigins: async (request) => [ process.env.APP_TRUSTED_ORIGIN_DOMAIN, "https://app.example.com" ] ``` Wildcard patterns are supported, but use `"*"` with caution for security reasons. More details are in [this thread](https://github.com/better-auth/better-auth/issues/3853). For full Fail2ban support, consider customizing your logger to output logs in a format Fail2ban can parse, including IP and user agent info. If you need more granular control, you may want to inject middleware or extend Better Auth to log richer metadata on failed authentication attempts. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/a07341e7-7c70-4152-93de-cf1657f1911e?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/a07341e7-7c70-4152-93de-cf1657f1911e?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/a07341e7-7c70-4152-93de-cf1657f1911e?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/a07341e7-7c70-4152-93de-cf1657f1911e?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/a07341e7-7c70-4152-93de-cf1657f1911e?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/a07341e7-7c70-4152-93de-cf1657f1911e?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/a07341e7-7c70-4152-93de-cf1657f1911e?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/4329)
Author
Owner

@Kinfe123 commented on GitHub (Sep 2, 2025):

you can make sure to resolve the trusted origin dynamically here - https://www.better-auth.com/docs/reference/options#trustedorigins and for the logger. we are making it generalized for BA endpoints to work well with logger options in your own auth config.

@Kinfe123 commented on GitHub (Sep 2, 2025): you can make sure to resolve the trusted origin dynamically here - https://www.better-auth.com/docs/reference/options#trustedorigins and for the logger. we are making it generalized for BA endpoints to work well with logger options in your own auth config.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1814