@intelligo-dev/auth 1.0.0-beta.1
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 +201 -0
- package/dist/client.js +25 -0
- package/dist/client.js.map +1 -0
- package/dist/edge.js +39 -0
- package/dist/edge.js.map +1 -0
- package/dist/helpers.js +248 -0
- package/dist/helpers.js.map +1 -0
- package/dist/impersonation.js +42 -0
- package/dist/impersonation.js.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/onboarding/errors.js +31 -0
- package/dist/onboarding/errors.js.map +1 -0
- package/dist/onboarding/schemas.js +24 -0
- package/dist/onboarding/schemas.js.map +1 -0
- package/dist/onboarding/service.js +147 -0
- package/dist/onboarding/service.js.map +1 -0
- package/dist/org-api.js +86 -0
- package/dist/org-api.js.map +1 -0
- package/dist/profile/errors.js +32 -0
- package/dist/profile/errors.js.map +1 -0
- package/dist/profile/schemas.js +20 -0
- package/dist/profile/schemas.js.map +1 -0
- package/dist/profile/service.js +157 -0
- package/dist/profile/service.js.map +1 -0
- package/dist/roles.js +17 -0
- package/dist/roles.js.map +1 -0
- package/dist/server.js +251 -0
- package/dist/server.js.map +1 -0
- package/dist/team/errors.js +31 -0
- package/dist/team/errors.js.map +1 -0
- package/dist/team/schemas.js +29 -0
- package/dist/team/schemas.js.map +1 -0
- package/dist/team/service.js +438 -0
- package/dist/team/service.js.map +1 -0
- package/dist/workspace/errors.js +32 -0
- package/dist/workspace/errors.js.map +1 -0
- package/dist/workspace/schemas.js +45 -0
- package/dist/workspace/schemas.js.map +1 -0
- package/dist/workspace/service.js +268 -0
- package/dist/workspace/service.js.map +1 -0
- package/dist/workspace-init.js +121 -0
- package/dist/workspace-init.js.map +1 -0
- package/package.json +58 -0
- package/src/client.ts +27 -0
- package/src/edge.ts +43 -0
- package/src/helpers.ts +317 -0
- package/src/impersonation.ts +57 -0
- package/src/index.ts +109 -0
- package/src/onboarding/errors.ts +52 -0
- package/src/onboarding/schemas.ts +27 -0
- package/src/onboarding/service.ts +174 -0
- package/src/org-api.ts +198 -0
- package/src/profile/errors.ts +58 -0
- package/src/profile/schemas.ts +23 -0
- package/src/profile/service.ts +208 -0
- package/src/roles.ts +17 -0
- package/src/server.ts +305 -0
- package/src/team/errors.ts +71 -0
- package/src/team/schemas.ts +35 -0
- package/src/team/service.ts +611 -0
- package/src/workspace/errors.ts +69 -0
- package/src/workspace/schemas.ts +57 -0
- package/src/workspace/service.ts +381 -0
- package/src/workspace-init.ts +140 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Intelligo
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Better-Auth Client
|
|
3
|
+
*
|
|
4
|
+
* Client-side authentication utilities for React components.
|
|
5
|
+
* This module uses "better-auth/react" which is a client-side module.
|
|
6
|
+
*
|
|
7
|
+
* IMPORTANT: Only import this in "use client" components.
|
|
8
|
+
* For server-side auth, use @intelligo-dev/auth (server.ts)
|
|
9
|
+
*/
|
|
10
|
+
import { createAuthClient } from "better-auth/react";
|
|
11
|
+
import { organizationClient } from "better-auth/client/plugins";
|
|
12
|
+
// No `baseURL`: the client and the Better-Auth server always live on the
|
|
13
|
+
// same origin (this app's own `/api/auth/*` routes), so Better-Auth's
|
|
14
|
+
// client defaults to `window.location.origin` and every request is
|
|
15
|
+
// same-origin. Do not reintroduce an absolute `NEXT_PUBLIC_APP_URL`
|
|
16
|
+
// baseURL here — the dev port has drifted before (3000 -> 3001 -> 4000)
|
|
17
|
+
// and an absolute cross-origin URL is blocked by the app's CSP
|
|
18
|
+
// (`connect-src 'self'`) whenever it doesn't match the port the app is
|
|
19
|
+
// actually served on.
|
|
20
|
+
export const authClient = createAuthClient({
|
|
21
|
+
plugins: [organizationClient()],
|
|
22
|
+
});
|
|
23
|
+
// Export commonly used hooks and methods for convenience
|
|
24
|
+
export const { useSession, signIn, signUp, signOut } = authClient;
|
|
25
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAEhE,yEAAyE;AACzE,sEAAsE;AACtE,mEAAmE;AACnE,oEAAoE;AACpE,wEAAwE;AACxE,+DAA+D;AAC/D,uEAAuE;AACvE,sBAAsB;AACtB,MAAM,CAAC,MAAM,UAAU,GAAG,gBAAgB,CAAC;IACzC,OAAO,EAAE,CAAC,kBAAkB,EAAE,CAAC;CAChC,CAAC,CAAC;AAEH,yDAAyD;AACzD,MAAM,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,UAAU,CAAC"}
|
package/dist/edge.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Edge-safe session presence check.
|
|
3
|
+
*
|
|
4
|
+
* Middleware runs in the Edge Runtime, where no TCP Postgres driver is
|
|
5
|
+
* available. The previous version of this module solved that by
|
|
6
|
+
* instantiating Better-Auth against the Neon HTTP driver — which made
|
|
7
|
+
* middleware silently Neon-only. Against any other Postgres the session
|
|
8
|
+
* query failed with `TypeError: fetch failed`, Better-Auth turned that
|
|
9
|
+
* into `APIError: Failed to get session`, and the middleware threw: every
|
|
10
|
+
* request to the app returned HTTP 500. The old code hid this behind a
|
|
11
|
+
* `NODE_ENV === "development"` bypass that skipped auth entirely, so the
|
|
12
|
+
* failure only appeared in a production build on a non-Neon database.
|
|
13
|
+
*
|
|
14
|
+
* The fix is to stop querying the database from the edge at all.
|
|
15
|
+
* Middleware performs an OPTIMISTIC check: it reads the session cookie
|
|
16
|
+
* and decides where to send the request. It never asserts that the
|
|
17
|
+
* session is valid.
|
|
18
|
+
*
|
|
19
|
+
* The authoritative check stays server-side, where it always was —
|
|
20
|
+
* `requireAuth`/`requireWorkspace`/`requireRole` and the authenticated
|
|
21
|
+
* layout's own `getAuthSession()` redirect. A forged or expired cookie
|
|
22
|
+
* gets past middleware and is then rejected by the page or action that
|
|
23
|
+
* actually reads data. This is the pattern Better-Auth documents for
|
|
24
|
+
* Next.js middleware, and it is what makes the guard work on any
|
|
25
|
+
* Postgres, in any runtime, with one code path in every environment.
|
|
26
|
+
*/
|
|
27
|
+
import { getSessionCookie } from "better-auth/cookies";
|
|
28
|
+
/**
|
|
29
|
+
* True when the request carries a Better-Auth session cookie.
|
|
30
|
+
*
|
|
31
|
+
* Presence only — the cookie's signature is NOT verified and no
|
|
32
|
+
* database read happens. Never use this to authorize access to data;
|
|
33
|
+
* use it to choose a redirect. Authorization belongs to `requireAuth`
|
|
34
|
+
* and friends, which run in the Node runtime against the real session.
|
|
35
|
+
*/
|
|
36
|
+
export function hasSessionCookie(request) {
|
|
37
|
+
return getSessionCookie(request) !== null;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=edge.js.map
|
package/dist/edge.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"edge.js","sourceRoot":"","sources":["../src/edge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAgB;IAC/C,OAAO,gBAAgB,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC;AAC5C,CAAC"}
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-Side Auth Helpers
|
|
3
|
+
*
|
|
4
|
+
* Utilities for server components and server actions to check authentication.
|
|
5
|
+
* These helpers use Better-Auth's session API with Next.js headers.
|
|
6
|
+
*
|
|
7
|
+
* Usage in server actions (TECH-10 pattern):
|
|
8
|
+
* ```typescript
|
|
9
|
+
* export async function myServerAction() {
|
|
10
|
+
* const { user } = await requireAuth();
|
|
11
|
+
* // ... proceed with authenticated user
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
import { headers } from "next/headers";
|
|
16
|
+
import { auth } from "./server";
|
|
17
|
+
import { createLogger } from "@intelligo-dev/core/logger";
|
|
18
|
+
import { db } from "@intelligo-dev/core/db";
|
|
19
|
+
import { users } from "@intelligo-dev/core/db/schema";
|
|
20
|
+
import { eq } from "drizzle-orm";
|
|
21
|
+
import { PLATFORM_ADMIN_ROLE } from "./roles";
|
|
22
|
+
const log = createLogger("Auth");
|
|
23
|
+
/**
|
|
24
|
+
* Get current auth session from request headers.
|
|
25
|
+
*
|
|
26
|
+
* Returns session and user if authenticated, null otherwise.
|
|
27
|
+
* Use this in server components that handle both authenticated and unauthenticated states.
|
|
28
|
+
*/
|
|
29
|
+
export async function getAuthSession() {
|
|
30
|
+
const session = await auth.api.getSession({
|
|
31
|
+
headers: await headers(),
|
|
32
|
+
});
|
|
33
|
+
if (!session?.user) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
session: session.session,
|
|
38
|
+
user: session.user,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Require authentication.
|
|
43
|
+
*
|
|
44
|
+
* Throws "Unauthorized" error if no valid session.
|
|
45
|
+
* Use this in server actions to enforce authentication (TECH-10 pattern).
|
|
46
|
+
*
|
|
47
|
+
* @throws Error with "Unauthorized" message if not authenticated
|
|
48
|
+
*/
|
|
49
|
+
export async function requireAuth() {
|
|
50
|
+
const result = await getAuthSession();
|
|
51
|
+
if (!result) {
|
|
52
|
+
throw new Error("Unauthorized");
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Get workspace context by organization ID.
|
|
58
|
+
* Use this when you know the org ID (e.g., just after creating/activating a workspace).
|
|
59
|
+
*
|
|
60
|
+
* @param organizationId - The organization ID to get context for
|
|
61
|
+
*/
|
|
62
|
+
export async function getWorkspaceContextById(organizationId) {
|
|
63
|
+
const authResult = await getAuthSession();
|
|
64
|
+
if (!authResult) {
|
|
65
|
+
log.debug("getWorkspaceContextById: no auth session");
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
log.debug("getWorkspaceContextById: fetching org");
|
|
69
|
+
// Get organization by ID directly (not from session)
|
|
70
|
+
const org = await auth.api.getFullOrganization({
|
|
71
|
+
headers: await headers(),
|
|
72
|
+
query: { organizationId },
|
|
73
|
+
});
|
|
74
|
+
if (!org) {
|
|
75
|
+
log.debug("getWorkspaceContextById: org not found");
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
// Find user's membership
|
|
79
|
+
const membership = org.members?.find((m) => m.userId === authResult.user.id);
|
|
80
|
+
if (!membership) {
|
|
81
|
+
log.debug("getWorkspaceContextById: user not a member");
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
session: authResult.session,
|
|
86
|
+
user: authResult.user,
|
|
87
|
+
workspace: {
|
|
88
|
+
id: org.id,
|
|
89
|
+
name: org.name,
|
|
90
|
+
slug: org.slug,
|
|
91
|
+
logo: org.logo,
|
|
92
|
+
},
|
|
93
|
+
membership: {
|
|
94
|
+
id: membership.id,
|
|
95
|
+
role: membership.role,
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Get workspace context from the active organization in session.
|
|
101
|
+
* Returns null if no active organization set.
|
|
102
|
+
*
|
|
103
|
+
* Use this in server components that need workspace context but can handle
|
|
104
|
+
* the absence of an active workspace (e.g., workspace switcher UI).
|
|
105
|
+
*/
|
|
106
|
+
export async function getWorkspaceContext() {
|
|
107
|
+
const authResult = await getAuthSession();
|
|
108
|
+
if (!authResult) {
|
|
109
|
+
log.debug("getWorkspaceContext: no auth session");
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
log.debug("getWorkspaceContext: session found");
|
|
113
|
+
// Get active organization from session
|
|
114
|
+
let activeOrg = await auth.api.getFullOrganization({
|
|
115
|
+
headers: await headers(),
|
|
116
|
+
});
|
|
117
|
+
log.debug("getWorkspaceContext: active org", { hasOrg: !!activeOrg });
|
|
118
|
+
// Fallback: If no active org in session, auto-select first available workspace
|
|
119
|
+
if (!activeOrg) {
|
|
120
|
+
log.debug("getWorkspaceContext: no active org, checking workspaces");
|
|
121
|
+
const orgs = await auth.api.listOrganizations({
|
|
122
|
+
headers: await headers(),
|
|
123
|
+
});
|
|
124
|
+
if (orgs && orgs.length > 0) {
|
|
125
|
+
log.debug("getWorkspaceContext: found workspaces", {
|
|
126
|
+
count: String(orgs.length),
|
|
127
|
+
});
|
|
128
|
+
// Fetch full organization details for the first workspace
|
|
129
|
+
activeOrg = await auth.api.getFullOrganization({
|
|
130
|
+
headers: await headers(),
|
|
131
|
+
query: { organizationId: orgs[0].id },
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (!activeOrg) {
|
|
136
|
+
log.debug("getWorkspaceContext: no workspaces found");
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
// Find user's membership in the active org
|
|
140
|
+
const activeMember = activeOrg.members.find((m) => m.userId === authResult.user.id);
|
|
141
|
+
log.debug("getWorkspaceContext: membership check", {
|
|
142
|
+
hasMembership: !!activeMember,
|
|
143
|
+
});
|
|
144
|
+
if (!activeMember) {
|
|
145
|
+
log.debug("getWorkspaceContext: user not a member of active org");
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
session: authResult.session,
|
|
150
|
+
user: authResult.user,
|
|
151
|
+
workspace: {
|
|
152
|
+
id: activeOrg.id,
|
|
153
|
+
name: activeOrg.name,
|
|
154
|
+
slug: activeOrg.slug,
|
|
155
|
+
logo: activeOrg.logo,
|
|
156
|
+
},
|
|
157
|
+
membership: {
|
|
158
|
+
id: activeMember.id,
|
|
159
|
+
role: activeMember.role,
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Require workspace context. Use in server actions that need workspace scope (TECH-10).
|
|
165
|
+
* Throws if not authenticated OR no active workspace selected.
|
|
166
|
+
*
|
|
167
|
+
* Use this in server actions that operate on workspace-scoped resources
|
|
168
|
+
* (conversations, knowledge bases, usage logs, etc.).
|
|
169
|
+
*
|
|
170
|
+
* @throws Error with "No active workspace" message if no workspace selected
|
|
171
|
+
*/
|
|
172
|
+
export async function requireWorkspace() {
|
|
173
|
+
const context = await getWorkspaceContext();
|
|
174
|
+
if (!context) {
|
|
175
|
+
throw new Error("No active workspace");
|
|
176
|
+
}
|
|
177
|
+
return context;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Require specific role in active workspace.
|
|
181
|
+
* Use for admin/owner-only server actions (TEAM-08).
|
|
182
|
+
*
|
|
183
|
+
* Example: requireRole(["owner", "admin"]) for workspace settings actions.
|
|
184
|
+
*
|
|
185
|
+
* @param allowedRoles - Array of role names that are permitted
|
|
186
|
+
* @throws Error with "Insufficient permissions" message if user lacks required role
|
|
187
|
+
*/
|
|
188
|
+
export async function requireRole(allowedRoles) {
|
|
189
|
+
const context = await requireWorkspace();
|
|
190
|
+
if (!allowedRoles.includes(context.membership.role)) {
|
|
191
|
+
throw new Error("Insufficient permissions");
|
|
192
|
+
}
|
|
193
|
+
return context;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Require PLATFORM admin — distinct from workspace roles.
|
|
197
|
+
*
|
|
198
|
+
* Workspace `owner` is a per-tenant role: any user who creates a
|
|
199
|
+
* workspace owns it. Platform-level surfaces (cross-workspace
|
|
200
|
+
* analytics, the operational console, impersonation) must never be
|
|
201
|
+
* gated on it.
|
|
202
|
+
*
|
|
203
|
+
* The authority is `users.role`. PLATFORM_ADMIN_EMAILS is the
|
|
204
|
+
* bootstrap: an allowlisted user is promoted into the column the first
|
|
205
|
+
* time they pass through here, so a fresh deployment has a way in and
|
|
206
|
+
* every later check — including Better-Auth's admin plugin, which can
|
|
207
|
+
* only read the row — agrees with this one. Two gates that can
|
|
208
|
+
* disagree is how the cross-tenant analytics leak happened in the
|
|
209
|
+
* first place.
|
|
210
|
+
*
|
|
211
|
+
* Closed by default: no allowlist and no role means no admin.
|
|
212
|
+
*
|
|
213
|
+
* @throws Error("Insufficient permissions") when the user is not a
|
|
214
|
+
* platform admin.
|
|
215
|
+
*/
|
|
216
|
+
export async function requirePlatformAdmin() {
|
|
217
|
+
const { session, user } = await requireAuth();
|
|
218
|
+
const allowlist = (process.env.PLATFORM_ADMIN_EMAILS ?? "")
|
|
219
|
+
.split(",")
|
|
220
|
+
.map((e) => e.trim().toLowerCase())
|
|
221
|
+
.filter(Boolean);
|
|
222
|
+
const email = user.email?.toLowerCase();
|
|
223
|
+
const allowlisted = !!email && allowlist.includes(email);
|
|
224
|
+
const roles = (user.role ?? "")
|
|
225
|
+
.split(",")
|
|
226
|
+
.map((r) => r.trim());
|
|
227
|
+
const hasRole = roles.includes(PLATFORM_ADMIN_ROLE);
|
|
228
|
+
if (!allowlisted && !hasRole) {
|
|
229
|
+
throw new Error("Insufficient permissions");
|
|
230
|
+
}
|
|
231
|
+
// Promote on first use so the row, not the environment, is what
|
|
232
|
+
// every other check reads. A failure here must not lock an admin out
|
|
233
|
+
// mid-incident — they are already authorized by the allowlist.
|
|
234
|
+
if (allowlisted && !hasRole) {
|
|
235
|
+
const next = [...roles.filter(Boolean), PLATFORM_ADMIN_ROLE].join(",");
|
|
236
|
+
try {
|
|
237
|
+
await db.update(users).set({ role: next }).where(eq(users.id, user.id));
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
log.error("Failed to promote allowlisted platform admin", {
|
|
241
|
+
userId: user.id,
|
|
242
|
+
error: error instanceof Error ? error.message : String(error),
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return { session, user };
|
|
247
|
+
}
|
|
248
|
+
//# sourceMappingURL=helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAChC,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,EAAE,EAAE,MAAM,wBAAwB,CAAC;AAC5C,OAAO,EAAE,KAAK,EAAE,MAAM,+BAA+B,CAAC;AACtD,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAEjC,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAK9C,MAAM,GAAG,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;AAEjC;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc;IAIlC,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC;QACxC,OAAO,EAAE,MAAM,OAAO,EAAE;KACzB,CAAC,CAAC;IAEH,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC;QACnB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO;QACL,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,IAAI,EAAE,OA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IAAI,CAAC,OAAO,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;IAC9C,CAAC;IAED,gEAAgE;IAChE,qEAAqE;IACrE,+DAA+D;IAC/D,IAAI,WAAW,IAAI,CAAC,OAAO,EAAE,CAAC;QAC5B,MAAM,IAAI,GAAG,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,mBAAmB,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACvE,IAAI,CAAC;YACH,MAAM,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;QAC1E,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,GAAG,CAAC,KAAK,CAAC,8CAA8C,EAAE;gBACxD,MAAM,EAAE,IAAI,CAAC,EAAE;gBACf,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;aAC9D,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;AAC3B,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import "server-only";
|
|
2
|
+
/**
|
|
3
|
+
* Session-level impersonation.
|
|
4
|
+
*
|
|
5
|
+
* This module owns only the session mechanics, because that is what
|
|
6
|
+
* @intelligo-dev/auth owns: it holds the Better-Auth instance and the
|
|
7
|
+
* request headers. The *policy* — who may do it, that it is audited or
|
|
8
|
+
* refused, that a reason is mandatory — lives in @intelligo-dev/admin, and
|
|
9
|
+
* these functions must not be called without going through it.
|
|
10
|
+
*
|
|
11
|
+
* Nothing here re-checks authorization. Splitting the check from the
|
|
12
|
+
* act would give two places that can disagree about who is an admin,
|
|
13
|
+
* which is the mistake that put cross-tenant analytics behind a
|
|
14
|
+
* workspace role.
|
|
15
|
+
*/
|
|
16
|
+
import { headers } from "next/headers";
|
|
17
|
+
import { auth } from "./server";
|
|
18
|
+
/** Default matching `impersonationSessionDuration` in server.ts. */
|
|
19
|
+
const FALLBACK_DURATION_MS = 30 * 60000;
|
|
20
|
+
/**
|
|
21
|
+
* Swap the caller's session cookie for one belonging to `targetUserId`.
|
|
22
|
+
*
|
|
23
|
+
* Better-Auth refuses if the target is themselves a platform admin.
|
|
24
|
+
*/
|
|
25
|
+
export async function impersonateUser(targetUserId) {
|
|
26
|
+
const result = (await auth.api.impersonateUser({
|
|
27
|
+
body: { userId: targetUserId },
|
|
28
|
+
headers: await headers(),
|
|
29
|
+
}));
|
|
30
|
+
const expiresAt = result.session?.expiresAt;
|
|
31
|
+
return {
|
|
32
|
+
targetUserId,
|
|
33
|
+
expiresAt: expiresAt
|
|
34
|
+
? new Date(expiresAt)
|
|
35
|
+
: new Date(Date.now() + FALLBACK_DURATION_MS),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** Restore the admin's own session. */
|
|
39
|
+
export async function stopImpersonating() {
|
|
40
|
+
await auth.api.stopImpersonating({ headers: await headers() });
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=impersonation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"impersonation.js","sourceRoot":"","sources":["../src/impersonation.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,CAAC;AAErB;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAQhC,oEAAoE;AACpE,MAAM,oBAAoB,GAAG,EAAE,GAAG,KAAM,CAAC;AAEzC;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,YAAoB;IAEpB,MAAM,MAAM,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC;QAC7C,IAAI,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE;QAC9B,OAAO,EAAE,MAAM,OAAO,EAAE;KACzB,CAAC,CAAgD,CAAC;IAEnD,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,EAAE,SAAS,CAAC;IAE5C,OAAO;QACL,YAAY;QACZ,SAAS,EAAE,SAAS;YAClB,CAAC,CAAC,IAAI,IAAI,CAAC,SAAS,CAAC;YACrB,CAAC,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,oBAAoB,CAAC;KAChD,CAAC;AACJ,CAAC;AAED,uCAAuC;AACvC,MAAM,CAAC,KAAK,UAAU,iBAAiB;IACrC,MAAM,IAAI,CAAC,GAAG,CAAC,iBAAiB,CAAC,EAAE,OAAO,EAAE,MAAM,OAAO,EAAE,EAAE,CAAC,CAAC;AACjE,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Better-Auth Server Exports
|
|
3
|
+
*
|
|
4
|
+
* Server-side authentication utilities.
|
|
5
|
+
* For client-side auth, use @intelligo-dev/auth/client
|
|
6
|
+
*/
|
|
7
|
+
export { auth } from "./server";
|
|
8
|
+
export { getAuthSession, requireAuth, getWorkspaceContext, getWorkspaceContextById, requireWorkspace, requireRole, requirePlatformAdmin, } from "./helpers";
|
|
9
|
+
export { ensureUserWorkspace } from "./workspace-init";
|
|
10
|
+
export { PLATFORM_ADMIN_ROLE } from "./roles";
|
|
11
|
+
export { impersonateUser, stopImpersonating, } from "./impersonation";
|
|
12
|
+
// Typed Better-Auth organization plugin wrapper
|
|
13
|
+
export { orgApi, } from "./org-api";
|
|
14
|
+
// Team management service (page/registry migration, section C1)
|
|
15
|
+
export { createTeamService, } from "./team/service";
|
|
16
|
+
export { TeamServiceError, isTeamServiceError, } from "./team/errors";
|
|
17
|
+
export { inviteMemberSchema, updateRoleSchema, } from "./team/schemas";
|
|
18
|
+
// Workspace management service (page/registry migration, workspace-settings family)
|
|
19
|
+
export { createWorkspaceService, } from "./workspace/service";
|
|
20
|
+
export { WorkspaceServiceError, isWorkspaceServiceError, } from "./workspace/errors";
|
|
21
|
+
export { createWorkspaceSchema, updateWorkspaceSchema, } from "./workspace/schemas";
|
|
22
|
+
// Profile management service (page/registry migration, profile-settings family)
|
|
23
|
+
export { createProfileService, } from "./profile/service";
|
|
24
|
+
export { ProfileServiceError, isProfileServiceError, } from "./profile/errors";
|
|
25
|
+
export { updateProfileSchema, } from "./profile/schemas";
|
|
26
|
+
// Onboarding service (page/registry migration, `onboarding` family)
|
|
27
|
+
export { createOnboardingService, } from "./onboarding/service";
|
|
28
|
+
export { OnboardingServiceError, isOnboardingServiceError, } from "./onboarding/errors";
|
|
29
|
+
export { setStepSchema } from "./onboarding/schemas";
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAChC,OAAO,EACL,cAAc,EACd,WAAW,EACX,mBAAmB,EACnB,uBAAuB,EACvB,gBAAgB,EAChB,WAAW,EACX,oBAAoB,GACrB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAC9C,OAAO,EACL,eAAe,EACf,iBAAiB,GAElB,MAAM,iBAAiB,CAAC;AAKzB,gDAAgD;AAChD,OAAO,EACL,MAAM,GAOP,MAAM,WAAW,CAAC;AAEnB,gEAAgE;AAChE,OAAO,EACL,iBAAiB,GAGlB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,gBAAgB,EAChB,kBAAkB,GAGnB,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,kBAAkB,EAClB,gBAAgB,GAGjB,MAAM,gBAAgB,CAAC;AAExB,oFAAoF;AACpF,OAAO,EACL,sBAAsB,GAIvB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,qBAAqB,EACrB,uBAAuB,GAGxB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,qBAAqB,EACrB,qBAAqB,GAGtB,MAAM,qBAAqB,CAAC;AAE7B,gFAAgF;AAChF,OAAO,EACL,oBAAoB,GAIrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,mBAAmB,EACnB,qBAAqB,GAGtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,mBAAmB,GAEpB,MAAM,mBAAmB,CAAC;AAE3B,oEAAoE;AACpE,OAAO,EACL,uBAAuB,GAGxB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACL,sBAAsB,EACtB,wBAAwB,GAEzB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAqB,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Onboarding service error type.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors `../team/errors.ts` and `../workspace/errors.ts`: the
|
|
5
|
+
* onboarding service (./service.ts) throws this for every failure it
|
|
6
|
+
* recognizes rather than returning an ad-hoc `{ success, error }`
|
|
7
|
+
* envelope — that shaping is a transport concern (a Server Action, a
|
|
8
|
+
* route handler) and belongs one layer up, alongside
|
|
9
|
+
* revalidatePath/Sentry/toast/i18n, none of which this package may
|
|
10
|
+
* depend on.
|
|
11
|
+
*/
|
|
12
|
+
export class OnboardingServiceError extends Error {
|
|
13
|
+
constructor(code, message, options) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "OnboardingServiceError";
|
|
16
|
+
this.code = code;
|
|
17
|
+
if (options?.cause !== undefined) {
|
|
18
|
+
// ES2020 target predates the standard `cause` constructor option;
|
|
19
|
+
// assign it directly so `instanceof Error` consumers (and Node's
|
|
20
|
+
// own error inspection) still see it.
|
|
21
|
+
this.cause = options.cause;
|
|
22
|
+
}
|
|
23
|
+
// Restore prototype chain (extending built-ins across some
|
|
24
|
+
// transpilation targets loses `instanceof`).
|
|
25
|
+
Object.setPrototypeOf(this, OnboardingServiceError.prototype);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function isOnboardingServiceError(error) {
|
|
29
|
+
return error instanceof OnboardingServiceError;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/onboarding/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAaH,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IAG/C,YACE,IAAgC,EAChC,OAAe,EACf,OAA6B;QAE7B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;QACrC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,OAAO,EAAE,KAAK,KAAK,SAAS,EAAE,CAAC;YACjC,kEAAkE;YAClE,iEAAiE;YACjE,sCAAsC;YACrC,IAA4B,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QACtD,CAAC;QAED,2DAA2D;QAC3D,6CAA6C;QAC7C,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,sBAAsB,CAAC,SAAS,CAAC,CAAC;IAChE,CAAC;CACF;AAED,MAAM,UAAU,wBAAwB,CACtC,KAAc;IAEd,OAAO,KAAK,YAAY,sBAAsB,CAAC;AACjD,CAAC"}
|