Skip to content

Commit b29b319

Browse files
committed
avoid env var usage, add tests
1 parent 83d57fb commit b29b319

3 files changed

Lines changed: 110 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export default http;
9090

9191
## Custom domain
9292

93-
If you've configured a [custom WorkOS authentication API domain](https://workos.com/docs/custom-domains/auth-api), set the `apiHostname` option (or `WORKOS_API_HOSTNAME` env var) when constructing the AuthKit client. This routes the WorkOS SDK through your domain and updates the JWT issuer and JWKS URLs to match.
93+
If you've configured a [custom WorkOS authentication API domain](https://workos.com/docs/custom-domains/auth-api), pass it as `apiHostname` when constructing the AuthKit client. This routes the WorkOS SDK through your domain and updates the JWT issuer and JWKS URLs to match.
9494

9595
```ts
9696
// convex/auth.ts
@@ -99,12 +99,6 @@ export const authKit = new AuthKit<DataModel>(components.workOSAuthKit, {
9999
});
100100
```
101101

102-
Or set it on the deployment:
103-
104-
```sh
105-
npx convex env set WORKOS_API_HOSTNAME=auth.example.com
106-
```
107-
108102
## Usage
109103

110104
User create/update/delete in WorkOS will be automatically synced by the

src/client/index.test.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/// <reference types="vite/client" />
2+
import { describe, test, expect, vi, beforeEach, afterEach } from "vitest";
3+
import { WorkOS } from "@workos-inc/node";
4+
import { AuthKit } from "./index.js";
5+
import type { ComponentApi } from "../component/_generated/component.js";
6+
7+
vi.mock("@workos-inc/node", () => {
8+
return {
9+
WorkOS: vi.fn().mockImplementation(function () {
10+
return {};
11+
}),
12+
};
13+
});
14+
15+
const requiredEnv = {
16+
WORKOS_CLIENT_ID: "client_test",
17+
WORKOS_API_KEY: "sk_test",
18+
WORKOS_WEBHOOK_SECRET: "whsec_test",
19+
};
20+
21+
const fakeComponent = {} as ComponentApi;
22+
23+
describe("AuthKit constructor", () => {
24+
beforeEach(() => {
25+
for (const [k, v] of Object.entries(requiredEnv)) {
26+
process.env[k] = v;
27+
}
28+
});
29+
30+
afterEach(() => {
31+
for (const k of Object.keys(requiredEnv)) {
32+
delete process.env[k];
33+
}
34+
vi.clearAllMocks();
35+
});
36+
37+
describe("apiHostname", () => {
38+
test("forwards apiHostname option to the WorkOS SDK", () => {
39+
new AuthKit(fakeComponent, { apiHostname: "auth.example.com" });
40+
expect(vi.mocked(WorkOS)).toHaveBeenCalledWith(
41+
"sk_test",
42+
expect.objectContaining({ apiHostname: "auth.example.com" })
43+
);
44+
});
45+
46+
test("forwards undefined when option is not set", () => {
47+
new AuthKit(fakeComponent);
48+
expect(vi.mocked(WorkOS)).toHaveBeenCalledWith(
49+
"sk_test",
50+
expect.objectContaining({ apiHostname: undefined })
51+
);
52+
});
53+
});
54+
55+
test("clientId is forwarded to the WorkOS SDK", () => {
56+
new AuthKit(fakeComponent);
57+
expect(vi.mocked(WorkOS)).toHaveBeenCalledWith(
58+
"sk_test",
59+
expect.objectContaining({ clientId: "client_test" })
60+
);
61+
});
62+
});
63+
64+
describe("AuthKit.getAuthConfigProviders", () => {
65+
beforeEach(() => {
66+
for (const [k, v] of Object.entries(requiredEnv)) {
67+
process.env[k] = v;
68+
}
69+
});
70+
71+
afterEach(() => {
72+
for (const k of Object.keys(requiredEnv)) {
73+
delete process.env[k];
74+
}
75+
vi.clearAllMocks();
76+
});
77+
78+
test("falls back to api.workos.com when no custom hostname is set", () => {
79+
const authKit = new AuthKit(fakeComponent);
80+
const providers = authKit.getAuthConfigProviders();
81+
expect(providers[0].issuer).toBe("https://api.workos.com/");
82+
expect(providers[0].jwks).toBe(
83+
"https://api.workos.com/sso/jwks/client_test"
84+
);
85+
expect(providers[1].issuer).toBe(
86+
"https://api.workos.com/user_management/client_test"
87+
);
88+
expect(providers[1].jwks).toBe(
89+
"https://api.workos.com/sso/jwks/client_test"
90+
);
91+
});
92+
93+
test("uses custom hostname from option in both providers", () => {
94+
const authKit = new AuthKit(fakeComponent, {
95+
apiHostname: "auth.example.com",
96+
});
97+
const providers = authKit.getAuthConfigProviders();
98+
expect(providers[0].issuer).toBe("https://auth.example.com/");
99+
expect(providers[0].jwks).toBe(
100+
"https://auth.example.com/sso/jwks/client_test"
101+
);
102+
expect(providers[1].issuer).toBe(
103+
"https://auth.example.com/user_management/client_test"
104+
);
105+
expect(providers[1].jwks).toBe(
106+
"https://auth.example.com/sso/jwks/client_test"
107+
);
108+
});
109+
});

src/client/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ export class AuthKit<DataModel extends GenericDataModel> {
9595
apiKey,
9696
webhookSecret,
9797
actionSecret: options?.actionSecret ?? process.env.WORKOS_ACTION_SECRET,
98-
apiHostname: options?.apiHostname ?? process.env.WORKOS_API_HOSTNAME,
9998
webhookPath: options?.webhookPath ?? "/workos/webhook",
10099
};
101100
this.workos = new WorkOS(this.config.apiKey, {

0 commit comments

Comments
 (0)