@farm.js/workos 0.1.0-beta.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/LICENSE +22 -0
- package/README.md +11 -0
- package/dist/client.d.ts +25 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +15 -0
- package/dist/index.d.ts +97 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +252 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Farm.js Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface WorkOSRedirectQuery {
|
|
2
|
+
returnTo?: string;
|
|
3
|
+
}
|
|
4
|
+
export interface WorkOSRedirectResult {
|
|
5
|
+
redirectTo: string;
|
|
6
|
+
}
|
|
7
|
+
export interface WorkOSSessionResult {
|
|
8
|
+
authenticated: boolean;
|
|
9
|
+
sessionId?: string;
|
|
10
|
+
organizationId?: string;
|
|
11
|
+
user?: {
|
|
12
|
+
id: string;
|
|
13
|
+
email: string;
|
|
14
|
+
firstName?: string | null;
|
|
15
|
+
lastName?: string | null;
|
|
16
|
+
profilePictureUrl?: string | null;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export declare const workosClient: {
|
|
20
|
+
login: import("@farm.js/core/client").FarmIntegrationAPIOperation<never, WorkOSRedirectQuery, WorkOSRedirectResult, false, "GET">;
|
|
21
|
+
signup: import("@farm.js/core/client").FarmIntegrationAPIOperation<never, WorkOSRedirectQuery, WorkOSRedirectResult, false, "GET">;
|
|
22
|
+
logout: import("@farm.js/core/client").FarmIntegrationAPIOperation<never, never, WorkOSRedirectResult, false, "POST">;
|
|
23
|
+
session: import("@farm.js/core/client").FarmIntegrationAPIOperation<never, never, WorkOSSessionResult, false, "GET">;
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,oBAAoB;IACnC,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,mBAAmB;IAClC,aAAa,EAAE,OAAO,CAAC;IACvB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,IAAI,CAAC,EAAE;QACL,EAAE,EAAE,MAAM,CAAC;QACX,KAAK,EAAE,MAAM,CAAC;QACd,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAC1B,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QACzB,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;KACnC,CAAC;CACH;AAED,eAAO,MAAM,YAAY;;;;;CAaxB,CAAC"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { api } from "@farm.js/core/client";
|
|
2
|
+
export const workosClient = {
|
|
3
|
+
login: api.get("/login", {
|
|
4
|
+
responseFormat: "json",
|
|
5
|
+
}),
|
|
6
|
+
signup: api.get("/signup", {
|
|
7
|
+
responseFormat: "json",
|
|
8
|
+
}),
|
|
9
|
+
logout: api.post("/logout", {
|
|
10
|
+
responseFormat: "json",
|
|
11
|
+
}),
|
|
12
|
+
session: api.get("/auth/session", {
|
|
13
|
+
responseFormat: "json",
|
|
14
|
+
}),
|
|
15
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { type FarmIntegrationLogger } from "@farm.js/core";
|
|
2
|
+
import type { WorkOSRedirectResult } from "./client.js";
|
|
3
|
+
export interface WorkOSIntegrationInput {
|
|
4
|
+
clientId?: string;
|
|
5
|
+
apiKey?: string;
|
|
6
|
+
cookiePassword?: string;
|
|
7
|
+
cookieName?: string;
|
|
8
|
+
loginPath?: string;
|
|
9
|
+
signUpPath?: string;
|
|
10
|
+
callbackPath?: string;
|
|
11
|
+
logoutPath?: string;
|
|
12
|
+
sessionPath?: string;
|
|
13
|
+
protectedRoutes?: string | string[];
|
|
14
|
+
log?: FarmIntegrationLogger;
|
|
15
|
+
}
|
|
16
|
+
interface ResolvedWorkOSConfig {
|
|
17
|
+
clientId: string;
|
|
18
|
+
apiKey: string;
|
|
19
|
+
cookiePassword: string;
|
|
20
|
+
}
|
|
21
|
+
export declare function workos(input?: WorkOSIntegrationInput): import("@farm.js/core").DefinedIntegration<{
|
|
22
|
+
category: "auth";
|
|
23
|
+
type: string;
|
|
24
|
+
instance: {
|
|
25
|
+
clientId: string;
|
|
26
|
+
apiKey: string;
|
|
27
|
+
protectedRoutes: string[];
|
|
28
|
+
};
|
|
29
|
+
config: import("@farm.js/core").FarmIntegrationConfigDefinition<ResolvedWorkOSConfig, import("@farm.js/core").FarmIntegrationSchema | undefined>;
|
|
30
|
+
api: {
|
|
31
|
+
[x: string]: {
|
|
32
|
+
get: {
|
|
33
|
+
readonly kind: "farm-integration-api-operation";
|
|
34
|
+
path: string;
|
|
35
|
+
method: "GET";
|
|
36
|
+
bodyFormat?: import("@farm.js/core/client").FarmIntegrationAPIBodyFormat | undefined;
|
|
37
|
+
responseFormat?: import("@farm.js/core/client").FarmIntegrationAPIResponseFormat | undefined;
|
|
38
|
+
headers?: {
|
|
39
|
+
[x: string]: string;
|
|
40
|
+
} | undefined;
|
|
41
|
+
credentials?: RequestCredentials | undefined;
|
|
42
|
+
isServer?: false | undefined;
|
|
43
|
+
__pathless?: boolean | undefined;
|
|
44
|
+
__types?: {
|
|
45
|
+
body: never;
|
|
46
|
+
query: never;
|
|
47
|
+
response: {
|
|
48
|
+
redirectTo: string;
|
|
49
|
+
authenticated: boolean;
|
|
50
|
+
sessionId?: string | undefined;
|
|
51
|
+
organizationId?: string | undefined;
|
|
52
|
+
user?: {
|
|
53
|
+
id: string;
|
|
54
|
+
email: string;
|
|
55
|
+
firstName?: string | null | undefined;
|
|
56
|
+
lastName?: string | null | undefined;
|
|
57
|
+
profilePictureUrl?: string | null | undefined;
|
|
58
|
+
} | undefined;
|
|
59
|
+
};
|
|
60
|
+
} | undefined;
|
|
61
|
+
};
|
|
62
|
+
post: {
|
|
63
|
+
readonly kind: "farm-integration-api-operation";
|
|
64
|
+
path: string;
|
|
65
|
+
method: "POST";
|
|
66
|
+
bodyFormat?: import("@farm.js/core/client").FarmIntegrationAPIBodyFormat | undefined;
|
|
67
|
+
responseFormat?: import("@farm.js/core/client").FarmIntegrationAPIResponseFormat | undefined;
|
|
68
|
+
headers?: {
|
|
69
|
+
[x: string]: string;
|
|
70
|
+
} | undefined;
|
|
71
|
+
credentials?: RequestCredentials | undefined;
|
|
72
|
+
isServer?: false | undefined;
|
|
73
|
+
__pathless?: boolean | undefined;
|
|
74
|
+
__types?: {
|
|
75
|
+
body: never;
|
|
76
|
+
query: never;
|
|
77
|
+
response: {
|
|
78
|
+
redirectTo: string;
|
|
79
|
+
};
|
|
80
|
+
} | undefined;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
log: FarmIntegrationLogger | undefined;
|
|
85
|
+
documentNavigations: {
|
|
86
|
+
matcher: string[];
|
|
87
|
+
}[];
|
|
88
|
+
routes: (import("@farm.js/core").FarmTypedIntegrationRoute<string, never, any, unknown, false, "GET", import("@farm.js/core").FarmIntegrationSchema | undefined> | import("@farm.js/core").FarmTypedIntegrationRoute<string, {
|
|
89
|
+
returnTo?: string;
|
|
90
|
+
}, never, WorkOSRedirectResult, false, "POST", import("@farm.js/core").FarmIntegrationSchema | undefined>)[];
|
|
91
|
+
middleware: {
|
|
92
|
+
matcher: string[];
|
|
93
|
+
handler(request: Request): Promise<Response | undefined>;
|
|
94
|
+
}[];
|
|
95
|
+
}>;
|
|
96
|
+
export {};
|
|
97
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAuC,KAAK,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAWhG,OAAO,KAAK,EAAuB,oBAAoB,EAAuB,MAAM,aAAa,CAAC;AAGlG,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,eAAe,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACpC,GAAG,CAAC,EAAE,qBAAqB,CAAC;CAC7B;AAID,UAAU,oBAAoB;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,MAAM,CAAC;CACxB;AA+ED,wBAAgB,MAAM,CAAC,KAAK,GAAE,sBAA2B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBAkJG,MAAM;;;;yBAoE7B,OAAO;;GAe3C"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { WorkOS } from "@workos-inc/node";
|
|
2
|
+
import { defineIntegration, integrationRoute } from "@farm.js/core";
|
|
3
|
+
import { clearRequestCookie, createPathInferredClientApi, createDocumentNavigationMatchers, createRequestCookie, getCookieValue, getReturnTo, integrationConfig, normalizeMatchers, } from "@farm.js/integration-utils";
|
|
4
|
+
import { workosClient } from "./client.js";
|
|
5
|
+
const DEV_COOKIE_PASSWORD = "farmjs-workos-cookie-password-development-2026";
|
|
6
|
+
function createWorkOSApi(input) {
|
|
7
|
+
return createPathInferredClientApi({
|
|
8
|
+
path: input.loginPath,
|
|
9
|
+
operation: workosClient.login,
|
|
10
|
+
}, {
|
|
11
|
+
path: input.signUpPath,
|
|
12
|
+
operation: workosClient.signup,
|
|
13
|
+
}, {
|
|
14
|
+
path: input.logoutPath,
|
|
15
|
+
operation: workosClient.logout,
|
|
16
|
+
}, {
|
|
17
|
+
path: input.sessionPath,
|
|
18
|
+
operation: workosClient.session,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function resolveEnv(input) {
|
|
22
|
+
const clientId = input.clientId ?? process.env.WORKOS_CLIENT_ID ?? "";
|
|
23
|
+
const apiKey = input.apiKey ?? process.env.WORKOS_API_KEY ?? "";
|
|
24
|
+
const cookiePassword = input.cookiePassword ??
|
|
25
|
+
process.env.WORKOS_COOKIE_PASSWORD ??
|
|
26
|
+
process.env.FARM_WORKOS_COOKIE_PASSWORD ??
|
|
27
|
+
(process.env.NODE_ENV === "production" ? "" : DEV_COOKIE_PASSWORD);
|
|
28
|
+
if (!clientId || !apiKey) {
|
|
29
|
+
throw new Error("WorkOS integration requires WORKOS_CLIENT_ID and WORKOS_API_KEY.");
|
|
30
|
+
}
|
|
31
|
+
if (!cookiePassword) {
|
|
32
|
+
throw new Error("WorkOS integration requires WORKOS_COOKIE_PASSWORD in production.");
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
clientId,
|
|
36
|
+
apiKey,
|
|
37
|
+
cookiePassword,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
async function getSession(workos, request, cookieName, cookiePassword) {
|
|
41
|
+
const sessionData = getCookieValue(request.headers, cookieName);
|
|
42
|
+
if (!sessionData) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const session = workos.userManagement.loadSealedSession({
|
|
46
|
+
sessionData,
|
|
47
|
+
cookiePassword,
|
|
48
|
+
});
|
|
49
|
+
const authentication = await session.authenticate();
|
|
50
|
+
if (!authentication.authenticated) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
session,
|
|
55
|
+
authentication,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export function workos(input = {}) {
|
|
59
|
+
const { clientId, apiKey, cookiePassword } = resolveEnv(input);
|
|
60
|
+
const protectedMatchers = normalizeMatchers(input.protectedRoutes);
|
|
61
|
+
const cookieName = input.cookieName ?? "wos-session";
|
|
62
|
+
const loginPath = input.loginPath ?? "/login";
|
|
63
|
+
const signUpPath = input.signUpPath ?? "/signup";
|
|
64
|
+
const callbackPath = input.callbackPath ?? "/callback";
|
|
65
|
+
const logoutPath = input.logoutPath ?? "/logout";
|
|
66
|
+
const sessionPath = input.sessionPath ?? "/auth/session";
|
|
67
|
+
const workos = new WorkOS({
|
|
68
|
+
apiKey,
|
|
69
|
+
clientId,
|
|
70
|
+
});
|
|
71
|
+
async function redirectToAuth(request, screenHint) {
|
|
72
|
+
const requestUrl = new URL(request.url);
|
|
73
|
+
const returnTo = getReturnTo(requestUrl.searchParams.get("returnTo"), "/dashboard");
|
|
74
|
+
const callbackUrl = new URL(callbackPath, requestUrl.origin);
|
|
75
|
+
const authorizationUrl = workos.userManagement.getAuthorizationUrl({
|
|
76
|
+
provider: "authkit",
|
|
77
|
+
clientId,
|
|
78
|
+
redirectUri: callbackUrl.toString(),
|
|
79
|
+
screenHint,
|
|
80
|
+
state: JSON.stringify({ returnTo }),
|
|
81
|
+
});
|
|
82
|
+
return {
|
|
83
|
+
redirectTo: authorizationUrl,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return defineIntegration({
|
|
87
|
+
category: "auth",
|
|
88
|
+
type: "workos",
|
|
89
|
+
instance: {
|
|
90
|
+
clientId,
|
|
91
|
+
apiKey: "[redacted]",
|
|
92
|
+
protectedRoutes: protectedMatchers,
|
|
93
|
+
},
|
|
94
|
+
config: integrationConfig({
|
|
95
|
+
label: "WorkOS integration",
|
|
96
|
+
env: {
|
|
97
|
+
clientId: "WORKOS_CLIENT_ID",
|
|
98
|
+
apiKey: "WORKOS_API_KEY",
|
|
99
|
+
cookiePassword: ["WORKOS_COOKIE_PASSWORD", "FARM_WORKOS_COOKIE_PASSWORD"],
|
|
100
|
+
},
|
|
101
|
+
input: {
|
|
102
|
+
clientId,
|
|
103
|
+
apiKey,
|
|
104
|
+
cookiePassword,
|
|
105
|
+
},
|
|
106
|
+
required: ["clientId", "apiKey", "cookiePassword"],
|
|
107
|
+
}),
|
|
108
|
+
api: createWorkOSApi({
|
|
109
|
+
loginPath,
|
|
110
|
+
signUpPath,
|
|
111
|
+
logoutPath,
|
|
112
|
+
sessionPath,
|
|
113
|
+
}),
|
|
114
|
+
log: input.log,
|
|
115
|
+
documentNavigations: [
|
|
116
|
+
{
|
|
117
|
+
matcher: createDocumentNavigationMatchers(loginPath, signUpPath, callbackPath),
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
routes: [
|
|
121
|
+
integrationRoute.get(loginPath, {
|
|
122
|
+
responseFormat: "json",
|
|
123
|
+
async handler(request) {
|
|
124
|
+
const result = await redirectToAuth(request, "sign-in");
|
|
125
|
+
if (request.headers.get("x-farm-integration-client") === "1") {
|
|
126
|
+
return Response.json(result);
|
|
127
|
+
}
|
|
128
|
+
return Response.redirect(result.redirectTo, 302);
|
|
129
|
+
},
|
|
130
|
+
}),
|
|
131
|
+
integrationRoute.get(signUpPath, {
|
|
132
|
+
responseFormat: "json",
|
|
133
|
+
async handler(request) {
|
|
134
|
+
const result = await redirectToAuth(request, "sign-up");
|
|
135
|
+
if (request.headers.get("x-farm-integration-client") === "1") {
|
|
136
|
+
return Response.json(result);
|
|
137
|
+
}
|
|
138
|
+
return Response.redirect(result.redirectTo, 302);
|
|
139
|
+
},
|
|
140
|
+
}),
|
|
141
|
+
integrationRoute.get(callbackPath, {
|
|
142
|
+
async handler(request) {
|
|
143
|
+
const requestUrl = new URL(request.url);
|
|
144
|
+
const code = requestUrl.searchParams.get("code");
|
|
145
|
+
const error = requestUrl.searchParams.get("error");
|
|
146
|
+
const errorDescription = requestUrl.searchParams.get("error_description");
|
|
147
|
+
if (error) {
|
|
148
|
+
return new Response(errorDescription || error, { status: 400 });
|
|
149
|
+
}
|
|
150
|
+
if (!code) {
|
|
151
|
+
return new Response("Missing WorkOS authorization code.", { status: 400 });
|
|
152
|
+
}
|
|
153
|
+
const authentication = await workos.userManagement.authenticateWithCode({
|
|
154
|
+
clientId,
|
|
155
|
+
code,
|
|
156
|
+
session: {
|
|
157
|
+
sealSession: true,
|
|
158
|
+
cookiePassword,
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
if (!authentication.sealedSession) {
|
|
162
|
+
return new Response("WorkOS did not return a sealed session.", { status: 500 });
|
|
163
|
+
}
|
|
164
|
+
let returnTo = "/dashboard";
|
|
165
|
+
const rawState = requestUrl.searchParams.get("state");
|
|
166
|
+
if (rawState) {
|
|
167
|
+
try {
|
|
168
|
+
const parsed = JSON.parse(rawState);
|
|
169
|
+
returnTo = getReturnTo(parsed.returnTo ?? null, "/dashboard");
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
returnTo = "/dashboard";
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const headers = new Headers();
|
|
176
|
+
headers.set("set-cookie", createRequestCookie(cookieName, authentication.sealedSession, request));
|
|
177
|
+
headers.set("location", new URL(returnTo, requestUrl.origin).toString());
|
|
178
|
+
return new Response(null, {
|
|
179
|
+
status: 302,
|
|
180
|
+
headers,
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
}),
|
|
184
|
+
integrationRoute.post(logoutPath, {
|
|
185
|
+
responseFormat: "json",
|
|
186
|
+
async handler(request) {
|
|
187
|
+
const requestUrl = new URL(request.url);
|
|
188
|
+
const sessionState = await getSession(workos, request, cookieName, cookiePassword);
|
|
189
|
+
const headers = new Headers();
|
|
190
|
+
headers.set("set-cookie", clearRequestCookie(cookieName, request));
|
|
191
|
+
if (!sessionState) {
|
|
192
|
+
const redirectTo = new URL("/", requestUrl.origin).toString();
|
|
193
|
+
if (request.headers.get("x-farm-integration-client") === "1") {
|
|
194
|
+
return Response.json({ redirectTo }, { status: 200, headers });
|
|
195
|
+
}
|
|
196
|
+
headers.set("location", redirectTo);
|
|
197
|
+
return new Response(null, { status: 303, headers });
|
|
198
|
+
}
|
|
199
|
+
const logoutUrl = await sessionState.session.getLogoutUrl({
|
|
200
|
+
returnTo: requestUrl.origin,
|
|
201
|
+
});
|
|
202
|
+
if (request.headers.get("x-farm-integration-client") === "1") {
|
|
203
|
+
return Response.json({ redirectTo: logoutUrl }, { status: 200, headers });
|
|
204
|
+
}
|
|
205
|
+
headers.set("location", logoutUrl);
|
|
206
|
+
return new Response(null, { status: 303, headers });
|
|
207
|
+
},
|
|
208
|
+
}),
|
|
209
|
+
integrationRoute.get(sessionPath, {
|
|
210
|
+
responseFormat: "json",
|
|
211
|
+
async handler(request) {
|
|
212
|
+
const sessionState = await getSession(workos, request, cookieName, cookiePassword);
|
|
213
|
+
if (!sessionState) {
|
|
214
|
+
return Response.json({
|
|
215
|
+
authenticated: false,
|
|
216
|
+
}, { status: 401 });
|
|
217
|
+
}
|
|
218
|
+
const { authentication } = sessionState;
|
|
219
|
+
return Response.json({
|
|
220
|
+
authenticated: true,
|
|
221
|
+
sessionId: authentication.sessionId,
|
|
222
|
+
organizationId: authentication.organizationId,
|
|
223
|
+
user: {
|
|
224
|
+
id: authentication.user.id,
|
|
225
|
+
email: authentication.user.email,
|
|
226
|
+
firstName: authentication.user.firstName,
|
|
227
|
+
lastName: authentication.user.lastName,
|
|
228
|
+
profilePictureUrl: authentication.user.profilePictureUrl,
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
},
|
|
232
|
+
}),
|
|
233
|
+
],
|
|
234
|
+
middleware: protectedMatchers.length > 0
|
|
235
|
+
? [
|
|
236
|
+
{
|
|
237
|
+
matcher: protectedMatchers,
|
|
238
|
+
async handler(request) {
|
|
239
|
+
const sessionState = await getSession(workos, request, cookieName, cookiePassword);
|
|
240
|
+
if (sessionState) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
const requestUrl = new URL(request.url);
|
|
244
|
+
const loginUrl = new URL(loginPath, request.url);
|
|
245
|
+
loginUrl.searchParams.set("returnTo", `${requestUrl.pathname}${requestUrl.search}`);
|
|
246
|
+
return Response.redirect(loginUrl, 307);
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
]
|
|
250
|
+
: [],
|
|
251
|
+
});
|
|
252
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@farm.js/workos",
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "WorkOS integration for Farm.js",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/farming-labs/farm.js",
|
|
9
|
+
"directory": "packages/farm-workos"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"module": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./client": {
|
|
24
|
+
"types": "./dist/client.d.ts",
|
|
25
|
+
"import": "./dist/client.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@workos-inc/node": "^8.9.0",
|
|
33
|
+
"@farm.js/core": "0.1.0-beta.0",
|
|
34
|
+
"@farm.js/integration-utils": "0.1.0-beta.0"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc",
|
|
38
|
+
"dev": "tsc --watch",
|
|
39
|
+
"type-check": "tsc --noEmit",
|
|
40
|
+
"test": "echo 'No tests in this package'"
|
|
41
|
+
}
|
|
42
|
+
}
|