@multiplatform.one/auth 7.11.0 → 7.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -311,6 +311,19 @@ function createKeycloakSsoPlugin(options: KeycloakSsoPluginOptions) {
311
311
  signOutSso,
312
312
  },
313
313
  hooks: {
314
+ before: [
315
+ {
316
+ // Signed out is the expected answer on every anonymous page load,
317
+ // so it is a 200 with no token, as /get-session answers, instead
318
+ // of a 4xx the browser logs. Runs before body validation, so the
319
+ // client's body-shape negotiation never 400s for a visitor either.
320
+ matcher: (ctx) => ctx.path === "/get-access-token",
321
+ handler: createAuthMiddleware(async (ctx) => {
322
+ const session = await getSessionFromCtx(ctx).catch(() => null);
323
+ if (!session) return ctx.json(null);
324
+ }),
325
+ },
326
+ ],
314
327
  after: [
315
328
  {
316
329
  matcher: (ctx) => ctx.path === "/get-access-token",
@@ -392,25 +405,33 @@ function createKeycloakSsoPlugin(options: KeycloakSsoPluginOptions) {
392
405
  } satisfies BetterAuthPlugin;
393
406
  }
394
407
 
408
+ function runtimeEnv(): Record<string, string | undefined> {
409
+ return typeof process === "undefined" ? {} : process.env;
410
+ }
411
+
395
412
  /**
396
413
  * Create a Better Auth instance configured for Keycloak.
397
414
  * Keycloak is expected at ${BASE_URL}/auth (proxied by Frappe in dev).
415
+ *
416
+ * Settings are read from the env OBJECT when createAuth runs, never as a
417
+ * static `process.env.<KEY>`: vxrn's build defines that expression for every
418
+ * key in apps/one/.env, which baked the build machine's Keycloak realm,
419
+ * client, client secret and SECRET into the deployed server bundle.
398
420
  */
399
421
  export function createAuth(options: CreateAuthOptions = {}) {
400
- const baseUrl =
401
- options.baseUrl || process.env.BASE_URL || `http://localhost:${process.env.ONE_PORT || "3000"}`;
422
+ const env = runtimeEnv();
423
+ const baseUrl = options.baseUrl || env.BASE_URL || `http://localhost:${env.ONE_PORT || "3000"}`;
402
424
  const isProduction = process.env.NODE_ENV === "production";
403
425
 
404
426
  // In production Keycloak is proxied at /auth by Frappe, so default to ${baseUrl}/auth.
405
427
  // In dev (no proxy), use KEYCLOAK_BACKCHANNEL_URL (server-to-server direct address).
406
428
  const keycloakBaseUrl =
407
- options.keycloak?.baseUrl || process.env.KEYCLOAK_BACKCHANNEL_URL || `${baseUrl}/auth`;
408
- const keycloakRealm = options.keycloak?.realm || process.env.KEYCLOAK_REALM || "master";
429
+ options.keycloak?.baseUrl || env.KEYCLOAK_BACKCHANNEL_URL || `${baseUrl}/auth`;
430
+ const keycloakRealm = options.keycloak?.realm || env.KEYCLOAK_REALM || "master";
409
431
  const issuer = `${keycloakBaseUrl}/realms/${keycloakRealm}`;
410
432
 
411
- const keycloakClientId = options.keycloak?.clientId || process.env.KEYCLOAK_CLIENT_ID || "";
412
- const keycloakClientSecret =
413
- options.keycloak?.clientSecret || process.env.KEYCLOAK_CLIENT_SECRET || "";
433
+ const keycloakClientId = options.keycloak?.clientId || env.KEYCLOAK_CLIENT_ID || "";
434
+ const keycloakClientSecret = options.keycloak?.clientSecret || env.KEYCLOAK_CLIENT_SECRET || "";
414
435
 
415
436
  const expoPlugin = options.enableExpo !== false ? expo() : undefined;
416
437
 
@@ -567,7 +588,7 @@ export function createAuth(options: CreateAuthOptions = {}) {
567
588
 
568
589
  // Database for session/account storage.
569
590
  // Reads DATABASE_URL env var, or uses an explicit option.
570
- const database = options.database ?? (process.env.DATABASE_URL || undefined);
591
+ const database = options.database ?? (env.DATABASE_URL || undefined);
571
592
 
572
593
  return betterAuth({
573
594
  basePath,
@@ -578,13 +599,13 @@ export function createAuth(options: CreateAuthOptions = {}) {
578
599
  // redirectURI getter above + the LAN trustedOrigins below instead.
579
600
  baseURL: baseUrl,
580
601
  ...(database ? { database } : {}),
581
- secret: options.secret || process.env.SECRET || "",
602
+ secret: options.secret || env.SECRET || "",
582
603
  trustedOrigins: options.trustedOrigins || [
583
604
  baseUrl,
584
605
  keycloakBaseUrl,
585
606
  // In dev, ONE_PORT may differ from BASE_URL; ensure the dev server origin is trusted.
586
- ...(process.env.ONE_PORT && !baseUrl.includes(`:${process.env.ONE_PORT}`)
587
- ? [`http://localhost:${process.env.ONE_PORT}`]
607
+ ...(env.ONE_PORT && !baseUrl.includes(`:${env.ONE_PORT}`)
608
+ ? [`http://localhost:${env.ONE_PORT}`]
588
609
  : []),
589
610
  "multiplatform-one://",
590
611
  // Expo Go (dev) receives OAuth returns on the exp:// scheme — the app
package/types/index.d.ts CHANGED
@@ -57,6 +57,11 @@ export interface AuthLike {
57
57
  /**
58
58
  * Create a Better Auth instance configured for Keycloak.
59
59
  * Keycloak is expected at ${BASE_URL}/auth (proxied by Frappe in dev).
60
+ *
61
+ * Settings are read from the env OBJECT when createAuth runs, never as a
62
+ * static `process.env.<KEY>`: vxrn's build defines that expression for every
63
+ * key in apps/one/.env, which baked the build machine's Keycloak realm,
64
+ * client, client secret and SECRET into the deployed server bundle.
60
65
  */
61
66
  export declare function createAuth(options?: CreateAuthOptions): import("better-auth").Auth<{
62
67
  secret: string;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAE,UAAU,EAAsB,MAAM,aAAa,CAAC;AAc7D,OAAO,EACL,4BAA4B,EAC5B,0BAA0B,EAC1B,yBAAyB,EACzB,2BAA2B,EAC3B,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,sBAAsB,CAAC;AAC9B,YAAY,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAmDvF,MAAM,WAAW,eAAe;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,iBAAiB;IAChC,uDAAuD;IACvD,QAAQ,CAAC,EAAE,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