@convex-dev/workos-authkit 0.1.1 → 0.1.3
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/README.md +12 -12
- package/dist/client/index.d.ts +4 -4
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +21 -21
- package/dist/client/index.js.map +1 -1
- package/package.json +2 -2
- package/src/client/index.ts +38 -30
package/README.md
CHANGED
|
@@ -64,18 +64,7 @@ app.use(workOSAuthKit);
|
|
|
64
64
|
export default app;
|
|
65
65
|
```
|
|
66
66
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
```ts
|
|
70
|
-
import { httpRouter } from "convex/server";
|
|
71
|
-
import { authKit } from "./auth";
|
|
72
|
-
|
|
73
|
-
const http = httpRouter();
|
|
74
|
-
authKit.registerRoutes(http);
|
|
75
|
-
export default http;
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
Finally, create a Convex AuthKit client within your `convex/` folder, and point it
|
|
67
|
+
Create a Convex AuthKit client within your `convex/` folder, and point it
|
|
79
68
|
to the installed component:
|
|
80
69
|
|
|
81
70
|
```ts
|
|
@@ -87,6 +76,17 @@ import type { DataModel } from "./_generated/dataModel";
|
|
|
87
76
|
export const authKit = new AuthKit<DataModel>(components.workOSAuthKit);
|
|
88
77
|
```
|
|
89
78
|
|
|
79
|
+
Finally, register component webhook routes in `convex/http.ts`:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { httpRouter } from "convex/server";
|
|
83
|
+
import { authKit } from "./auth";
|
|
84
|
+
|
|
85
|
+
const http = httpRouter();
|
|
86
|
+
authKit.registerRoutes(http);
|
|
87
|
+
export default http;
|
|
88
|
+
```
|
|
89
|
+
|
|
90
90
|
## Usage
|
|
91
91
|
|
|
92
92
|
User create/update/delete in WorkOS will be automatically synced by the
|
package/dist/client/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { WorkOS, type Event as WorkOSEvent, type ActionContext as WorkOSActionCo
|
|
|
4
4
|
import type { ComponentApi } from "../component/_generated/component.js";
|
|
5
5
|
type WorkOSResponsePayload = AuthenticationActionResponseData | UserRegistrationActionResponseData;
|
|
6
6
|
type Options = {
|
|
7
|
-
authFunctions
|
|
7
|
+
authFunctions?: AuthFunctions;
|
|
8
8
|
clientId?: string;
|
|
9
9
|
apiKey?: string;
|
|
10
10
|
webhookSecret?: string;
|
|
@@ -14,7 +14,7 @@ type Options = {
|
|
|
14
14
|
logLevel?: "DEBUG";
|
|
15
15
|
};
|
|
16
16
|
export type AuthFunctions = {
|
|
17
|
-
authKitAction
|
|
17
|
+
authKitAction?: FunctionReference<"mutation", "internal", {
|
|
18
18
|
action: unknown;
|
|
19
19
|
}, WorkOSResponsePayload>;
|
|
20
20
|
authKitEvent?: FunctionReference<"mutation", "internal", {
|
|
@@ -24,10 +24,10 @@ export type AuthFunctions = {
|
|
|
24
24
|
};
|
|
25
25
|
export declare class AuthKit<DataModel extends GenericDataModel> {
|
|
26
26
|
component: ComponentApi;
|
|
27
|
-
options
|
|
27
|
+
options?: Options | undefined;
|
|
28
28
|
workos: WorkOS;
|
|
29
29
|
private config;
|
|
30
|
-
constructor(component: ComponentApi, options
|
|
30
|
+
constructor(component: ComponentApi, options?: Options | undefined);
|
|
31
31
|
getAuthConfigProviders: () => ({
|
|
32
32
|
type: "customJwt";
|
|
33
33
|
issuer: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACvB,KAAK,UAAU,EAIhB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,EACL,MAAM,EACN,KAAK,KAAK,IAAI,WAAW,EACzB,KAAK,aAAa,IAAI,mBAAmB,EACzC,KAAK,kCAAkC,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AqB,CAAC;KACpC;;;;;IAkCD,cAAc,CAAC,IAAI,EAAE,UAAU;CA+EhC"}
|
package/dist/client/index.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { createFunctionHandle, httpActionGeneric, internalMutationGeneric, } from "convex/server";
|
|
2
2
|
import { WorkOS, } from "@workos-inc/node";
|
|
3
3
|
import { v } from "convex/values";
|
|
4
|
+
const requireEnvVar = (str, onUndefined) => {
|
|
5
|
+
if (!str) {
|
|
6
|
+
onUndefined();
|
|
7
|
+
}
|
|
8
|
+
return str;
|
|
9
|
+
};
|
|
4
10
|
export class AuthKit {
|
|
5
11
|
component;
|
|
6
12
|
options;
|
|
@@ -9,32 +15,19 @@ export class AuthKit {
|
|
|
9
15
|
constructor(component, options) {
|
|
10
16
|
this.component = component;
|
|
11
17
|
this.options = options;
|
|
12
|
-
const clientId = options?.clientId ?? process.env.WORKOS_CLIENT_ID;
|
|
13
|
-
const apiKey = options?.apiKey ?? process.env.WORKOS_API_KEY;
|
|
14
|
-
const webhookSecret = options?.webhookSecret ?? process.env.WORKOS_WEBHOOK_SECRET;
|
|
15
|
-
const actionSecret = options?.actionSecret ?? process.env.WORKOS_ACTION_SECRET;
|
|
16
18
|
const missingEnvVars = [];
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (
|
|
21
|
-
missingEnvVars.push("WORKOS_API_KEY");
|
|
22
|
-
}
|
|
23
|
-
if (!webhookSecret) {
|
|
24
|
-
missingEnvVars.push("WORKOS_WEBHOOK_SECRET");
|
|
25
|
-
}
|
|
26
|
-
if (!actionSecret) {
|
|
27
|
-
missingEnvVars.push("WORKOS_ACTION_SECRET");
|
|
28
|
-
}
|
|
29
|
-
if (!clientId || !apiKey || !webhookSecret || !actionSecret) {
|
|
19
|
+
const clientId = requireEnvVar(options?.clientId ?? process.env.WORKOS_CLIENT_ID, () => missingEnvVars.push("WORKOS_CLIENT_ID"));
|
|
20
|
+
const apiKey = requireEnvVar(options?.apiKey ?? process.env.WORKOS_API_KEY, () => missingEnvVars.push("WORKOS_API_KEY"));
|
|
21
|
+
const webhookSecret = requireEnvVar(options?.webhookSecret ?? process.env.WORKOS_WEBHOOK_SECRET, () => missingEnvVars.push("WORKOS_WEBHOOK_SECRET"));
|
|
22
|
+
if (missingEnvVars.length > 0) {
|
|
30
23
|
throw new Error(`Missing environment variables: ${missingEnvVars.join(", ")}`);
|
|
31
24
|
}
|
|
32
25
|
this.config = {
|
|
33
|
-
...options,
|
|
26
|
+
...(options ?? {}),
|
|
34
27
|
clientId,
|
|
35
28
|
apiKey,
|
|
36
29
|
webhookSecret,
|
|
37
|
-
actionSecret,
|
|
30
|
+
actionSecret: options?.actionSecret ?? process.env.WORKOS_ACTION_SECRET,
|
|
38
31
|
webhookPath: options?.webhookPath ?? "/workos/webhook",
|
|
39
32
|
};
|
|
40
33
|
this.workos = new WorkOS(this.config.apiKey);
|
|
@@ -165,10 +158,17 @@ export class AuthKit {
|
|
|
165
158
|
if (this.config.logLevel === "DEBUG") {
|
|
166
159
|
console.log("received action", action);
|
|
167
160
|
}
|
|
168
|
-
|
|
161
|
+
if (!this.config.authFunctions?.authKitAction) {
|
|
162
|
+
throw new Error("authFunctions not set in AuthKit component configuration, or no authKitAction function exported");
|
|
163
|
+
}
|
|
164
|
+
const responsePayload = await ctx.runMutation(
|
|
165
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
|
|
166
|
+
this.config.authFunctions?.authKitAction, {
|
|
169
167
|
action,
|
|
170
168
|
});
|
|
171
|
-
const response = await this.workos.actions.signResponse(responsePayload,
|
|
169
|
+
const response = await this.workos.actions.signResponse(responsePayload,
|
|
170
|
+
// We check for this in the constructor
|
|
171
|
+
this.config.actionSecret);
|
|
172
172
|
return new Response(JSON.stringify(response), { status: 200 });
|
|
173
173
|
}),
|
|
174
174
|
});
|
package/dist/client/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,oBAAoB,EACpB,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,MAAM,GAKP,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,oBAAoB,EACpB,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,MAAM,GAKP,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAkClC,MAAM,aAAa,GAAG,CACpB,GAAuB,EACvB,WAAuB,EACf,EAAE;IACV,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,WAAW,EAAE,CAAC;IAChB,CAAC;IACD,OAAO,GAAI,CAAC;AACd,CAAC,CAAC;AAEF,MAAM,OAAO,OAAO;IAIT;IACA;IAJF,MAAM,CAAS;IACd,MAAM,CAAS;IACvB,YACS,SAAuB,EACvB,OAAiB;QADjB,cAAS,GAAT,SAAS,CAAc;QACvB,YAAO,GAAP,OAAO,CAAU;QAExB,MAAM,cAAc,GAAa,EAAE,CAAC;QACpC,MAAM,QAAQ,GAAG,aAAa,CAC5B,OAAO,EAAE,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,EACjD,GAAG,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAC9C,CAAC;QACF,MAAM,MAAM,GAAG,aAAa,CAC1B,OAAO,EAAE,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,cAAc,EAC7C,GAAG,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAC5C,CAAC;QACF,MAAM,aAAa,GAAG,aAAa,CACjC,OAAO,EAAE,aAAa,IA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
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/get-convex/workos-authkit/issues"
|
|
8
8
|
},
|
|
9
|
-
"version": "0.1.
|
|
9
|
+
"version": "0.1.3",
|
|
10
10
|
"license": "Apache-2.0",
|
|
11
11
|
"keywords": [
|
|
12
12
|
"convex",
|
|
@@ -101,7 +101,7 @@
|
|
|
101
101
|
"@convex-dev/workos": "^0.0.1",
|
|
102
102
|
"@convex-dev/workpool": "^0.2.19",
|
|
103
103
|
"@workos-inc/authkit-react": "^0.13.0",
|
|
104
|
-
"@workos-inc/node": "
|
|
104
|
+
"@workos-inc/node": "7.73.0",
|
|
105
105
|
"convex-helpers": "^0.1.104",
|
|
106
106
|
"type-fest": "^5.1.0"
|
|
107
107
|
}
|
package/src/client/index.ts
CHANGED
|
@@ -25,7 +25,7 @@ type WorkOSResponsePayload =
|
|
|
25
25
|
| UserRegistrationActionResponseData;
|
|
26
26
|
|
|
27
27
|
type Options = {
|
|
28
|
-
authFunctions
|
|
28
|
+
authFunctions?: AuthFunctions;
|
|
29
29
|
clientId?: string;
|
|
30
30
|
apiKey?: string;
|
|
31
31
|
webhookSecret?: string;
|
|
@@ -34,13 +34,10 @@ type Options = {
|
|
|
34
34
|
actionSecret?: string;
|
|
35
35
|
logLevel?: "DEBUG";
|
|
36
36
|
};
|
|
37
|
-
type Config = SetRequired<
|
|
38
|
-
Options,
|
|
39
|
-
"clientId" | "apiKey" | "webhookSecret" | "actionSecret"
|
|
40
|
-
>;
|
|
37
|
+
type Config = SetRequired<Options, "clientId" | "apiKey" | "webhookSecret">;
|
|
41
38
|
|
|
42
39
|
export type AuthFunctions = {
|
|
43
|
-
authKitAction
|
|
40
|
+
authKitAction?: FunctionReference<
|
|
44
41
|
"mutation",
|
|
45
42
|
"internal",
|
|
46
43
|
{ action: unknown },
|
|
@@ -54,43 +51,47 @@ export type AuthFunctions = {
|
|
|
54
51
|
>;
|
|
55
52
|
};
|
|
56
53
|
|
|
54
|
+
const requireEnvVar = (
|
|
55
|
+
str: string | undefined,
|
|
56
|
+
onUndefined: () => void
|
|
57
|
+
): string => {
|
|
58
|
+
if (!str) {
|
|
59
|
+
onUndefined();
|
|
60
|
+
}
|
|
61
|
+
return str!;
|
|
62
|
+
};
|
|
63
|
+
|
|
57
64
|
export class AuthKit<DataModel extends GenericDataModel> {
|
|
58
65
|
public workos: WorkOS;
|
|
59
66
|
private config: Config;
|
|
60
67
|
constructor(
|
|
61
68
|
public component: ComponentApi,
|
|
62
|
-
public options
|
|
69
|
+
public options?: Options
|
|
63
70
|
) {
|
|
64
|
-
const clientId = options?.clientId ?? process.env.WORKOS_CLIENT_ID;
|
|
65
|
-
const apiKey = options?.apiKey ?? process.env.WORKOS_API_KEY;
|
|
66
|
-
const webhookSecret =
|
|
67
|
-
options?.webhookSecret ?? process.env.WORKOS_WEBHOOK_SECRET;
|
|
68
|
-
const actionSecret =
|
|
69
|
-
options?.actionSecret ?? process.env.WORKOS_ACTION_SECRET;
|
|
70
71
|
const missingEnvVars: string[] = [];
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
missingEnvVars.push("
|
|
82
|
-
|
|
83
|
-
if (
|
|
72
|
+
const clientId = requireEnvVar(
|
|
73
|
+
options?.clientId ?? process.env.WORKOS_CLIENT_ID,
|
|
74
|
+
() => missingEnvVars.push("WORKOS_CLIENT_ID")
|
|
75
|
+
);
|
|
76
|
+
const apiKey = requireEnvVar(
|
|
77
|
+
options?.apiKey ?? process.env.WORKOS_API_KEY,
|
|
78
|
+
() => missingEnvVars.push("WORKOS_API_KEY")
|
|
79
|
+
);
|
|
80
|
+
const webhookSecret = requireEnvVar(
|
|
81
|
+
options?.webhookSecret ?? process.env.WORKOS_WEBHOOK_SECRET,
|
|
82
|
+
() => missingEnvVars.push("WORKOS_WEBHOOK_SECRET")
|
|
83
|
+
);
|
|
84
|
+
if (missingEnvVars.length > 0) {
|
|
84
85
|
throw new Error(
|
|
85
86
|
`Missing environment variables: ${missingEnvVars.join(", ")}`
|
|
86
87
|
);
|
|
87
88
|
}
|
|
88
89
|
this.config = {
|
|
89
|
-
...options,
|
|
90
|
+
...(options ?? {}),
|
|
90
91
|
clientId,
|
|
91
92
|
apiKey,
|
|
92
93
|
webhookSecret,
|
|
93
|
-
actionSecret,
|
|
94
|
+
actionSecret: options?.actionSecret ?? process.env.WORKOS_ACTION_SECRET,
|
|
94
95
|
webhookPath: options?.webhookPath ?? "/workos/webhook",
|
|
95
96
|
};
|
|
96
97
|
this.workos = new WorkOS(this.config.apiKey);
|
|
@@ -261,15 +262,22 @@ export class AuthKit<DataModel extends GenericDataModel> {
|
|
|
261
262
|
if (this.config.logLevel === "DEBUG") {
|
|
262
263
|
console.log("received action", action);
|
|
263
264
|
}
|
|
265
|
+
if (!this.config.authFunctions?.authKitAction) {
|
|
266
|
+
throw new Error(
|
|
267
|
+
"authFunctions not set in AuthKit component configuration, or no authKitAction function exported"
|
|
268
|
+
);
|
|
269
|
+
}
|
|
264
270
|
const responsePayload: WorkOSResponsePayload = await ctx.runMutation(
|
|
265
|
-
|
|
271
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
|
|
272
|
+
this.config.authFunctions?.authKitAction!,
|
|
266
273
|
{
|
|
267
274
|
action,
|
|
268
275
|
}
|
|
269
276
|
);
|
|
270
277
|
const response = await this.workos.actions.signResponse(
|
|
271
278
|
responsePayload,
|
|
272
|
-
this
|
|
279
|
+
// We check for this in the constructor
|
|
280
|
+
this.config.actionSecret!
|
|
273
281
|
);
|
|
274
282
|
return new Response(JSON.stringify(response), { status: 200 });
|
|
275
283
|
}),
|