Skip to content

Commit 71f7083

Browse files
committed
fix(sso): use namespace import for samlify to fix ESM compatibility (#8697)
1 parent 65f67fc commit 71f7083

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

e2e/smoke/test/saml.spec.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import assert from "node:assert/strict";
2-
import { createRequire } from "node:module";
32
import { DatabaseSync } from "node:sqlite";
43
import { describe, test } from "node:test";
4+
import { sso } from "@better-auth/sso";
55
import { betterAuth } from "better-auth";
66
import { getMigrations } from "better-auth/db/migration";
77

8-
const { sso } = createRequire(import.meta.url)("@better-auth/sso");
9-
108
const TEST_CERT = `MIIDXTCCAkWgAwIBAgIJAOxEm08dOr3PMA0GCSqGSIb3DqEBCwUAMEUxCzAJBgNV
119
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
1210
aWRnaXRzIFB0eSBMdGQwHhcNMjMwMTAxMDAwMDAwWhcNMjQwMTAxMDAwMDAwWjBF
@@ -29,6 +27,21 @@ r1PAFY+X3xF+5qDTbPpcHFPTIEWLpJFJPkSS+Q==`;
2927
const IDP_ENTRY_POINT = "https://idp.example.com/saml2/sso";
3028

3129
describe("SAML SSO", () => {
30+
/**
31+
* @see https://github.com/better-auth/better-auth/issues/8695
32+
*
33+
* samlify is a CJS module with __esModule: true but no exports.default.
34+
* Using `import saml from "samlify"` resolves to undefined in ESM,
35+
* causing `saml.setSchemaValidator(...)` to throw at module load time.
36+
* This test verifies that @better-auth/sso loads without error under ESM
37+
* by using a native ESM import (not createRequire).
38+
*/
39+
test("should load @better-auth/sso via ESM without samlify import error", async () => {
40+
const mod = await import("@better-auth/sso");
41+
assert.ok(mod.sso, "sso export should be defined");
42+
assert.equal(typeof mod.sso, "function", "sso should be a function");
43+
});
44+
3245
test("should generate SAML login request URL via defaultSSO", async () => {
3346
const database = new DatabaseSync(":memory:");
3447
const auth = betterAuth({

packages/sso/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { BetterAuthPlugin } from "better-auth";
22
import { createAuthMiddleware, getSessionFromCtx } from "better-auth/api";
33
import { XMLValidator } from "fast-xml-parser";
4-
import saml from "samlify";
4+
import * as saml from "samlify";
55
import { SAML_SESSION_BY_ID_PREFIX } from "./constants";
66
import { assignOrganizationByDomain } from "./linking";
77
import {

packages/sso/src/routes/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { DBAdapter } from "@better-auth/core/db/adapter";
2-
import saml from "samlify";
2+
import * as saml from "samlify";
33
import type { SAMLConfig, SSOOptions, SSOProvider } from "../types";
44
import { safeJsonParse } from "../utils";
55

packages/sso/src/routes/sso.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { generateRandomString } from "better-auth/crypto";
1919
import { handleOAuthUserInfo } from "better-auth/oauth2";
2020
import { XMLParser } from "fast-xml-parser";
2121
import { decodeJwt } from "jose";
22-
import saml from "samlify";
22+
import * as saml from "samlify";
2323
import type { BindingContext } from "samlify/types/src/entity";
2424
import type { IdentityProvider } from "samlify/types/src/entity-idp";
2525
import type { FlowResult } from "samlify/types/src/flow";

0 commit comments

Comments
 (0)