@petrarca/sonnet-shell 0.1.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.md +190 -0
- package/dist/auth/index.d.ts +117 -0
- package/dist/auth/index.js +305 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/index.d.ts +597 -0
- package/dist/index.js +1194 -0
- package/dist/index.js.map +1 -0
- package/package.json +51 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
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
|
+
Copyright 2025 Petrarca Labs (Wolfgang Miller)
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* TenantSelection -- shown after authentication when the user has
|
|
5
|
+
* multiple tenants and no default is set. The user must pick a tenant
|
|
6
|
+
* before the app loads.
|
|
7
|
+
*
|
|
8
|
+
* Fully props-driven with no app-specific imports.
|
|
9
|
+
* Target package: @petrarca/sonnet-shell
|
|
10
|
+
*/
|
|
11
|
+
/** Minimal tenant descriptor. Consumers map their domain type to this shape. */
|
|
12
|
+
interface TenantOption {
|
|
13
|
+
id: string;
|
|
14
|
+
name: string;
|
|
15
|
+
}
|
|
16
|
+
interface TenantSelectionProps {
|
|
17
|
+
tenants: TenantOption[];
|
|
18
|
+
isLoading: boolean;
|
|
19
|
+
error: string | null;
|
|
20
|
+
onSelect: (id: string) => void;
|
|
21
|
+
}
|
|
22
|
+
declare function TenantSelection({ tenants, isLoading, error, onSelect, }: TenantSelectionProps): React.ReactElement;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Login -- multi-step authentication flow.
|
|
26
|
+
*
|
|
27
|
+
* Step 1: Credentials (local auth form or OIDC redirect)
|
|
28
|
+
* Step 2: Tenant selection (if user has multiple tenants and no default)
|
|
29
|
+
*
|
|
30
|
+
* Steps are shown conditionally. If the user has a default tenant,
|
|
31
|
+
* step 2 is skipped and the app loads immediately.
|
|
32
|
+
*
|
|
33
|
+
* ProtectedRoute passes a `step` hint in location.state so this
|
|
34
|
+
* component always opens on the correct step when redirected.
|
|
35
|
+
*
|
|
36
|
+
* All auth state and operations are injected via props -- no direct
|
|
37
|
+
* store or context imports.
|
|
38
|
+
* Target package: @petrarca/sonnet-shell
|
|
39
|
+
*/
|
|
40
|
+
/** Auth feature flags and provider configuration. Consumer-supplied. */
|
|
41
|
+
interface AuthConfig {
|
|
42
|
+
enabled: boolean;
|
|
43
|
+
features: {
|
|
44
|
+
local_auth: boolean;
|
|
45
|
+
oauth: boolean;
|
|
46
|
+
};
|
|
47
|
+
oauth?: {
|
|
48
|
+
providers: Record<string, {
|
|
49
|
+
enabled: boolean;
|
|
50
|
+
name: string;
|
|
51
|
+
login_url: string;
|
|
52
|
+
}>;
|
|
53
|
+
} | null;
|
|
54
|
+
ui: {
|
|
55
|
+
direct_redirect: string | null;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
interface LoginProps {
|
|
59
|
+
/** Auth feature flags and OAuth provider config. */
|
|
60
|
+
config: AuthConfig | null;
|
|
61
|
+
/** Whether the user is currently authenticated (token present). */
|
|
62
|
+
isAuthenticated: boolean;
|
|
63
|
+
/** Active tenant id, null when not yet selected. */
|
|
64
|
+
activeTenantId: string | null;
|
|
65
|
+
/** Whether an async operation is in flight. */
|
|
66
|
+
isLoading: boolean;
|
|
67
|
+
/** Auth error message to display, null when none. */
|
|
68
|
+
error: string | null;
|
|
69
|
+
/** The authenticated user's available tenants. */
|
|
70
|
+
tenants: TenantOption[] | null;
|
|
71
|
+
/** Perform credential login. Throws on failure. */
|
|
72
|
+
onLogin: (credentials: {
|
|
73
|
+
identifier: string;
|
|
74
|
+
password: string;
|
|
75
|
+
}) => Promise<void>;
|
|
76
|
+
/** Fetch the current user after login to resolve tenant state. */
|
|
77
|
+
onFetchCurrentUser: () => Promise<void>;
|
|
78
|
+
/** Switch to the given tenant. Throws on failure. */
|
|
79
|
+
onSwitchTenant: (id: string) => Promise<void>;
|
|
80
|
+
/** Clear the current auth error. */
|
|
81
|
+
onClearError: () => void;
|
|
82
|
+
}
|
|
83
|
+
declare function Login(props: LoginProps): React.ReactElement | null;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* ProtectedRoute -- guards the app shell.
|
|
87
|
+
*
|
|
88
|
+
* On first mount, calls the injected `initializeAuth` callback so
|
|
89
|
+
* the consumer can validate or restore a persisted session. While
|
|
90
|
+
* initialization is in progress a loading spinner is shown.
|
|
91
|
+
*
|
|
92
|
+
* Once initialization is complete, guards based on:
|
|
93
|
+
* - Auth disabled (`authEnabled` false): allow all access
|
|
94
|
+
* - Not authenticated: redirect to /login with step=credentials
|
|
95
|
+
* - Authenticated but no tenant: redirect to /login with step=tenant-selection
|
|
96
|
+
*
|
|
97
|
+
* All auth state is injected via props -- no direct store or context imports.
|
|
98
|
+
* Target package: @petrarca/sonnet-shell
|
|
99
|
+
*/
|
|
100
|
+
interface ProtectedRouteProps {
|
|
101
|
+
children: ReactNode;
|
|
102
|
+
/** Whether auth is globally enabled. When false, all access is allowed. */
|
|
103
|
+
authEnabled: boolean;
|
|
104
|
+
/** Whether the auth config is still loading. */
|
|
105
|
+
authConfigLoading: boolean;
|
|
106
|
+
/** Whether the user holds a valid session. */
|
|
107
|
+
isAuthenticated: boolean;
|
|
108
|
+
/** The current access token (null when not authenticated). */
|
|
109
|
+
accessToken: string | null;
|
|
110
|
+
/** The active tenant id (null when not yet selected). */
|
|
111
|
+
activeTenantId: string | null;
|
|
112
|
+
/** Called once on mount to restore and validate a persisted session. */
|
|
113
|
+
initializeAuth: () => Promise<void>;
|
|
114
|
+
}
|
|
115
|
+
declare function ProtectedRoute({ children, authEnabled, authConfigLoading, isAuthenticated, accessToken, activeTenantId, initializeAuth, }: ProtectedRouteProps): React.ReactElement;
|
|
116
|
+
|
|
117
|
+
export { type AuthConfig, Login, type LoginProps, ProtectedRoute, type ProtectedRouteProps, type TenantOption, TenantSelection };
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
// src/auth/Login.tsx
|
|
2
|
+
import { useState as useState2, useEffect, useCallback as useCallback2 } from "react";
|
|
3
|
+
import { useNavigate, useLocation } from "react-router-dom";
|
|
4
|
+
import { Button as Button2 } from "@petrarca/sonnet-ui";
|
|
5
|
+
import { Input } from "@petrarca/sonnet-ui";
|
|
6
|
+
import { Label } from "@petrarca/sonnet-ui";
|
|
7
|
+
import { Alert as Alert2 } from "@petrarca/sonnet-ui";
|
|
8
|
+
|
|
9
|
+
// src/auth/TenantSelection.tsx
|
|
10
|
+
import { useState, useCallback } from "react";
|
|
11
|
+
import { Check } from "lucide-react";
|
|
12
|
+
import { Button } from "@petrarca/sonnet-ui";
|
|
13
|
+
import { Alert } from "@petrarca/sonnet-ui";
|
|
14
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
15
|
+
function TenantSelection({
|
|
16
|
+
tenants,
|
|
17
|
+
isLoading,
|
|
18
|
+
error,
|
|
19
|
+
onSelect
|
|
20
|
+
}) {
|
|
21
|
+
const [selectedId, setSelectedId] = useState(null);
|
|
22
|
+
const handleContinue = useCallback(() => {
|
|
23
|
+
if (selectedId) {
|
|
24
|
+
onSelect(selectedId);
|
|
25
|
+
}
|
|
26
|
+
}, [selectedId, onSelect]);
|
|
27
|
+
return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center min-h-screen bg-slate-100", children: /* @__PURE__ */ jsxs("div", { className: "w-full max-w-md bg-white rounded-lg shadow-lg p-8", children: [
|
|
28
|
+
/* @__PURE__ */ jsx("h1", { className: "text-center mb-2", children: "Select Tenant" }),
|
|
29
|
+
/* @__PURE__ */ jsx("p", { className: "text-center text-sm text-muted-foreground mb-6", children: "Choose the tenant you want to work in." }),
|
|
30
|
+
error && /* @__PURE__ */ jsx(Alert, { className: "mb-4 border-red-200 bg-red-50 text-red-700", children: /* @__PURE__ */ jsx("p", { className: "text-sm", children: error }) }),
|
|
31
|
+
/* @__PURE__ */ jsx("div", { className: "space-y-2 mb-6 max-h-64 overflow-y-auto", children: tenants.map((tenant) => /* @__PURE__ */ jsxs(
|
|
32
|
+
"button",
|
|
33
|
+
{
|
|
34
|
+
onClick: () => setSelectedId(tenant.id),
|
|
35
|
+
className: `w-full flex items-center justify-between px-4 py-3 rounded-md border text-left text-sm transition-colors ${selectedId === tenant.id ? "border-blue-500 bg-blue-50 text-blue-900" : "border-slate-200 hover:bg-slate-50"}`,
|
|
36
|
+
children: [
|
|
37
|
+
/* @__PURE__ */ jsx("span", { children: tenant.name }),
|
|
38
|
+
selectedId === tenant.id && /* @__PURE__ */ jsx(Check, { className: "h-4 w-4 text-blue-500" })
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
tenant.id
|
|
42
|
+
)) }),
|
|
43
|
+
/* @__PURE__ */ jsx(
|
|
44
|
+
Button,
|
|
45
|
+
{
|
|
46
|
+
onClick: handleContinue,
|
|
47
|
+
disabled: !selectedId || isLoading,
|
|
48
|
+
className: "w-full",
|
|
49
|
+
children: isLoading ? "Loading..." : "Continue"
|
|
50
|
+
}
|
|
51
|
+
)
|
|
52
|
+
] }) });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/auth/Login.tsx
|
|
56
|
+
import { normalizeError } from "@petrarca/sonnet-core";
|
|
57
|
+
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
58
|
+
function useLoginFlow(props) {
|
|
59
|
+
const {
|
|
60
|
+
config,
|
|
61
|
+
isAuthenticated,
|
|
62
|
+
activeTenantId,
|
|
63
|
+
tenants,
|
|
64
|
+
onLogin,
|
|
65
|
+
onFetchCurrentUser,
|
|
66
|
+
onSwitchTenant,
|
|
67
|
+
onClearError
|
|
68
|
+
} = props;
|
|
69
|
+
const navigate = useNavigate();
|
|
70
|
+
const location = useLocation();
|
|
71
|
+
const locationState = location.state;
|
|
72
|
+
const [redirectTo] = useState2(() => locationState?.from?.pathname ?? "/");
|
|
73
|
+
const [step, setStep] = useState2(
|
|
74
|
+
() => locationState?.step ?? "credentials"
|
|
75
|
+
);
|
|
76
|
+
const [identifier, setIdentifier] = useState2("");
|
|
77
|
+
const [password, setPassword] = useState2("");
|
|
78
|
+
const [tenantError, setTenantError] = useState2(null);
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
if (isAuthenticated && activeTenantId)
|
|
81
|
+
navigate(redirectTo, { replace: true });
|
|
82
|
+
}, [isAuthenticated, activeTenantId, navigate, redirectTo]);
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
if (!isAuthenticated || !tenants || activeTenantId) return;
|
|
85
|
+
if (tenants.length === 1) {
|
|
86
|
+
onSwitchTenant(tenants[0].id).catch((err) => {
|
|
87
|
+
setTenantError(
|
|
88
|
+
normalizeError(err).message || "Failed to select tenant"
|
|
89
|
+
);
|
|
90
|
+
setStep("tenant-selection");
|
|
91
|
+
});
|
|
92
|
+
} else if (tenants.length > 1) {
|
|
93
|
+
setStep("tenant-selection");
|
|
94
|
+
}
|
|
95
|
+
}, [isAuthenticated, tenants, activeTenantId, onSwitchTenant]);
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
if (config?.ui.direct_redirect)
|
|
98
|
+
window.location.href = config.ui.direct_redirect;
|
|
99
|
+
}, [config]);
|
|
100
|
+
const handleCredentialSubmit = useCallback2(
|
|
101
|
+
async (e) => {
|
|
102
|
+
e.preventDefault();
|
|
103
|
+
onClearError();
|
|
104
|
+
try {
|
|
105
|
+
await onLogin({ identifier, password });
|
|
106
|
+
await onFetchCurrentUser();
|
|
107
|
+
} catch {
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
[identifier, password, onLogin, onFetchCurrentUser, onClearError]
|
|
111
|
+
);
|
|
112
|
+
const handleTenantSelect = useCallback2(
|
|
113
|
+
async (id) => {
|
|
114
|
+
setTenantError(null);
|
|
115
|
+
try {
|
|
116
|
+
await onSwitchTenant(id);
|
|
117
|
+
} catch (err) {
|
|
118
|
+
setTenantError(
|
|
119
|
+
normalizeError(err).message || "Failed to select tenant"
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
[onSwitchTenant]
|
|
124
|
+
);
|
|
125
|
+
return {
|
|
126
|
+
step,
|
|
127
|
+
identifier,
|
|
128
|
+
setIdentifier,
|
|
129
|
+
password,
|
|
130
|
+
setPassword,
|
|
131
|
+
tenantError,
|
|
132
|
+
handleCredentialSubmit,
|
|
133
|
+
handleTenantSelect
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function Login(props) {
|
|
137
|
+
const { config, isLoading, error, tenants } = props;
|
|
138
|
+
const flow = useLoginFlow(props);
|
|
139
|
+
if (config?.ui.direct_redirect) {
|
|
140
|
+
return /* @__PURE__ */ jsx2(RedirectingMessage, {});
|
|
141
|
+
}
|
|
142
|
+
if (flow.step === "tenant-selection" && tenants) {
|
|
143
|
+
return /* @__PURE__ */ jsx2(
|
|
144
|
+
TenantSelection,
|
|
145
|
+
{
|
|
146
|
+
tenants,
|
|
147
|
+
isLoading,
|
|
148
|
+
error: flow.tenantError,
|
|
149
|
+
onSelect: flow.handleTenantSelect
|
|
150
|
+
}
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
return /* @__PURE__ */ jsx2(
|
|
154
|
+
CredentialPage,
|
|
155
|
+
{
|
|
156
|
+
config,
|
|
157
|
+
error,
|
|
158
|
+
isLoading,
|
|
159
|
+
flow
|
|
160
|
+
}
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
function RedirectingMessage() {
|
|
164
|
+
return /* @__PURE__ */ jsx2("div", { className: "flex items-center justify-center min-h-screen", children: /* @__PURE__ */ jsx2("p", { className: "text-muted-foreground", children: "Redirecting to authentication..." }) });
|
|
165
|
+
}
|
|
166
|
+
function CredentialPage({
|
|
167
|
+
config,
|
|
168
|
+
error,
|
|
169
|
+
isLoading,
|
|
170
|
+
flow
|
|
171
|
+
}) {
|
|
172
|
+
return /* @__PURE__ */ jsx2("div", { className: "flex items-center justify-center min-h-screen bg-slate-100", children: /* @__PURE__ */ jsxs2("div", { className: "w-full max-w-md bg-white rounded-lg shadow-lg p-8", children: [
|
|
173
|
+
/* @__PURE__ */ jsx2("h1", { className: "text-center mb-6", children: "Sign In" }),
|
|
174
|
+
error && /* @__PURE__ */ jsx2(Alert2, { className: "mb-4 border-red-200 bg-red-50 text-red-700", children: /* @__PURE__ */ jsx2("p", { className: "text-sm", children: error }) }),
|
|
175
|
+
config?.features.local_auth && /* @__PURE__ */ jsx2(
|
|
176
|
+
CredentialForm,
|
|
177
|
+
{
|
|
178
|
+
identifier: flow.identifier,
|
|
179
|
+
password: flow.password,
|
|
180
|
+
isLoading,
|
|
181
|
+
onIdentifierChange: flow.setIdentifier,
|
|
182
|
+
onPasswordChange: flow.setPassword,
|
|
183
|
+
onSubmit: flow.handleCredentialSubmit
|
|
184
|
+
}
|
|
185
|
+
),
|
|
186
|
+
config?.features.oauth && config.oauth?.providers && /* @__PURE__ */ jsx2(OAuthProviders, { providers: config.oauth.providers })
|
|
187
|
+
] }) });
|
|
188
|
+
}
|
|
189
|
+
function CredentialForm({
|
|
190
|
+
identifier,
|
|
191
|
+
password,
|
|
192
|
+
isLoading,
|
|
193
|
+
onIdentifierChange,
|
|
194
|
+
onPasswordChange,
|
|
195
|
+
onSubmit
|
|
196
|
+
}) {
|
|
197
|
+
return /* @__PURE__ */ jsxs2("form", { onSubmit, className: "space-y-4", children: [
|
|
198
|
+
/* @__PURE__ */ jsxs2("div", { className: "space-y-2", children: [
|
|
199
|
+
/* @__PURE__ */ jsx2(Label, { htmlFor: "identifier", children: "Email or Username" }),
|
|
200
|
+
/* @__PURE__ */ jsx2(
|
|
201
|
+
Input,
|
|
202
|
+
{
|
|
203
|
+
id: "identifier",
|
|
204
|
+
type: "text",
|
|
205
|
+
value: identifier,
|
|
206
|
+
onChange: (e) => onIdentifierChange(e.target.value),
|
|
207
|
+
required: true,
|
|
208
|
+
placeholder: "Enter your email or username"
|
|
209
|
+
}
|
|
210
|
+
)
|
|
211
|
+
] }),
|
|
212
|
+
/* @__PURE__ */ jsxs2("div", { className: "space-y-2", children: [
|
|
213
|
+
/* @__PURE__ */ jsx2(Label, { htmlFor: "password", children: "Password" }),
|
|
214
|
+
/* @__PURE__ */ jsx2(
|
|
215
|
+
Input,
|
|
216
|
+
{
|
|
217
|
+
id: "password",
|
|
218
|
+
type: "password",
|
|
219
|
+
value: password,
|
|
220
|
+
onChange: (e) => onPasswordChange(e.target.value),
|
|
221
|
+
required: true,
|
|
222
|
+
placeholder: "Enter your password"
|
|
223
|
+
}
|
|
224
|
+
)
|
|
225
|
+
] }),
|
|
226
|
+
/* @__PURE__ */ jsx2(Button2, { type: "submit", disabled: isLoading, className: "w-full", children: isLoading ? "Signing in..." : "Sign In" })
|
|
227
|
+
] });
|
|
228
|
+
}
|
|
229
|
+
function OAuthProviders({ providers }) {
|
|
230
|
+
return /* @__PURE__ */ jsxs2("div", { className: "mt-6", children: [
|
|
231
|
+
/* @__PURE__ */ jsxs2("div", { className: "relative mb-4", children: [
|
|
232
|
+
/* @__PURE__ */ jsx2("div", { className: "absolute inset-0 flex items-center", children: /* @__PURE__ */ jsx2("div", { className: "w-full border-t border-slate-300" }) }),
|
|
233
|
+
/* @__PURE__ */ jsx2("div", { className: "relative flex justify-center text-sm", children: /* @__PURE__ */ jsx2("span", { className: "px-2 bg-white text-muted-foreground", children: "Or continue with" }) })
|
|
234
|
+
] }),
|
|
235
|
+
/* @__PURE__ */ jsx2("div", { className: "space-y-2", children: Object.entries(providers).map(
|
|
236
|
+
([key, provider]) => provider.enabled ? /* @__PURE__ */ jsxs2(
|
|
237
|
+
"a",
|
|
238
|
+
{
|
|
239
|
+
href: provider.login_url,
|
|
240
|
+
className: "w-full flex items-center justify-center py-2 px-4 border border-slate-300 rounded-md hover:bg-slate-50",
|
|
241
|
+
children: [
|
|
242
|
+
"Continue with ",
|
|
243
|
+
provider.name
|
|
244
|
+
]
|
|
245
|
+
},
|
|
246
|
+
key
|
|
247
|
+
) : null
|
|
248
|
+
) })
|
|
249
|
+
] });
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// src/auth/ProtectedRoute.tsx
|
|
253
|
+
import { Navigate, useLocation as useLocation2 } from "react-router-dom";
|
|
254
|
+
import { useEffect as useEffect2, useRef, useState as useState3 } from "react";
|
|
255
|
+
import { Spinner } from "@petrarca/sonnet-ui";
|
|
256
|
+
import { Fragment, jsx as jsx3 } from "react/jsx-runtime";
|
|
257
|
+
function ProtectedRoute({
|
|
258
|
+
children,
|
|
259
|
+
authEnabled,
|
|
260
|
+
authConfigLoading,
|
|
261
|
+
isAuthenticated,
|
|
262
|
+
accessToken,
|
|
263
|
+
activeTenantId,
|
|
264
|
+
initializeAuth
|
|
265
|
+
}) {
|
|
266
|
+
const location = useLocation2();
|
|
267
|
+
const [initializing, setInitializing] = useState3(true);
|
|
268
|
+
const initRef = useRef(initializeAuth);
|
|
269
|
+
useEffect2(() => {
|
|
270
|
+
initRef.current().finally(() => setInitializing(false));
|
|
271
|
+
}, []);
|
|
272
|
+
if (authConfigLoading || initializing) {
|
|
273
|
+
return /* @__PURE__ */ jsx3("div", { className: "flex items-center justify-center min-h-screen", children: /* @__PURE__ */ jsx3(Spinner, { size: "lg" }) });
|
|
274
|
+
}
|
|
275
|
+
if (!authEnabled) {
|
|
276
|
+
return /* @__PURE__ */ jsx3(Fragment, { children });
|
|
277
|
+
}
|
|
278
|
+
if (!isAuthenticated || !accessToken) {
|
|
279
|
+
return /* @__PURE__ */ jsx3(
|
|
280
|
+
Navigate,
|
|
281
|
+
{
|
|
282
|
+
to: "/login",
|
|
283
|
+
state: { from: location, step: "credentials" },
|
|
284
|
+
replace: true
|
|
285
|
+
}
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
if (!activeTenantId) {
|
|
289
|
+
return /* @__PURE__ */ jsx3(
|
|
290
|
+
Navigate,
|
|
291
|
+
{
|
|
292
|
+
to: "/login",
|
|
293
|
+
state: { from: location, step: "tenant-selection" },
|
|
294
|
+
replace: true
|
|
295
|
+
}
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
return /* @__PURE__ */ jsx3(Fragment, { children });
|
|
299
|
+
}
|
|
300
|
+
export {
|
|
301
|
+
Login,
|
|
302
|
+
ProtectedRoute,
|
|
303
|
+
TenantSelection
|
|
304
|
+
};
|
|
305
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/auth/Login.tsx","../../src/auth/TenantSelection.tsx","../../src/auth/ProtectedRoute.tsx"],"sourcesContent":["import { useState, useEffect, useCallback } from \"react\";\nimport { useNavigate, useLocation } from \"react-router-dom\";\nimport { Button } from \"@petrarca/sonnet-ui\";\nimport { Input } from \"@petrarca/sonnet-ui\";\nimport { Label } from \"@petrarca/sonnet-ui\";\nimport { Alert } from \"@petrarca/sonnet-ui\";\nimport { TenantSelection, type TenantOption } from \"./TenantSelection\";\nimport { normalizeError } from \"@petrarca/sonnet-core\";\n\n/**\n * Login -- multi-step authentication flow.\n *\n * Step 1: Credentials (local auth form or OIDC redirect)\n * Step 2: Tenant selection (if user has multiple tenants and no default)\n *\n * Steps are shown conditionally. If the user has a default tenant,\n * step 2 is skipped and the app loads immediately.\n *\n * ProtectedRoute passes a `step` hint in location.state so this\n * component always opens on the correct step when redirected.\n *\n * All auth state and operations are injected via props -- no direct\n * store or context imports.\n * Target package: @petrarca/sonnet-shell\n */\n\n/** Auth feature flags and provider configuration. Consumer-supplied. */\nexport interface AuthConfig {\n enabled: boolean;\n features: {\n local_auth: boolean;\n oauth: boolean;\n };\n oauth?: {\n providers: Record<\n string,\n {\n enabled: boolean;\n name: string;\n login_url: string;\n }\n >;\n } | null;\n ui: {\n direct_redirect: string | null;\n };\n}\n\ntype LoginStep = \"credentials\" | \"tenant-selection\";\n\ninterface LocationState {\n from?: { pathname: string };\n step?: LoginStep;\n}\n\nexport interface LoginProps {\n /** Auth feature flags and OAuth provider config. */\n config: AuthConfig | null;\n /** Whether the user is currently authenticated (token present). */\n isAuthenticated: boolean;\n /** Active tenant id, null when not yet selected. */\n activeTenantId: string | null;\n /** Whether an async operation is in flight. */\n isLoading: boolean;\n /** Auth error message to display, null when none. */\n error: string | null;\n /** The authenticated user's available tenants. */\n tenants: TenantOption[] | null;\n /** Perform credential login. Throws on failure. */\n onLogin: (credentials: {\n identifier: string;\n password: string;\n }) => Promise<void>;\n /** Fetch the current user after login to resolve tenant state. */\n onFetchCurrentUser: () => Promise<void>;\n /** Switch to the given tenant. Throws on failure. */\n onSwitchTenant: (id: string) => Promise<void>;\n /** Clear the current auth error. */\n onClearError: () => void;\n}\n\n// Custom hook: manages the login flow state (steps, redirects, tenant resolution).\nfunction useLoginFlow(props: LoginProps) {\n const {\n config,\n isAuthenticated,\n activeTenantId,\n tenants,\n onLogin,\n onFetchCurrentUser,\n onSwitchTenant,\n onClearError,\n } = props;\n const navigate = useNavigate();\n const location = useLocation();\n const locationState = location.state as LocationState | null;\n\n const [redirectTo] = useState(() => locationState?.from?.pathname ?? \"/\");\n const [step, setStep] = useState<LoginStep>(\n () => locationState?.step ?? \"credentials\",\n );\n const [identifier, setIdentifier] = useState(\"\");\n const [password, setPassword] = useState(\"\");\n const [tenantError, setTenantError] = useState<string | null>(null);\n\n useEffect(() => {\n if (isAuthenticated && activeTenantId)\n navigate(redirectTo, { replace: true });\n }, [isAuthenticated, activeTenantId, navigate, redirectTo]);\n\n useEffect(() => {\n if (!isAuthenticated || !tenants || activeTenantId) return;\n if (tenants.length === 1) {\n onSwitchTenant(tenants[0].id).catch((err) => {\n setTenantError(\n normalizeError(err).message || \"Failed to select tenant\",\n );\n setStep(\"tenant-selection\");\n });\n } else if (tenants.length > 1) {\n setStep(\"tenant-selection\");\n }\n }, [isAuthenticated, tenants, activeTenantId, onSwitchTenant]);\n\n useEffect(() => {\n if (config?.ui.direct_redirect)\n window.location.href = config.ui.direct_redirect;\n }, [config]);\n\n const handleCredentialSubmit = useCallback(\n async (e: React.FormEvent) => {\n e.preventDefault();\n onClearError();\n try {\n await onLogin({ identifier, password });\n await onFetchCurrentUser();\n } catch {\n /* error via prop */\n }\n },\n [identifier, password, onLogin, onFetchCurrentUser, onClearError],\n );\n\n const handleTenantSelect = useCallback(\n async (id: string) => {\n setTenantError(null);\n try {\n await onSwitchTenant(id);\n } catch (err) {\n setTenantError(\n normalizeError(err).message || \"Failed to select tenant\",\n );\n }\n },\n [onSwitchTenant],\n );\n\n return {\n step,\n identifier,\n setIdentifier,\n password,\n setPassword,\n tenantError,\n handleCredentialSubmit,\n handleTenantSelect,\n };\n}\n\nexport function Login(props: LoginProps): React.ReactElement | null {\n const { config, isLoading, error, tenants } = props;\n const flow = useLoginFlow(props);\n\n if (config?.ui.direct_redirect) {\n return <RedirectingMessage />;\n }\n\n if (flow.step === \"tenant-selection\" && tenants) {\n return (\n <TenantSelection\n tenants={tenants}\n isLoading={isLoading}\n error={flow.tenantError}\n onSelect={flow.handleTenantSelect}\n />\n );\n }\n\n return (\n <CredentialPage\n config={config}\n error={error}\n isLoading={isLoading}\n flow={flow}\n />\n );\n}\n\nfunction RedirectingMessage() {\n return (\n <div className=\"flex items-center justify-center min-h-screen\">\n <p className=\"text-muted-foreground\">Redirecting to authentication...</p>\n </div>\n );\n}\n\ninterface CredentialPageProps {\n config: AuthConfig | null;\n error: string | null;\n isLoading: boolean;\n flow: ReturnType<typeof useLoginFlow>;\n}\n\nfunction CredentialPage({\n config,\n error,\n isLoading,\n flow,\n}: CredentialPageProps) {\n return (\n <div className=\"flex items-center justify-center min-h-screen bg-slate-100\">\n <div className=\"w-full max-w-md bg-white rounded-lg shadow-lg p-8\">\n <h1 className=\"text-center mb-6\">Sign In</h1>\n\n {error && (\n <Alert className=\"mb-4 border-red-200 bg-red-50 text-red-700\">\n <p className=\"text-sm\">{error}</p>\n </Alert>\n )}\n\n {config?.features.local_auth && (\n <CredentialForm\n identifier={flow.identifier}\n password={flow.password}\n isLoading={isLoading}\n onIdentifierChange={flow.setIdentifier}\n onPasswordChange={flow.setPassword}\n onSubmit={flow.handleCredentialSubmit}\n />\n )}\n\n {config?.features.oauth && config.oauth?.providers && (\n <OAuthProviders providers={config.oauth.providers} />\n )}\n </div>\n </div>\n );\n}\n\n// -- Sub-components extracted to reduce Login complexity --\n\ninterface CredentialFormProps {\n identifier: string;\n password: string;\n isLoading: boolean;\n onIdentifierChange: (value: string) => void;\n onPasswordChange: (value: string) => void;\n onSubmit: (e: React.FormEvent) => void;\n}\n\nfunction CredentialForm({\n identifier,\n password,\n isLoading,\n onIdentifierChange,\n onPasswordChange,\n onSubmit,\n}: CredentialFormProps) {\n return (\n <form onSubmit={onSubmit} className=\"space-y-4\">\n <div className=\"space-y-2\">\n <Label htmlFor=\"identifier\">Email or Username</Label>\n <Input\n id=\"identifier\"\n type=\"text\"\n value={identifier}\n onChange={(e) => onIdentifierChange(e.target.value)}\n required\n placeholder=\"Enter your email or username\"\n />\n </div>\n\n <div className=\"space-y-2\">\n <Label htmlFor=\"password\">Password</Label>\n <Input\n id=\"password\"\n type=\"password\"\n value={password}\n onChange={(e) => onPasswordChange(e.target.value)}\n required\n placeholder=\"Enter your password\"\n />\n </div>\n\n <Button type=\"submit\" disabled={isLoading} className=\"w-full\">\n {isLoading ? \"Signing in...\" : \"Sign In\"}\n </Button>\n </form>\n );\n}\n\ninterface OAuthProvidersProps {\n providers: Record<\n string,\n { enabled: boolean; name: string; login_url: string }\n >;\n}\n\nfunction OAuthProviders({ providers }: OAuthProvidersProps) {\n return (\n <div className=\"mt-6\">\n <div className=\"relative mb-4\">\n <div className=\"absolute inset-0 flex items-center\">\n <div className=\"w-full border-t border-slate-300\"></div>\n </div>\n <div className=\"relative flex justify-center text-sm\">\n <span className=\"px-2 bg-white text-muted-foreground\">\n Or continue with\n </span>\n </div>\n </div>\n\n <div className=\"space-y-2\">\n {Object.entries(providers).map(([key, provider]) =>\n provider.enabled ? (\n <a\n key={key}\n href={provider.login_url}\n className=\"w-full flex items-center justify-center py-2 px-4 border border-slate-300 rounded-md hover:bg-slate-50\"\n >\n Continue with {provider.name}\n </a>\n ) : null,\n )}\n </div>\n </div>\n );\n}\n","import { useState, useCallback } from \"react\";\nimport { Check } from \"lucide-react\";\nimport { Button } from \"@petrarca/sonnet-ui\";\nimport { Alert } from \"@petrarca/sonnet-ui\";\n\n/**\n * TenantSelection -- shown after authentication when the user has\n * multiple tenants and no default is set. The user must pick a tenant\n * before the app loads.\n *\n * Fully props-driven with no app-specific imports.\n * Target package: @petrarca/sonnet-shell\n */\n\n/** Minimal tenant descriptor. Consumers map their domain type to this shape. */\nexport interface TenantOption {\n id: string;\n name: string;\n}\n\ninterface TenantSelectionProps {\n tenants: TenantOption[];\n isLoading: boolean;\n error: string | null;\n onSelect: (id: string) => void;\n}\n\nexport function TenantSelection({\n tenants,\n isLoading,\n error,\n onSelect,\n}: TenantSelectionProps): React.ReactElement {\n const [selectedId, setSelectedId] = useState<string | null>(null);\n\n const handleContinue = useCallback(() => {\n if (selectedId) {\n onSelect(selectedId);\n }\n }, [selectedId, onSelect]);\n\n return (\n <div className=\"flex items-center justify-center min-h-screen bg-slate-100\">\n <div className=\"w-full max-w-md bg-white rounded-lg shadow-lg p-8\">\n <h1 className=\"text-center mb-2\">Select Tenant</h1>\n <p className=\"text-center text-sm text-muted-foreground mb-6\">\n Choose the tenant you want to work in.\n </p>\n\n {error && (\n <Alert className=\"mb-4 border-red-200 bg-red-50 text-red-700\">\n <p className=\"text-sm\">{error}</p>\n </Alert>\n )}\n\n <div className=\"space-y-2 mb-6 max-h-64 overflow-y-auto\">\n {tenants.map((tenant) => (\n <button\n key={tenant.id}\n onClick={() => setSelectedId(tenant.id)}\n className={`w-full flex items-center justify-between px-4 py-3 rounded-md border text-left text-sm transition-colors ${\n selectedId === tenant.id\n ? \"border-blue-500 bg-blue-50 text-blue-900\"\n : \"border-slate-200 hover:bg-slate-50\"\n }`}\n >\n <span>{tenant.name}</span>\n {selectedId === tenant.id && (\n <Check className=\"h-4 w-4 text-blue-500\" />\n )}\n </button>\n ))}\n </div>\n\n <Button\n onClick={handleContinue}\n disabled={!selectedId || isLoading}\n className=\"w-full\"\n >\n {isLoading ? \"Loading...\" : \"Continue\"}\n </Button>\n </div>\n </div>\n );\n}\n","import { Navigate, useLocation } from \"react-router-dom\";\nimport { type ReactNode, useEffect, useRef, useState } from \"react\";\nimport { Spinner } from \"@petrarca/sonnet-ui\";\n\n/**\n * ProtectedRoute -- guards the app shell.\n *\n * On first mount, calls the injected `initializeAuth` callback so\n * the consumer can validate or restore a persisted session. While\n * initialization is in progress a loading spinner is shown.\n *\n * Once initialization is complete, guards based on:\n * - Auth disabled (`authEnabled` false): allow all access\n * - Not authenticated: redirect to /login with step=credentials\n * - Authenticated but no tenant: redirect to /login with step=tenant-selection\n *\n * All auth state is injected via props -- no direct store or context imports.\n * Target package: @petrarca/sonnet-shell\n */\n\nexport interface ProtectedRouteProps {\n children: ReactNode;\n /** Whether auth is globally enabled. When false, all access is allowed. */\n authEnabled: boolean;\n /** Whether the auth config is still loading. */\n authConfigLoading: boolean;\n /** Whether the user holds a valid session. */\n isAuthenticated: boolean;\n /** The current access token (null when not authenticated). */\n accessToken: string | null;\n /** The active tenant id (null when not yet selected). */\n activeTenantId: string | null;\n /** Called once on mount to restore and validate a persisted session. */\n initializeAuth: () => Promise<void>;\n}\n\nexport function ProtectedRoute({\n children,\n authEnabled,\n authConfigLoading,\n isAuthenticated,\n accessToken,\n activeTenantId,\n initializeAuth,\n}: ProtectedRouteProps): React.ReactElement {\n const location = useLocation();\n const [initializing, setInitializing] = useState(true);\n const initRef = useRef(initializeAuth);\n\n useEffect(() => {\n initRef.current().finally(() => setInitializing(false));\n }, []);\n\n if (authConfigLoading || initializing) {\n return (\n <div className=\"flex items-center justify-center min-h-screen\">\n <Spinner size=\"lg\" />\n </div>\n );\n }\n\n // Auth disabled: allow all access\n if (!authEnabled) {\n return <>{children}</>;\n }\n\n // Not authenticated: go to credentials step\n if (!isAuthenticated || !accessToken) {\n return (\n <Navigate\n to=\"/login\"\n state={{ from: location, step: \"credentials\" }}\n replace\n />\n );\n }\n\n // Authenticated but no tenant: go to tenant selection step\n if (!activeTenantId) {\n return (\n <Navigate\n to=\"/login\"\n state={{ from: location, step: \"tenant-selection\" }}\n replace\n />\n );\n }\n\n return <>{children}</>;\n}\n"],"mappings":";AAAA,SAAS,YAAAA,WAAU,WAAW,eAAAC,oBAAmB;AACjD,SAAS,aAAa,mBAAmB;AACzC,SAAS,UAAAC,eAAc;AACvB,SAAS,aAAa;AACtB,SAAS,aAAa;AACtB,SAAS,SAAAC,cAAa;;;ACLtB,SAAS,UAAU,mBAAmB;AACtC,SAAS,aAAa;AACtB,SAAS,cAAc;AACvB,SAAS,aAAa;AAyCd,cAaI,YAbJ;AAjBD,SAAS,gBAAgB;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAA6C;AAC3C,QAAM,CAAC,YAAY,aAAa,IAAI,SAAwB,IAAI;AAEhE,QAAM,iBAAiB,YAAY,MAAM;AACvC,QAAI,YAAY;AACd,eAAS,UAAU;AAAA,IACrB;AAAA,EACF,GAAG,CAAC,YAAY,QAAQ,CAAC;AAEzB,SACE,oBAAC,SAAI,WAAU,8DACb,+BAAC,SAAI,WAAU,qDACb;AAAA,wBAAC,QAAG,WAAU,oBAAmB,2BAAa;AAAA,IAC9C,oBAAC,OAAE,WAAU,kDAAiD,oDAE9D;AAAA,IAEC,SACC,oBAAC,SAAM,WAAU,8CACf,8BAAC,OAAE,WAAU,WAAW,iBAAM,GAChC;AAAA,IAGF,oBAAC,SAAI,WAAU,2CACZ,kBAAQ,IAAI,CAAC,WACZ;AAAA,MAAC;AAAA;AAAA,QAEC,SAAS,MAAM,cAAc,OAAO,EAAE;AAAA,QACtC,WAAW,4GACT,eAAe,OAAO,KAClB,6CACA,oCACN;AAAA,QAEA;AAAA,8BAAC,UAAM,iBAAO,MAAK;AAAA,UAClB,eAAe,OAAO,MACrB,oBAAC,SAAM,WAAU,yBAAwB;AAAA;AAAA;AAAA,MAVtC,OAAO;AAAA,IAYd,CACD,GACH;AAAA,IAEA;AAAA,MAAC;AAAA;AAAA,QACC,SAAS;A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,IAAIE,UAAS,IAAI;AACrD,QAAM,UAAU,OAAO,cAAc;AAErC,EAAAD,WAAU,MAAM;AACd,YAAQ,QAAQ,EAAE,QAAQ,MAAM,gBAAgB,KAAK,CAAC;AAAA,EACxD,GAAG,CAAC,CAAC;AAEL,MAAI,qBAAqB,cAAc;AACrC,WACE,gBAAAE,KAAC,SAAI,WAAU,iDACb,0BAAAA,KAAC,WAAQ,MAAK,MAAK,GACrB;AAAA,EAEJ;AAGA,MAAI,CAAC,aAAa;AAChB,WAAO,gBAAAA,KAAA,YAAG,UAAS;AAAA,EACrB;AAGA,MAAI,CAAC,mBAAmB,CAAC,aAAa;AACpC,WACE,gBAAAA;AAAA,MAAC;AAAA;AAAA,QACC,IAAG;AAAA,QACH,OAAO,EAAE,MAAM,UAAU,MAAM,cAAc;AAAA,QAC7C,SAAO;AAAA;AAAA,IACT;AAAA,EAEJ;AAGA,MAAI,CAAC,gBAAgB;AACnB,WACE,gBAAAA;AAAA,MAAC;AAAA;AAAA,QACC,IAAG;AAAA,QACH,OAAO,EAAE,MAAM,UAAU,MAAM,mBAAmB;AAAA,QAClD,SAAO;AAAA;AAAA,IACT;AAAA,EAEJ;AAEA,SAAO,gBAAAA,KAAA,YAAG,UAAS;AACrB;","names":["useState","useCallback","Button","Alert","jsx","jsxs","useState","useCallback","Alert","Button","useLocation","useEffect","useState","jsx"]}
|