-
Notifications
You must be signed in to change notification settings - Fork 4
feat(auth-service): log better-auth OTP verification failures #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aspiers
wants to merge
2
commits into
main
Choose a base branch
from
fix/log-otp-verification-failures
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| 'ePDS': patch | ||
| --- | ||
|
|
||
| Failed sign-in code entries now show up in the server logs, split by reason. | ||
|
|
||
| **Affects:** Operators | ||
|
|
||
| **Operators:** the auth service now logs its own 4xx client errors — most usefully the sign-in code failures that the browser posts straight to `/api/auth/sign-in/email-otp`, which previously reached no log at all. | ||
|
|
||
| - Each failure is logged at `warn` (visible at the default `info` level, no `LOG_LEVEL` change needed) under the `auth:better-auth` logger name, with the message `better-auth API error` and a `message` field carrying the reason verbatim: `OTP expired`, `Invalid OTP`, or `Too many attempts`. | ||
| - Counting `OTP expired` vs `Invalid OTP` over time separates users whose code genuinely lapsed (late email delivery) from users retyping a stale code, so a rise in `OTP expired` points at delivery delay rather than user error. | ||
| - Only 4xx errors are logged here; 3xx redirects are filtered out and 5xx errors are already logged by the framework, so this adds no duplicate 5xx noise. | ||
| - The email address is not included: the framework hands this logging hook the instance-wide context, not the per-request body, so the address is not reliably available at that point. |
105 changes: 105 additions & 0 deletions
105
packages/auth-service/src/__tests__/log-better-auth-api-error.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /** | ||
| * Tests for logBetterAuthApiError — the bridge that surfaces better-auth's | ||
| * 4xx client errors (notably "OTP expired" vs "Invalid OTP") in our pino logs. | ||
| * | ||
| * These are exactly the errors better-auth throws from its email-otp verify | ||
| * endpoint (better-auth 1.4.18 dist/plugins/email-otp/routes.mjs), which the | ||
| * browser posts to directly and which were previously logged nowhere. | ||
| */ | ||
| import { describe, it, expect, vi } from 'vitest' | ||
| import { APIError } from 'better-auth/api' | ||
| import { logBetterAuthApiError } from '../better-auth.js' | ||
|
|
||
| /** Minimal stand-in for the pino logger — only `warn` is exercised. */ | ||
| function makeLogger() { | ||
| return { warn: vi.fn() } as unknown as Parameters< | ||
| typeof logBetterAuthApiError | ||
| >[1] | ||
| } | ||
|
|
||
| describe('logBetterAuthApiError', () => { | ||
| it('logs an expired-OTP error at warn with the reason and the error object', () => { | ||
| const log = makeLogger() | ||
| const error = new APIError('BAD_REQUEST', { message: 'OTP expired' }) | ||
| logBetterAuthApiError(error, log) | ||
|
|
||
| expect(log.warn).toHaveBeenCalledOnce() | ||
| expect(log.warn).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| err: error, | ||
| status: 'BAD_REQUEST', | ||
| statusCode: 400, | ||
| message: 'OTP expired', | ||
| }), | ||
| 'better-auth API error', | ||
| ) | ||
| }) | ||
|
|
||
| it('logs an invalid-OTP error at warn, distinct from expiry', () => { | ||
| const log = makeLogger() | ||
| logBetterAuthApiError( | ||
| new APIError('BAD_REQUEST', { message: 'Invalid OTP' }), | ||
| log, | ||
| ) | ||
|
|
||
| expect(log.warn).toHaveBeenCalledWith( | ||
| expect.objectContaining({ statusCode: 400, message: 'Invalid OTP' }), | ||
| 'better-auth API error', | ||
| ) | ||
| }) | ||
|
|
||
| it('logs a 403 too-many-attempts error at warn', () => { | ||
| const log = makeLogger() | ||
| logBetterAuthApiError( | ||
| new APIError('FORBIDDEN', { message: 'Too many attempts' }), | ||
| log, | ||
| ) | ||
|
|
||
| expect(log.warn).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| status: 'FORBIDDEN', | ||
| statusCode: 403, | ||
| message: 'Too many attempts', | ||
| }), | ||
| 'better-auth API error', | ||
| ) | ||
| }) | ||
|
|
||
| it('falls back to error.message when the body has no message', () => { | ||
| const log = makeLogger() | ||
| const err = new APIError('BAD_REQUEST') | ||
| err.message = 'bare message' | ||
| logBetterAuthApiError(err, log) | ||
|
|
||
| expect(log.warn).toHaveBeenCalledWith( | ||
| expect.objectContaining({ message: 'bare message' }), | ||
| 'better-auth API error', | ||
| ) | ||
| }) | ||
|
|
||
| it('ignores 5xx errors (already logged by better-auth itself)', () => { | ||
| const log = makeLogger() | ||
| logBetterAuthApiError( | ||
| new APIError('INTERNAL_SERVER_ERROR', { message: 'boom' }), | ||
| log, | ||
| ) | ||
|
|
||
| expect(log.warn).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('ignores 3xx redirects', () => { | ||
| const log = makeLogger() | ||
| logBetterAuthApiError(new APIError('FOUND'), log) | ||
|
|
||
| expect(log.warn).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('ignores non-APIError values', () => { | ||
| const log = makeLogger() | ||
| logBetterAuthApiError(new Error('some other failure'), log) | ||
| logBetterAuthApiError('a string', log) | ||
| logBetterAuthApiError(undefined, log) | ||
|
|
||
| expect(log.warn).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in de5456e — updated the module header to note the new
onAPIErrorlogging hook and dropped the stale "foundation-only, no existing behavior is changed" line.