@smartcrab/react 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 +202 -0
- package/dist/context.d.ts +20 -0
- package/dist/context.js +12 -0
- package/dist/headless.d.ts +73 -0
- package/dist/headless.js +72 -0
- package/dist/hooks.d.ts +108 -0
- package/dist/hooks.js +143 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +26 -0
- package/dist/provider.d.ts +15 -0
- package/dist/provider.js +16 -0
- package/dist/sessions-controller.d.ts +33 -0
- package/dist/sessions-controller.js +73 -0
- package/dist/sign-in-flow.d.ts +91 -0
- package/dist/sign-in-flow.js +193 -0
- package/dist/user-controller.d.ts +24 -0
- package/dist/user-controller.js +67 -0
- package/package.json +46 -0
- package/src/context.ts +32 -0
- package/src/headless.tsx +172 -0
- package/src/hooks.ts +267 -0
- package/src/index.test.ts +60 -0
- package/src/index.ts +79 -0
- package/src/provider.tsx +35 -0
- package/src/sessions-controller.test.ts +107 -0
- package/src/sessions-controller.ts +120 -0
- package/src/sign-in-flow.test.ts +271 -0
- package/src/sign-in-flow.ts +341 -0
- package/src/user-controller.test.ts +112 -0
- package/src/user-controller.ts +106 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { BrowserAuthClient } from "@smartcrab/browser";
|
|
2
|
+
import type { SignInFlow } from "./sign-in-flow.js";
|
|
3
|
+
import type { SessionListController } from "./sessions-controller.js";
|
|
4
|
+
import type { UserProfileController } from "./user-controller.js";
|
|
5
|
+
/**
|
|
6
|
+
* Dependency context for the web React SDK (design.md §3.1, §21.4).
|
|
7
|
+
*
|
|
8
|
+
* One `AuthProvider` per client; all hooks/headless components consume this.
|
|
9
|
+
* The controllers are created once per client by the provider so that hook
|
|
10
|
+
* state survives re-renders but is rebuilt on client swap.
|
|
11
|
+
*/
|
|
12
|
+
export interface AuthContextValue {
|
|
13
|
+
readonly client: BrowserAuthClient;
|
|
14
|
+
readonly flow: SignInFlow;
|
|
15
|
+
readonly userProfile: UserProfileController;
|
|
16
|
+
readonly sessionList: SessionListController;
|
|
17
|
+
}
|
|
18
|
+
export declare const AuthContext: import("react").Context<AuthContextValue | null>;
|
|
19
|
+
/** Internal: every public hook resolves its dependencies through here. */
|
|
20
|
+
export declare const useAuthContextValue: () => AuthContextValue;
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createContext, useContext } from "react";
|
|
2
|
+
export const AuthContext = createContext(null);
|
|
3
|
+
/** Internal: every public hook resolves its dependencies through here. */
|
|
4
|
+
export const useAuthContextValue = () => {
|
|
5
|
+
const value = useContext(AuthContext);
|
|
6
|
+
if (value === null) {
|
|
7
|
+
// Programmer error (missing <AuthProvider>), surfaced loudly — the React
|
|
8
|
+
// convention for required-provider hooks.
|
|
9
|
+
throw new Error("Auth hooks must be used inside <AuthProvider client={...}>");
|
|
10
|
+
}
|
|
11
|
+
return value;
|
|
12
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { AuthenticatedSession, BrowserAuthError, SocialProvider } from "@smartcrab/browser";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* Headless UI components for the web (design.md §3.1: "React | Hooks、
|
|
5
|
+
* Provider、UIコンポーネント").
|
|
6
|
+
*
|
|
7
|
+
* These are the behavior-only counterparts of `@smartcrab/ui-web`'s
|
|
8
|
+
* styled hosted-login kit: they render NOTHING themselves — no DOM, no
|
|
9
|
+
* styling, no theme tokens. Customers compose fully custom UIs by passing a
|
|
10
|
+
* render function as `children` and wiring the provided state/handlers to
|
|
11
|
+
* their own elements.
|
|
12
|
+
*/
|
|
13
|
+
export interface PasskeyButtonRenderProps {
|
|
14
|
+
/** Wire to your button's onClick. Runs the full ceremony through the sign-in flow. */
|
|
15
|
+
readonly onClick: () => void;
|
|
16
|
+
/** True while a ceremony is running or passkeys are unavailable — disable your button. */
|
|
17
|
+
readonly disabled: boolean;
|
|
18
|
+
/** Runtime WebAuthn capability (design.md §21.2: render nothing when false). */
|
|
19
|
+
readonly isSupported: boolean;
|
|
20
|
+
readonly pending: boolean;
|
|
21
|
+
readonly error: BrowserAuthError | null;
|
|
22
|
+
}
|
|
23
|
+
export interface PasskeyButtonProps {
|
|
24
|
+
/** Ceremony kind: `sign-in` (discoverable credential) or `register` (design.md §17). */
|
|
25
|
+
readonly mode?: "sign-in" | "register";
|
|
26
|
+
/** Optional authenticator label for `register` mode (design.md §12.5 `passkeys.name`). */
|
|
27
|
+
readonly name?: string;
|
|
28
|
+
readonly onComplete?: (session: AuthenticatedSession) => void;
|
|
29
|
+
readonly children: (props: PasskeyButtonRenderProps) => ReactNode;
|
|
30
|
+
}
|
|
31
|
+
/** Headless passkey button: ceremony wiring only, zero rendering opinions. */
|
|
32
|
+
export declare const PasskeyButton: ({ mode, name, onComplete, children, }: PasskeyButtonProps) => ReactNode;
|
|
33
|
+
export type EmailOtpFormStep = "enter_email" | "enter_code";
|
|
34
|
+
export interface EmailOtpFormRenderProps {
|
|
35
|
+
readonly step: EmailOtpFormStep;
|
|
36
|
+
readonly email: string;
|
|
37
|
+
readonly setEmail: (value: string) => void;
|
|
38
|
+
readonly code: string;
|
|
39
|
+
readonly setCode: (value: string) => void;
|
|
40
|
+
/** Sends the code for the current `email` value. */
|
|
41
|
+
readonly send: () => void;
|
|
42
|
+
readonly resend: () => void;
|
|
43
|
+
/** Verifies the current `code` value, then completes sign-in. */
|
|
44
|
+
readonly verify: () => void;
|
|
45
|
+
readonly pendingAction: "send_email" | "resend_email" | "verify_email" | null;
|
|
46
|
+
readonly error: BrowserAuthError | null;
|
|
47
|
+
/** Challenge metadata once sent (expiry drives resend cooldown UIs, design.md §18.2). */
|
|
48
|
+
readonly challengeExpiresAt: string | null;
|
|
49
|
+
}
|
|
50
|
+
export interface EmailOtpFormProps {
|
|
51
|
+
readonly onSignedIn?: (session: AuthenticatedSession) => void;
|
|
52
|
+
readonly children: (props: EmailOtpFormRenderProps) => ReactNode;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Headless email-OTP form state machine: owns the two text fields' local
|
|
56
|
+
* state and maps the sign-in flow's steps onto `enter_email`/`enter_code`.
|
|
57
|
+
* All mutation goes through the render props.
|
|
58
|
+
*/
|
|
59
|
+
export declare const EmailOtpForm: ({ onSignedIn, children }: EmailOtpFormProps) => ReactNode;
|
|
60
|
+
export interface SocialButtonsRenderProps {
|
|
61
|
+
readonly providers: readonly SocialProvider[];
|
|
62
|
+
/** Starts the top-level redirect for a provider (design.md §20, §22.3). */
|
|
63
|
+
readonly signIn: (provider: SocialProvider) => void;
|
|
64
|
+
readonly pending: boolean;
|
|
65
|
+
readonly error: BrowserAuthError | null;
|
|
66
|
+
}
|
|
67
|
+
export interface SocialButtonsProps {
|
|
68
|
+
/** Which providers to offer; typically filtered by `getPublicConfig().available_methods`. */
|
|
69
|
+
readonly providers: readonly SocialProvider[];
|
|
70
|
+
readonly children: (props: SocialButtonsRenderProps) => ReactNode;
|
|
71
|
+
}
|
|
72
|
+
/** Headless social provider list: redirect wiring only. */
|
|
73
|
+
export declare const SocialButtons: ({ providers, children }: SocialButtonsProps) => ReactNode;
|
package/dist/headless.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { useEmailOtp, usePasskey, useSocialSignIn } from "./hooks.js";
|
|
3
|
+
/** Headless passkey button: ceremony wiring only, zero rendering opinions. */
|
|
4
|
+
export const PasskeyButton = ({ mode = "sign-in", name, onComplete, children, }) => {
|
|
5
|
+
const { isSupported, isCeremonyActive, pendingAction, error, signInWithPasskey, registerPasskey, } = usePasskey();
|
|
6
|
+
const pending = isCeremonyActive || pendingAction === "passkey";
|
|
7
|
+
const onClick = () => {
|
|
8
|
+
const action = mode === "register"
|
|
9
|
+
? registerPasskey(name !== undefined ? { name } : undefined)
|
|
10
|
+
: signInWithPasskey();
|
|
11
|
+
void action.then((outcome) => {
|
|
12
|
+
if (outcome.ok)
|
|
13
|
+
onComplete?.(outcome.value);
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
return children({
|
|
17
|
+
onClick,
|
|
18
|
+
disabled: pending || !isSupported,
|
|
19
|
+
isSupported,
|
|
20
|
+
pending,
|
|
21
|
+
error,
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Headless email-OTP form state machine: owns the two text fields' local
|
|
26
|
+
* state and maps the sign-in flow's steps onto `enter_email`/`enter_code`.
|
|
27
|
+
* All mutation goes through the render props.
|
|
28
|
+
*/
|
|
29
|
+
export const EmailOtpForm = ({ onSignedIn, children }) => {
|
|
30
|
+
const { challenge, pendingAction, error, send, resend, verify } = useEmailOtp();
|
|
31
|
+
const [email, setEmail] = useState("");
|
|
32
|
+
const [code, setCode] = useState("");
|
|
33
|
+
const step = challenge === null ? "enter_email" : "enter_code";
|
|
34
|
+
return children({
|
|
35
|
+
step,
|
|
36
|
+
email,
|
|
37
|
+
setEmail,
|
|
38
|
+
code,
|
|
39
|
+
setCode,
|
|
40
|
+
send: () => {
|
|
41
|
+
void send(email);
|
|
42
|
+
},
|
|
43
|
+
resend: () => {
|
|
44
|
+
void resend();
|
|
45
|
+
},
|
|
46
|
+
verify: () => {
|
|
47
|
+
void verify(code).then((outcome) => {
|
|
48
|
+
if (outcome.ok)
|
|
49
|
+
onSignedIn?.(outcome.value);
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
pendingAction: pendingAction === "send_email" ||
|
|
53
|
+
pendingAction === "resend_email" ||
|
|
54
|
+
pendingAction === "verify_email"
|
|
55
|
+
? pendingAction
|
|
56
|
+
: null,
|
|
57
|
+
error,
|
|
58
|
+
challengeExpiresAt: challenge?.expiresAt ?? null,
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
/** Headless social provider list: redirect wiring only. */
|
|
62
|
+
export const SocialButtons = ({ providers, children }) => {
|
|
63
|
+
const { signIn, pending, error } = useSocialSignIn();
|
|
64
|
+
return children({
|
|
65
|
+
providers,
|
|
66
|
+
signIn: (provider) => {
|
|
67
|
+
void signIn(provider);
|
|
68
|
+
},
|
|
69
|
+
pending,
|
|
70
|
+
error,
|
|
71
|
+
});
|
|
72
|
+
};
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { AuthenticatedSession, BrowserAuthClient, BrowserAuthError, BrowserAuthSnapshot, MeSessionSummary, MeUpdateRequest, MeUser, Outcome, SocialProvider } from "@smartcrab/browser";
|
|
2
|
+
import type { PendingAction, SignInFlow, SignInFlowState } from "./sign-in-flow.js";
|
|
3
|
+
import type { SignInFlowError } from "./sign-in-flow.js";
|
|
4
|
+
import type { SessionListStatus } from "./sessions-controller.js";
|
|
5
|
+
import type { UserProfileStatus } from "./user-controller.js";
|
|
6
|
+
/** Raw client escape hatch (advanced flows: hosted redirect, config, custom calls). */
|
|
7
|
+
export declare const useAuthClient: () => BrowserAuthClient;
|
|
8
|
+
/** Current auth snapshot (signed-in status, expiry metadata — never raw tokens). */
|
|
9
|
+
export declare const useAuthState: () => BrowserAuthSnapshot;
|
|
10
|
+
export interface UseAuthResult {
|
|
11
|
+
readonly snapshot: BrowserAuthSnapshot;
|
|
12
|
+
readonly isSignedIn: boolean;
|
|
13
|
+
/** Headless sign-in flow state (transaction step machine, design.md §16). */
|
|
14
|
+
readonly flowState: SignInFlowState;
|
|
15
|
+
readonly pendingAction: PendingAction;
|
|
16
|
+
readonly flowError: BrowserAuthError | null;
|
|
17
|
+
readonly begin: SignInFlow["begin"];
|
|
18
|
+
readonly sendEmailCode: SignInFlow["sendEmailCode"];
|
|
19
|
+
readonly resendEmailCode: SignInFlow["resendEmailCode"];
|
|
20
|
+
readonly verifyEmailCode: SignInFlow["verifyEmailCode"];
|
|
21
|
+
readonly signInWithPasskey: SignInFlow["signInWithPasskey"];
|
|
22
|
+
readonly registerPasskey: SignInFlow["registerPasskey"];
|
|
23
|
+
readonly signInWithSocial: SignInFlow["signInWithSocial"];
|
|
24
|
+
readonly cancelSignIn: SignInFlow["cancel"];
|
|
25
|
+
readonly resetSignIn: SignInFlow["reset"];
|
|
26
|
+
readonly getAccessToken: BrowserAuthClient["getAccessToken"];
|
|
27
|
+
readonly refreshSession: BrowserAuthClient["refreshSession"];
|
|
28
|
+
readonly restoreSession: BrowserAuthClient["restoreSession"];
|
|
29
|
+
readonly signOut: BrowserAuthClient["signOut"];
|
|
30
|
+
/** Hosted redirect resume: pass the full callback URL after an authorize/social round-trip. */
|
|
31
|
+
readonly handleRedirectCallback: BrowserAuthClient["handleHostedAuthorizationCallback"];
|
|
32
|
+
}
|
|
33
|
+
/** Main facade (design.md §21.4 `useAuth`). */
|
|
34
|
+
export declare const useAuth: () => UseAuthResult;
|
|
35
|
+
export interface UseUserResult {
|
|
36
|
+
readonly status: UserProfileStatus;
|
|
37
|
+
readonly user: MeUser | null;
|
|
38
|
+
readonly error: BrowserAuthError | null;
|
|
39
|
+
readonly reload: () => Promise<Outcome<MeUser, BrowserAuthError>>;
|
|
40
|
+
readonly update: (patch: MeUpdateRequest) => Promise<Outcome<MeUser, BrowserAuthError>>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Signed-in user's profile (design.md §21.4 `useUser`). Auto-loads when the
|
|
44
|
+
* client transitions to `signed_in` and clears on sign-out.
|
|
45
|
+
*/
|
|
46
|
+
export declare const useUser: () => UseUserResult;
|
|
47
|
+
export interface UseSessionResult {
|
|
48
|
+
readonly snapshot: BrowserAuthSnapshot;
|
|
49
|
+
readonly status: SessionListStatus;
|
|
50
|
+
readonly sessions: readonly MeSessionSummary[] | null;
|
|
51
|
+
readonly error: BrowserAuthError | null;
|
|
52
|
+
readonly reload: () => Promise<Outcome<readonly MeSessionSummary[], BrowserAuthError>>;
|
|
53
|
+
readonly revoke: (sessionId: string) => Promise<Outcome<{
|
|
54
|
+
revoked: true;
|
|
55
|
+
}, BrowserAuthError>>;
|
|
56
|
+
readonly revokeAll: () => Promise<Outcome<{
|
|
57
|
+
revoked: true;
|
|
58
|
+
}, BrowserAuthError>>;
|
|
59
|
+
readonly refreshSession: BrowserAuthClient["refreshSession"];
|
|
60
|
+
readonly signOut: BrowserAuthClient["signOut"];
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Auth state plus the device-session list (design.md §21.4 `useSession`,
|
|
64
|
+
* §24). Auto-loads the list while signed in.
|
|
65
|
+
*/
|
|
66
|
+
export declare const useSession: () => UseSessionResult;
|
|
67
|
+
export interface UsePasskeyResult {
|
|
68
|
+
/** Runtime capability (design.md §21.2: hide passkey UI when false). */
|
|
69
|
+
readonly isSupported: boolean;
|
|
70
|
+
readonly isCeremonyActive: boolean;
|
|
71
|
+
readonly pendingAction: PendingAction;
|
|
72
|
+
readonly error: BrowserAuthError | null;
|
|
73
|
+
readonly signInWithPasskey: () => Promise<Outcome<AuthenticatedSession, SignInFlowError>>;
|
|
74
|
+
readonly registerPasskey: (input?: {
|
|
75
|
+
readonly name?: string;
|
|
76
|
+
}) => Promise<Outcome<AuthenticatedSession, SignInFlowError>>;
|
|
77
|
+
}
|
|
78
|
+
/** Passkey ceremonies (design.md §21.4 `usePasskey`, §17). */
|
|
79
|
+
export declare const usePasskey: () => UsePasskeyResult;
|
|
80
|
+
export interface EmailOtpChallenge {
|
|
81
|
+
readonly challengeId: string;
|
|
82
|
+
readonly email: string;
|
|
83
|
+
readonly expiresAt: string;
|
|
84
|
+
}
|
|
85
|
+
export interface UseEmailOtpResult {
|
|
86
|
+
/** Present once a code has been sent (design.md §18). */
|
|
87
|
+
readonly challenge: EmailOtpChallenge | null;
|
|
88
|
+
readonly pendingAction: PendingAction;
|
|
89
|
+
readonly error: BrowserAuthError | null;
|
|
90
|
+
readonly send: (email: string) => Promise<Outcome<{
|
|
91
|
+
challengeId: string;
|
|
92
|
+
}, SignInFlowError>>;
|
|
93
|
+
readonly resend: () => Promise<Outcome<{
|
|
94
|
+
challengeId: string;
|
|
95
|
+
}, SignInFlowError>>;
|
|
96
|
+
readonly verify: (code: string) => Promise<Outcome<AuthenticatedSession, SignInFlowError>>;
|
|
97
|
+
}
|
|
98
|
+
/** Email OTP sub-flow (design.md §21.4 `useEmailOtp`, §18). */
|
|
99
|
+
export declare const useEmailOtp: () => UseEmailOtpResult;
|
|
100
|
+
export interface UseSocialSignInResult {
|
|
101
|
+
readonly signIn: (provider: SocialProvider) => Promise<Outcome<{
|
|
102
|
+
redirected: true;
|
|
103
|
+
}, SignInFlowError>>;
|
|
104
|
+
readonly pending: boolean;
|
|
105
|
+
readonly error: BrowserAuthError | null;
|
|
106
|
+
}
|
|
107
|
+
/** Social sign-in via top-level redirect (design.md §21.4 `useSocialSignIn`, §20). */
|
|
108
|
+
export declare const useSocialSignIn: () => UseSocialSignInResult;
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { useEffect, useMemo, useSyncExternalStore } from "react";
|
|
2
|
+
import { useAuthContextValue } from "./context.js";
|
|
3
|
+
/**
|
|
4
|
+
* The React hook surface (design.md §3.1 client matrix, §21.4 hook list).
|
|
5
|
+
*
|
|
6
|
+
* Every hook is a thin `useSyncExternalStore` binding over the headless
|
|
7
|
+
* controllers (sign-in flow, user profile, session list) created by
|
|
8
|
+
* `<AuthProvider>`; all behavior lives in the controllers so it stays
|
|
9
|
+
* testable without a renderer.
|
|
10
|
+
*/
|
|
11
|
+
const useStoreSnapshot = (subscribe, getSnapshot) => useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
12
|
+
/** Raw client escape hatch (advanced flows: hosted redirect, config, custom calls). */
|
|
13
|
+
export const useAuthClient = () => useAuthContextValue().client;
|
|
14
|
+
/** Current auth snapshot (signed-in status, expiry metadata — never raw tokens). */
|
|
15
|
+
export const useAuthState = () => {
|
|
16
|
+
const { client } = useAuthContextValue();
|
|
17
|
+
return useStoreSnapshot(client.subscribe, client.getSnapshot);
|
|
18
|
+
};
|
|
19
|
+
const flowError = (state) => state.step === "failed" ? state.error : null;
|
|
20
|
+
/** Main facade (design.md §21.4 `useAuth`). */
|
|
21
|
+
export const useAuth = () => {
|
|
22
|
+
const { client, flow } = useAuthContextValue();
|
|
23
|
+
const snapshot = useStoreSnapshot(client.subscribe, client.getSnapshot);
|
|
24
|
+
const flowSnapshot = useStoreSnapshot(flow.subscribe, flow.getSnapshot);
|
|
25
|
+
return useMemo(() => ({
|
|
26
|
+
snapshot,
|
|
27
|
+
isSignedIn: snapshot.status === "signed_in",
|
|
28
|
+
flowState: flowSnapshot.state,
|
|
29
|
+
pendingAction: flowSnapshot.pendingAction,
|
|
30
|
+
flowError: flowError(flowSnapshot.state),
|
|
31
|
+
begin: flow.begin,
|
|
32
|
+
sendEmailCode: flow.sendEmailCode,
|
|
33
|
+
resendEmailCode: flow.resendEmailCode,
|
|
34
|
+
verifyEmailCode: flow.verifyEmailCode,
|
|
35
|
+
signInWithPasskey: flow.signInWithPasskey,
|
|
36
|
+
registerPasskey: flow.registerPasskey,
|
|
37
|
+
signInWithSocial: flow.signInWithSocial,
|
|
38
|
+
cancelSignIn: flow.cancel,
|
|
39
|
+
resetSignIn: flow.reset,
|
|
40
|
+
getAccessToken: client.getAccessToken,
|
|
41
|
+
refreshSession: client.refreshSession,
|
|
42
|
+
restoreSession: client.restoreSession,
|
|
43
|
+
signOut: client.signOut,
|
|
44
|
+
handleRedirectCallback: client.handleHostedAuthorizationCallback,
|
|
45
|
+
}), [client, flow, snapshot, flowSnapshot]);
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Signed-in user's profile (design.md §21.4 `useUser`). Auto-loads when the
|
|
49
|
+
* client transitions to `signed_in` and clears on sign-out.
|
|
50
|
+
*/
|
|
51
|
+
export const useUser = () => {
|
|
52
|
+
const { client, userProfile } = useAuthContextValue();
|
|
53
|
+
const authSnapshot = useStoreSnapshot(client.subscribe, client.getSnapshot);
|
|
54
|
+
const profile = useStoreSnapshot(userProfile.subscribe, userProfile.getSnapshot);
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
if (authSnapshot.status === "signed_in") {
|
|
57
|
+
void userProfile.load();
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
userProfile.clear();
|
|
61
|
+
}
|
|
62
|
+
}, [authSnapshot.status, userProfile]);
|
|
63
|
+
return useMemo(() => ({
|
|
64
|
+
status: profile.status,
|
|
65
|
+
user: profile.user,
|
|
66
|
+
error: profile.error,
|
|
67
|
+
reload: userProfile.load,
|
|
68
|
+
update: userProfile.update,
|
|
69
|
+
}), [profile, userProfile]);
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Auth state plus the device-session list (design.md §21.4 `useSession`,
|
|
73
|
+
* §24). Auto-loads the list while signed in.
|
|
74
|
+
*/
|
|
75
|
+
export const useSession = () => {
|
|
76
|
+
const { client, sessionList } = useAuthContextValue();
|
|
77
|
+
const snapshot = useStoreSnapshot(client.subscribe, client.getSnapshot);
|
|
78
|
+
const list = useStoreSnapshot(sessionList.subscribe, sessionList.getSnapshot);
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
if (snapshot.status === "signed_in") {
|
|
81
|
+
void sessionList.load();
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
sessionList.clear();
|
|
85
|
+
}
|
|
86
|
+
}, [snapshot.status, sessionList]);
|
|
87
|
+
return useMemo(() => ({
|
|
88
|
+
snapshot,
|
|
89
|
+
status: list.status,
|
|
90
|
+
sessions: list.sessions,
|
|
91
|
+
error: list.error,
|
|
92
|
+
reload: sessionList.load,
|
|
93
|
+
revoke: sessionList.revoke,
|
|
94
|
+
revokeAll: sessionList.revokeAll,
|
|
95
|
+
refreshSession: client.refreshSession,
|
|
96
|
+
signOut: client.signOut,
|
|
97
|
+
}), [client, sessionList, snapshot, list]);
|
|
98
|
+
};
|
|
99
|
+
/** Passkey ceremonies (design.md §21.4 `usePasskey`, §17). */
|
|
100
|
+
export const usePasskey = () => {
|
|
101
|
+
const { client, flow } = useAuthContextValue();
|
|
102
|
+
const flowSnapshot = useStoreSnapshot(flow.subscribe, flow.getSnapshot);
|
|
103
|
+
return useMemo(() => ({
|
|
104
|
+
isSupported: client.isPasskeySupported(),
|
|
105
|
+
isCeremonyActive: flowSnapshot.state.step === "ceremony",
|
|
106
|
+
pendingAction: flowSnapshot.pendingAction,
|
|
107
|
+
error: flowError(flowSnapshot.state),
|
|
108
|
+
signInWithPasskey: flow.signInWithPasskey,
|
|
109
|
+
registerPasskey: flow.registerPasskey,
|
|
110
|
+
}), [client, flow, flowSnapshot]);
|
|
111
|
+
};
|
|
112
|
+
/** Email OTP sub-flow (design.md §21.4 `useEmailOtp`, §18). */
|
|
113
|
+
export const useEmailOtp = () => {
|
|
114
|
+
const { flow } = useAuthContextValue();
|
|
115
|
+
const flowSnapshot = useStoreSnapshot(flow.subscribe, flow.getSnapshot);
|
|
116
|
+
return useMemo(() => {
|
|
117
|
+
const state = flowSnapshot.state;
|
|
118
|
+
return {
|
|
119
|
+
challenge: state.step === "email_challenge_sent"
|
|
120
|
+
? {
|
|
121
|
+
challengeId: state.challengeId,
|
|
122
|
+
email: state.email,
|
|
123
|
+
expiresAt: state.challengeExpiresAt,
|
|
124
|
+
}
|
|
125
|
+
: null,
|
|
126
|
+
pendingAction: flowSnapshot.pendingAction,
|
|
127
|
+
error: flowError(state),
|
|
128
|
+
send: flow.sendEmailCode,
|
|
129
|
+
resend: flow.resendEmailCode,
|
|
130
|
+
verify: flow.verifyEmailCode,
|
|
131
|
+
};
|
|
132
|
+
}, [flow, flowSnapshot]);
|
|
133
|
+
};
|
|
134
|
+
/** Social sign-in via top-level redirect (design.md §21.4 `useSocialSignIn`, §20). */
|
|
135
|
+
export const useSocialSignIn = () => {
|
|
136
|
+
const { flow } = useAuthContextValue();
|
|
137
|
+
const flowSnapshot = useStoreSnapshot(flow.subscribe, flow.getSnapshot);
|
|
138
|
+
return useMemo(() => ({
|
|
139
|
+
signIn: flow.signInWithSocial,
|
|
140
|
+
pending: flowSnapshot.pendingAction === "social",
|
|
141
|
+
error: flowError(flowSnapshot.state),
|
|
142
|
+
}), [flow, flowSnapshot]);
|
|
143
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@smartcrab/react` — the web React SDK (design.md §3.1 client matrix,
|
|
3
|
+
* §21.4 hook list), implemented over `@smartcrab/browser`.
|
|
4
|
+
*
|
|
5
|
+
* - `<AuthProvider client={...}>` plus hooks: `useAuth`, `useUser`,
|
|
6
|
+
* `useSession`, `usePasskey`, `useEmailOtp`, `useSocialSignIn`.
|
|
7
|
+
* - Headless, unstyled components (`PasskeyButton`, `EmailOtpForm`,
|
|
8
|
+
* `SocialButtons`): behavior-only render-prop components for fully custom
|
|
9
|
+
* UIs — distinct from `@smartcrab/ui-web`'s styled hosted-login kit.
|
|
10
|
+
* - All behavior lives in React-free controllers (`createSignInFlow`,
|
|
11
|
+
* `createUserProfileController`, `createSessionListController`); the hooks
|
|
12
|
+
* are thin `useSyncExternalStore` bindings over them.
|
|
13
|
+
*
|
|
14
|
+
* Testing constraint (documented per assignment): `react-dom` /
|
|
15
|
+
* `react-test-renderer` are intentionally not dependencies of this package,
|
|
16
|
+
* so no renderer exists in tests. Tests exercise the controllers and store
|
|
17
|
+
* semantics directly; the hook layer is a type-checked binding over that
|
|
18
|
+
* tested logic.
|
|
19
|
+
*/
|
|
20
|
+
export { AuthProvider } from "./provider.js";
|
|
21
|
+
export type { AuthProviderProps } from "./provider.js";
|
|
22
|
+
export { AuthContext, useAuthContextValue } from "./context.js";
|
|
23
|
+
export type { AuthContextValue } from "./context.js";
|
|
24
|
+
export { useAuth, useAuthClient, useAuthState, useEmailOtp, usePasskey, useSession, useSocialSignIn, useUser, } from "./hooks.js";
|
|
25
|
+
export type { EmailOtpChallenge, UseAuthResult, UseEmailOtpResult, UsePasskeyResult, UseSessionResult, UseSocialSignInResult, UseUserResult, } from "./hooks.js";
|
|
26
|
+
export { createSignInFlow } from "./sign-in-flow.js";
|
|
27
|
+
export type { InvalidFlowStateError, PendingAction, SignInFlow, SignInFlowClient, SignInFlowError, SignInFlowListener, SignInFlowSnapshot, SignInFlowState, } from "./sign-in-flow.js";
|
|
28
|
+
export { createUserProfileController } from "./user-controller.js";
|
|
29
|
+
export type { UserProfileClient, UserProfileController, UserProfileListener, UserProfileSnapshot, UserProfileStatus, } from "./user-controller.js";
|
|
30
|
+
export { createSessionListController } from "./sessions-controller.js";
|
|
31
|
+
export type { SessionListClient, SessionListController, SessionListListener, SessionListSnapshot, SessionListStatus, } from "./sessions-controller.js";
|
|
32
|
+
export { EmailOtpForm, PasskeyButton, SocialButtons } from "./headless.js";
|
|
33
|
+
export type { EmailOtpFormProps, EmailOtpFormRenderProps, EmailOtpFormStep, PasskeyButtonProps, PasskeyButtonRenderProps, SocialButtonsProps, SocialButtonsRenderProps, } from "./headless.js";
|