Skip to content

Commit 40b9af0

Browse files
committed
refactor(plugin-auth): read the creating request's Accept-Language through auth-manager's one reader
Claude-Session: https://claude.ai/code/session_01JVTd3PprStGa26aez5omLB Co-authored-by: Claude <noreply@anthropic.com>
1 parent 8bce042 commit 40b9af0

2 files changed

Lines changed: 7 additions & 16 deletions

File tree

‎packages/plugins/plugin-auth/src/auth-manager.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,8 +1320,12 @@ export function authEmailLocaleFromRequest(source: unknown): string | undefined
13201320
* context, or anything carrying either. Never throws: a vendor changing the
13211321
* shape it hands a callback must degrade to the deployment default, never fail
13221322
* the send.
1323+
*
1324+
* Exported for `organization-default-currency.ts`, which reads the creating
1325+
* request's language off the endpoint context the same way (#25495): one
1326+
* reader, not two.
13231327
*/
1324-
function acceptLanguageHeader(source: unknown): string | undefined {
1328+
export function acceptLanguageHeader(source: unknown): string | undefined {
13251329
if (!source || typeof source !== 'object') return undefined;
13261330
const nested = (source as { request?: unknown }).request;
13271331
const bags = [

‎packages/plugins/plugin-auth/src/organization-default-currency.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import { tryGetCurrentAuthEndpointContext } from '@better-auth/core/context';
5050
import type { IDataEngine } from '@objectstack/spec/contracts';
5151
import { preferredLocaleFromHeader, SystemObjectName } from '@objectstack/spec/system';
52+
import { acceptLanguageHeader } from './auth-manager.js';
5253

5354
/**
5455
* Region (ISO 3166-1 alpha-2 and the CLDR territory codes beside it) → the
@@ -204,25 +205,11 @@ export type CreatorLocaleSource = 'the creator\'s stored language' | 'the creati
204205

205206
/** The `accept-language` value on the better-auth endpoint context this call runs inside, if any. */
206207
function acceptLanguageOfCurrentRequest(): string | undefined {
207-
let endpoint: unknown;
208208
try {
209-
endpoint = tryGetCurrentAuthEndpointContext();
209+
return acceptLanguageHeader(tryGetCurrentAuthEndpointContext());
210210
} catch {
211211
return undefined;
212212
}
213-
if (!endpoint || typeof endpoint !== 'object') return undefined;
214-
const request = (endpoint as { request?: { headers?: unknown } }).request;
215-
for (const bag of [(endpoint as { headers?: unknown }).headers, request?.headers]) {
216-
const get = (bag as { get?: unknown } | undefined)?.get;
217-
if (typeof get !== 'function') continue;
218-
try {
219-
const value = (bag as Headers).get('accept-language');
220-
if (typeof value === 'string' && value) return value;
221-
} catch {
222-
// A header bag that throws on read leaves the request's language unknown.
223-
}
224-
}
225-
return undefined;
226213
}
227214

228215
/**

0 commit comments

Comments
 (0)