@smartcrab/react 0.3.2 → 0.4.0-next-20261017141739
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/dist/headless.d.ts +7 -0
- package/dist/headless.js +9 -3
- package/dist/hooks.d.ts +9 -1
- package/dist/hooks.js +33 -15
- package/dist/index.d.ts +1 -1
- package/dist/sessions-controller.js +1 -4
- package/dist/sign-in-flow.d.ts +33 -10
- package/dist/sign-in-flow.js +138 -54
- package/dist/user-controller.js +1 -4
- package/package.json +2 -2
- package/src/headless.tsx +26 -2
- package/src/hooks.ts +47 -15
- package/src/index.ts +3 -0
- package/src/sessions-controller.ts +1 -5
- package/src/sign-in-flow.adaptive.test.ts +374 -0
- package/src/sign-in-flow.ts +241 -78
- package/src/user-controller.ts +1 -5
package/dist/headless.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { AuthenticatedSession, BrowserAuthError, SocialProvider } from "@smartcrab/browser";
|
|
2
2
|
import type { ReactNode } from "react";
|
|
3
|
+
import type { SignInFlow, SignInFlowAdaptiveChallengeState } from "./sign-in-flow.js";
|
|
3
4
|
/**
|
|
4
5
|
* Headless UI components for the web (design.md §3.1: "React | Hooks、
|
|
5
6
|
* Provider、UIコンポーネント").
|
|
@@ -19,6 +20,8 @@ export interface PasskeyButtonRenderProps {
|
|
|
19
20
|
readonly isSupported: boolean;
|
|
20
21
|
readonly pending: boolean;
|
|
21
22
|
readonly error: BrowserAuthError | null;
|
|
23
|
+
readonly adaptiveChallenge: SignInFlowAdaptiveChallengeState | null;
|
|
24
|
+
readonly retryAdaptiveChallenge: SignInFlow["retryAdaptiveChallenge"];
|
|
22
25
|
}
|
|
23
26
|
export interface PasskeyButtonProps {
|
|
24
27
|
/** Ceremony kind: `sign-in` (discoverable credential) or `register` (design.md §17). */
|
|
@@ -45,6 +48,8 @@ export interface EmailOtpFormRenderProps {
|
|
|
45
48
|
readonly pendingAction: "send_email" | "resend_email" | "verify_email" | null;
|
|
46
49
|
readonly error: BrowserAuthError | null;
|
|
47
50
|
/** Challenge metadata once sent (expiry drives resend cooldown UIs, design.md §18.2). */
|
|
51
|
+
readonly adaptiveChallenge: SignInFlowAdaptiveChallengeState | null;
|
|
52
|
+
readonly retryAdaptiveChallenge: SignInFlow["retryAdaptiveChallenge"];
|
|
48
53
|
readonly challengeExpiresAt: string | null;
|
|
49
54
|
}
|
|
50
55
|
export interface EmailOtpFormProps {
|
|
@@ -63,6 +68,8 @@ export interface SocialButtonsRenderProps {
|
|
|
63
68
|
readonly signIn: (provider: SocialProvider) => void;
|
|
64
69
|
readonly pending: boolean;
|
|
65
70
|
readonly error: BrowserAuthError | null;
|
|
71
|
+
readonly adaptiveChallenge: SignInFlowAdaptiveChallengeState | null;
|
|
72
|
+
readonly retryAdaptiveChallenge: SignInFlow["retryAdaptiveChallenge"];
|
|
66
73
|
}
|
|
67
74
|
export interface SocialButtonsProps {
|
|
68
75
|
/** Which providers to offer; typically filtered by `getPublicConfig().available_methods`. */
|
package/dist/headless.js
CHANGED
|
@@ -2,7 +2,7 @@ import { useState } from "react";
|
|
|
2
2
|
import { useEmailOtp, usePasskey, useSocialSignIn } from "./hooks.js";
|
|
3
3
|
/** Headless passkey button: ceremony wiring only, zero rendering opinions. */
|
|
4
4
|
export const PasskeyButton = ({ mode = "sign-in", name, onComplete, children, }) => {
|
|
5
|
-
const { isSupported, isCeremonyActive, pendingAction, error, signInWithPasskey, registerPasskey, } = usePasskey();
|
|
5
|
+
const { isSupported, isCeremonyActive, pendingAction, error, signInWithPasskey, registerPasskey, adaptiveChallenge, retryAdaptiveChallenge, } = usePasskey();
|
|
6
6
|
const pending = isCeremonyActive || pendingAction === "passkey";
|
|
7
7
|
const onClick = () => {
|
|
8
8
|
const action = mode === "register"
|
|
@@ -19,6 +19,8 @@ export const PasskeyButton = ({ mode = "sign-in", name, onComplete, children, })
|
|
|
19
19
|
isSupported,
|
|
20
20
|
pending,
|
|
21
21
|
error,
|
|
22
|
+
adaptiveChallenge,
|
|
23
|
+
retryAdaptiveChallenge,
|
|
22
24
|
});
|
|
23
25
|
};
|
|
24
26
|
/**
|
|
@@ -27,7 +29,7 @@ export const PasskeyButton = ({ mode = "sign-in", name, onComplete, children, })
|
|
|
27
29
|
* All mutation goes through the render props.
|
|
28
30
|
*/
|
|
29
31
|
export const EmailOtpForm = ({ onSignedIn, children }) => {
|
|
30
|
-
const { challenge, pendingAction, error, send, resend, verify } = useEmailOtp();
|
|
32
|
+
const { challenge, pendingAction, error, adaptiveChallenge, retryAdaptiveChallenge, send, resend, verify, } = useEmailOtp();
|
|
31
33
|
const [email, setEmail] = useState("");
|
|
32
34
|
const [code, setCode] = useState("");
|
|
33
35
|
const step = challenge === null ? "enter_email" : "enter_code";
|
|
@@ -55,12 +57,14 @@ export const EmailOtpForm = ({ onSignedIn, children }) => {
|
|
|
55
57
|
? pendingAction
|
|
56
58
|
: null,
|
|
57
59
|
error,
|
|
60
|
+
adaptiveChallenge,
|
|
61
|
+
retryAdaptiveChallenge,
|
|
58
62
|
challengeExpiresAt: challenge?.expiresAt ?? null,
|
|
59
63
|
});
|
|
60
64
|
};
|
|
61
65
|
/** Headless social provider list: redirect wiring only. */
|
|
62
66
|
export const SocialButtons = ({ providers, children }) => {
|
|
63
|
-
const { signIn, pending, error } = useSocialSignIn();
|
|
67
|
+
const { signIn, pending, error, adaptiveChallenge, retryAdaptiveChallenge } = useSocialSignIn();
|
|
64
68
|
return children({
|
|
65
69
|
providers,
|
|
66
70
|
signIn: (provider) => {
|
|
@@ -68,5 +72,7 @@ export const SocialButtons = ({ providers, children }) => {
|
|
|
68
72
|
},
|
|
69
73
|
pending,
|
|
70
74
|
error,
|
|
75
|
+
adaptiveChallenge,
|
|
76
|
+
retryAdaptiveChallenge,
|
|
71
77
|
});
|
|
72
78
|
};
|
package/dist/hooks.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
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";
|
|
2
|
+
import type { PendingAction, SignInFlow, SignInFlowAdaptiveChallengeState, SignInFlowState } from "./sign-in-flow.js";
|
|
3
3
|
import type { SignInFlowError } from "./sign-in-flow.js";
|
|
4
4
|
import type { SessionListStatus } from "./sessions-controller.js";
|
|
5
5
|
import type { UserProfileStatus } from "./user-controller.js";
|
|
@@ -14,6 +14,7 @@ export interface UseAuthResult {
|
|
|
14
14
|
readonly flowState: SignInFlowState;
|
|
15
15
|
readonly pendingAction: PendingAction;
|
|
16
16
|
readonly flowError: BrowserAuthError | null;
|
|
17
|
+
readonly adaptiveChallenge: SignInFlowAdaptiveChallengeState | null;
|
|
17
18
|
readonly begin: SignInFlow["begin"];
|
|
18
19
|
readonly sendEmailCode: SignInFlow["sendEmailCode"];
|
|
19
20
|
readonly resendEmailCode: SignInFlow["resendEmailCode"];
|
|
@@ -21,6 +22,7 @@ export interface UseAuthResult {
|
|
|
21
22
|
readonly signInWithPasskey: SignInFlow["signInWithPasskey"];
|
|
22
23
|
readonly registerPasskey: SignInFlow["registerPasskey"];
|
|
23
24
|
readonly signInWithSocial: SignInFlow["signInWithSocial"];
|
|
25
|
+
readonly retryAdaptiveChallenge: SignInFlow["retryAdaptiveChallenge"];
|
|
24
26
|
readonly cancelSignIn: SignInFlow["cancel"];
|
|
25
27
|
readonly resetSignIn: SignInFlow["reset"];
|
|
26
28
|
readonly getAccessToken: BrowserAuthClient["getAccessToken"];
|
|
@@ -70,6 +72,8 @@ export interface UsePasskeyResult {
|
|
|
70
72
|
readonly isCeremonyActive: boolean;
|
|
71
73
|
readonly pendingAction: PendingAction;
|
|
72
74
|
readonly error: BrowserAuthError | null;
|
|
75
|
+
readonly adaptiveChallenge: SignInFlowAdaptiveChallengeState | null;
|
|
76
|
+
readonly retryAdaptiveChallenge: SignInFlow["retryAdaptiveChallenge"];
|
|
73
77
|
readonly signInWithPasskey: () => Promise<Outcome<AuthenticatedSession, SignInFlowError>>;
|
|
74
78
|
readonly registerPasskey: (input?: {
|
|
75
79
|
readonly name?: string;
|
|
@@ -87,6 +91,8 @@ export interface UseEmailOtpResult {
|
|
|
87
91
|
readonly challenge: EmailOtpChallenge | null;
|
|
88
92
|
readonly pendingAction: PendingAction;
|
|
89
93
|
readonly error: BrowserAuthError | null;
|
|
94
|
+
readonly adaptiveChallenge: SignInFlowAdaptiveChallengeState | null;
|
|
95
|
+
readonly retryAdaptiveChallenge: SignInFlow["retryAdaptiveChallenge"];
|
|
90
96
|
readonly send: (email: string) => Promise<Outcome<{
|
|
91
97
|
challengeId: string;
|
|
92
98
|
}, SignInFlowError>>;
|
|
@@ -103,6 +109,8 @@ export interface UseSocialSignInResult {
|
|
|
103
109
|
}, SignInFlowError>>;
|
|
104
110
|
readonly pending: boolean;
|
|
105
111
|
readonly error: BrowserAuthError | null;
|
|
112
|
+
readonly adaptiveChallenge: SignInFlowAdaptiveChallengeState | null;
|
|
113
|
+
readonly retryAdaptiveChallenge: SignInFlow["retryAdaptiveChallenge"];
|
|
106
114
|
}
|
|
107
115
|
/** Social sign-in via top-level redirect (design.md §21.4 `useSocialSignIn`, §20). */
|
|
108
116
|
export declare const useSocialSignIn: () => UseSocialSignInResult;
|
package/dist/hooks.js
CHANGED
|
@@ -17,6 +17,7 @@ export const useAuthState = () => {
|
|
|
17
17
|
return useStoreSnapshot(client.subscribe, client.getSnapshot);
|
|
18
18
|
};
|
|
19
19
|
const flowError = (state) => state.step === "failed" ? state.error : null;
|
|
20
|
+
const flowAdaptiveChallenge = (state) => state.step === "challenge_required" ? state : null;
|
|
20
21
|
/** Main facade (design.md §21.4 `useAuth`). */
|
|
21
22
|
export const useAuth = () => {
|
|
22
23
|
const { client, flow } = useAuthContextValue();
|
|
@@ -28,6 +29,7 @@ export const useAuth = () => {
|
|
|
28
29
|
flowState: flowSnapshot.state,
|
|
29
30
|
pendingAction: flowSnapshot.pendingAction,
|
|
30
31
|
flowError: flowError(flowSnapshot.state),
|
|
32
|
+
adaptiveChallenge: flowAdaptiveChallenge(flowSnapshot.state),
|
|
31
33
|
begin: flow.begin,
|
|
32
34
|
sendEmailCode: flow.sendEmailCode,
|
|
33
35
|
resendEmailCode: flow.resendEmailCode,
|
|
@@ -38,6 +40,7 @@ export const useAuth = () => {
|
|
|
38
40
|
cancelSignIn: flow.cancel,
|
|
39
41
|
resetSignIn: flow.reset,
|
|
40
42
|
getAccessToken: client.getAccessToken,
|
|
43
|
+
retryAdaptiveChallenge: flow.retryAdaptiveChallenge,
|
|
41
44
|
refreshSession: client.refreshSession,
|
|
42
45
|
restoreSession: client.restoreSession,
|
|
43
46
|
signOut: client.signOut,
|
|
@@ -100,14 +103,20 @@ export const useSession = () => {
|
|
|
100
103
|
export const usePasskey = () => {
|
|
101
104
|
const { client, flow } = useAuthContextValue();
|
|
102
105
|
const flowSnapshot = useStoreSnapshot(flow.subscribe, flow.getSnapshot);
|
|
103
|
-
return useMemo(() =>
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
106
|
+
return useMemo(() => {
|
|
107
|
+
const state = flowSnapshot.state;
|
|
108
|
+
return {
|
|
109
|
+
isSupported: client.isPasskeySupported(),
|
|
110
|
+
isCeremonyActive: state.step === "ceremony" ||
|
|
111
|
+
(state.step === "challenge_required" && state.previousState.step === "ceremony"),
|
|
112
|
+
pendingAction: flowSnapshot.pendingAction,
|
|
113
|
+
error: flowError(state),
|
|
114
|
+
adaptiveChallenge: flowAdaptiveChallenge(state),
|
|
115
|
+
retryAdaptiveChallenge: flow.retryAdaptiveChallenge,
|
|
116
|
+
signInWithPasskey: flow.signInWithPasskey,
|
|
117
|
+
registerPasskey: flow.registerPasskey,
|
|
118
|
+
};
|
|
119
|
+
}, [client, flow, flowSnapshot]);
|
|
111
120
|
};
|
|
112
121
|
/** Email OTP sub-flow (design.md §21.4 `useEmailOtp`, §18). */
|
|
113
122
|
export const useEmailOtp = () => {
|
|
@@ -115,16 +124,23 @@ export const useEmailOtp = () => {
|
|
|
115
124
|
const flowSnapshot = useStoreSnapshot(flow.subscribe, flow.getSnapshot);
|
|
116
125
|
return useMemo(() => {
|
|
117
126
|
const state = flowSnapshot.state;
|
|
127
|
+
const emailState = state.step === "email_challenge_sent"
|
|
128
|
+
? state
|
|
129
|
+
: state.step === "challenge_required" && state.previousState.step === "email_challenge_sent"
|
|
130
|
+
? state.previousState
|
|
131
|
+
: null;
|
|
118
132
|
return {
|
|
119
|
-
challenge:
|
|
120
|
-
?
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
133
|
+
challenge: emailState === null
|
|
134
|
+
? null
|
|
135
|
+
: {
|
|
136
|
+
challengeId: emailState.challengeId,
|
|
137
|
+
email: emailState.email,
|
|
138
|
+
expiresAt: emailState.challengeExpiresAt,
|
|
139
|
+
},
|
|
126
140
|
pendingAction: flowSnapshot.pendingAction,
|
|
127
141
|
error: flowError(state),
|
|
142
|
+
adaptiveChallenge: flowAdaptiveChallenge(state),
|
|
143
|
+
retryAdaptiveChallenge: flow.retryAdaptiveChallenge,
|
|
128
144
|
send: flow.sendEmailCode,
|
|
129
145
|
resend: flow.resendEmailCode,
|
|
130
146
|
verify: flow.verifyEmailCode,
|
|
@@ -139,5 +155,7 @@ export const useSocialSignIn = () => {
|
|
|
139
155
|
signIn: flow.signInWithSocial,
|
|
140
156
|
pending: flowSnapshot.pendingAction === "social",
|
|
141
157
|
error: flowError(flowSnapshot.state),
|
|
158
|
+
adaptiveChallenge: flowAdaptiveChallenge(flowSnapshot.state),
|
|
159
|
+
retryAdaptiveChallenge: flow.retryAdaptiveChallenge,
|
|
142
160
|
}), [flow, flowSnapshot]);
|
|
143
161
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ export type { AuthContextValue } from "./context.js";
|
|
|
24
24
|
export { useAuth, useAuthClient, useAuthState, useEmailOtp, usePasskey, useSession, useSocialSignIn, useUser, } from "./hooks.js";
|
|
25
25
|
export type { EmailOtpChallenge, UseAuthResult, UseEmailOtpResult, UsePasskeyResult, UseSessionResult, UseSocialSignInResult, UseUserResult, } from "./hooks.js";
|
|
26
26
|
export { createSignInFlow } from "./sign-in-flow.js";
|
|
27
|
-
export type { InvalidFlowStateError, PendingAction, SignInFlow, SignInFlowClient, SignInFlowError, SignInFlowListener, SignInFlowSnapshot, SignInFlowState, } from "./sign-in-flow.js";
|
|
27
|
+
export type { InvalidFlowStateError, PendingAction, SignInFlow, SignInFlowClient, SignInFlowError, SignInFlowListener, SignInFlowSnapshot, SignInFlowState, SignInFlowAdaptiveChallengeResult, SignInFlowAdaptiveChallengeState, SignInFlowChallengeResumeState, } from "./sign-in-flow.js";
|
|
28
28
|
export { createUserProfileController } from "./user-controller.js";
|
|
29
29
|
export type { UserProfileClient, UserProfileController, UserProfileListener, UserProfileSnapshot, UserProfileStatus, } from "./user-controller.js";
|
|
30
30
|
export { createSessionListController } from "./sessions-controller.js";
|
|
@@ -56,9 +56,6 @@ export const createSessionListController = (client) => {
|
|
|
56
56
|
transition(IDLE);
|
|
57
57
|
return ok({ revoked: true });
|
|
58
58
|
};
|
|
59
|
-
const clear = () => {
|
|
60
|
-
transition(IDLE);
|
|
61
|
-
};
|
|
62
59
|
return {
|
|
63
60
|
getSnapshot: () => snapshot,
|
|
64
61
|
subscribe: (listener) => {
|
|
@@ -68,6 +65,6 @@ export const createSessionListController = (client) => {
|
|
|
68
65
|
load,
|
|
69
66
|
revoke,
|
|
70
67
|
revokeAll,
|
|
71
|
-
clear,
|
|
68
|
+
clear: () => transition(IDLE),
|
|
72
69
|
};
|
|
73
70
|
};
|
package/dist/sign-in-flow.d.ts
CHANGED
|
@@ -8,31 +8,51 @@ import type { AuthenticatedSession, BrowserAuthClient, BrowserAuthError, Browser
|
|
|
8
8
|
* without a renderer (react-dom is intentionally not a dependency here).
|
|
9
9
|
*
|
|
10
10
|
* Flow: idle → starting → awaiting_method → (email_challenge_sent |
|
|
11
|
-
* ceremony) → completing → signed_in.
|
|
11
|
+
* ceremony | challenge_required) → completing → signed_in. Other failures land
|
|
12
|
+
* in `failed`; challenge proof retry resumes the retained operation/transaction.
|
|
12
13
|
* `reset()` returns to `idle`. Social sign-in leaves the page via top-level
|
|
13
|
-
* redirect (design.md §22.3),
|
|
14
|
-
* redirect back, the app calls `client.handleHostedAuthorizationCallback`.
|
|
14
|
+
* redirect (design.md §22.3), then resumes through the callback handler.
|
|
15
15
|
*/
|
|
16
|
-
|
|
17
|
-
readonly step: "idle";
|
|
18
|
-
} | {
|
|
19
|
-
readonly step: "starting";
|
|
20
|
-
} | {
|
|
16
|
+
type AwaitingMethodState = {
|
|
21
17
|
readonly step: "awaiting_method";
|
|
22
18
|
readonly transactionId: string;
|
|
23
19
|
readonly methods: BrowserTransaction["availableMethods"];
|
|
24
20
|
readonly expiresAt: string;
|
|
25
|
-
}
|
|
21
|
+
};
|
|
22
|
+
type EmailChallengeSentState = {
|
|
26
23
|
readonly step: "email_challenge_sent";
|
|
27
24
|
readonly transactionId: string;
|
|
28
25
|
readonly challengeId: string;
|
|
29
26
|
readonly email: string;
|
|
30
27
|
readonly challengeExpiresAt: string;
|
|
31
|
-
}
|
|
28
|
+
};
|
|
29
|
+
type CeremonyState = {
|
|
32
30
|
readonly step: "ceremony";
|
|
33
31
|
readonly transactionId: string;
|
|
34
32
|
readonly ceremony: "passkey_registration" | "passkey_authentication";
|
|
33
|
+
};
|
|
34
|
+
export type SignInFlowChallengeResumeState = AwaitingMethodState | EmailChallengeSentState | CeremonyState;
|
|
35
|
+
export interface SignInFlowAdaptiveChallengeState {
|
|
36
|
+
readonly step: "challenge_required";
|
|
37
|
+
readonly challengeId: string;
|
|
38
|
+
readonly siteKey: string;
|
|
39
|
+
readonly action: Extract<BrowserAuthError, {
|
|
40
|
+
readonly type: "adaptive_challenge_required";
|
|
41
|
+
}>["action"];
|
|
42
|
+
readonly transactionId: string;
|
|
43
|
+
readonly requestId?: string;
|
|
44
|
+
readonly previousState: SignInFlowChallengeResumeState;
|
|
45
|
+
}
|
|
46
|
+
export type SignInFlowAdaptiveChallengeResult = {
|
|
47
|
+
readonly challengeId: string;
|
|
48
|
+
} | AuthenticatedSession | {
|
|
49
|
+
readonly redirected: true;
|
|
50
|
+
};
|
|
51
|
+
export type SignInFlowState = {
|
|
52
|
+
readonly step: "idle";
|
|
35
53
|
} | {
|
|
54
|
+
readonly step: "starting";
|
|
55
|
+
} | AwaitingMethodState | EmailChallengeSentState | CeremonyState | SignInFlowAdaptiveChallengeState | {
|
|
36
56
|
readonly step: "completing";
|
|
37
57
|
readonly transactionId: string;
|
|
38
58
|
} | {
|
|
@@ -81,6 +101,8 @@ export interface SignInFlow {
|
|
|
81
101
|
signInWithSocial(provider: SocialProvider): Promise<Outcome<{
|
|
82
102
|
redirected: true;
|
|
83
103
|
}, SignInFlowError>>;
|
|
104
|
+
/** Replays the retained factor-initiation request with its Turnstile proof. */
|
|
105
|
+
retryAdaptiveChallenge(challengeToken: string): Promise<Outcome<SignInFlowAdaptiveChallengeResult, SignInFlowError>>;
|
|
84
106
|
/** Best-effort server-side cancel, then back to `idle`. */
|
|
85
107
|
cancel(): Promise<Outcome<{
|
|
86
108
|
cancelled: true;
|
|
@@ -89,3 +111,4 @@ export interface SignInFlow {
|
|
|
89
111
|
reset(): void;
|
|
90
112
|
}
|
|
91
113
|
export declare const createSignInFlow: (client: SignInFlowClient) => SignInFlow;
|
|
114
|
+
export {};
|
package/dist/sign-in-flow.js
CHANGED
|
@@ -2,6 +2,8 @@ import { err, ok } from "@smartcrab/browser";
|
|
|
2
2
|
const invalidState = (expected, actual) => err({ type: "invalid_flow_state", expected, actual });
|
|
3
3
|
export const createSignInFlow = (client) => {
|
|
4
4
|
let snapshot = { state: { step: "idle" }, pendingAction: null };
|
|
5
|
+
let pendingAdaptiveChallenge = null;
|
|
6
|
+
let challengeAttemptId = 0;
|
|
5
7
|
const listeners = new Set();
|
|
6
8
|
const transition = (state, pendingAction = null) => {
|
|
7
9
|
snapshot = { state, pendingAction };
|
|
@@ -10,8 +12,28 @@ export const createSignInFlow = (client) => {
|
|
|
10
12
|
}
|
|
11
13
|
};
|
|
12
14
|
const fail = (error) => {
|
|
15
|
+
pendingAdaptiveChallenge = null;
|
|
16
|
+
challengeAttemptId += 1;
|
|
13
17
|
transition({ step: "failed", error });
|
|
14
18
|
};
|
|
19
|
+
const handleOperationFailure = (error, action, resumeState, pendingAction, retry) => {
|
|
20
|
+
if (error.type !== "adaptive_challenge_required" ||
|
|
21
|
+
error.action !== action ||
|
|
22
|
+
error.transactionId !== resumeState.transactionId) {
|
|
23
|
+
fail(error);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
pendingAdaptiveChallenge = { pendingAction, retry };
|
|
27
|
+
transition({
|
|
28
|
+
step: "challenge_required",
|
|
29
|
+
challengeId: error.challengeId,
|
|
30
|
+
siteKey: error.siteKey,
|
|
31
|
+
action: error.action,
|
|
32
|
+
transactionId: error.transactionId,
|
|
33
|
+
...(error.requestId !== undefined ? { requestId: error.requestId } : {}),
|
|
34
|
+
previousState: resumeState,
|
|
35
|
+
});
|
|
36
|
+
};
|
|
15
37
|
/** complete → exchange, shared by every verify path (design.md §16.3). */
|
|
16
38
|
const finalize = async (transactionId) => {
|
|
17
39
|
transition({ step: "completing", transactionId });
|
|
@@ -51,45 +73,61 @@ export const createSignInFlow = (client) => {
|
|
|
51
73
|
return outcome;
|
|
52
74
|
};
|
|
53
75
|
const sendEmailCode = async (email) => {
|
|
54
|
-
const
|
|
55
|
-
if (
|
|
56
|
-
return invalidState("awaiting_method",
|
|
76
|
+
const flowState = snapshot.state;
|
|
77
|
+
if (flowState.step !== "awaiting_method") {
|
|
78
|
+
return invalidState("awaiting_method", flowState.step);
|
|
57
79
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
80
|
+
const state = flowState;
|
|
81
|
+
async function run(challengeToken, attemptId) {
|
|
82
|
+
const outcome = await client.startEmailOtp(state.transactionId, challengeToken === undefined ? { email } : { email, challenge_token: challengeToken });
|
|
83
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
84
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
85
|
+
}
|
|
86
|
+
if (!outcome.ok) {
|
|
87
|
+
handleOperationFailure(outcome.error, "email_start", state, "send_email", run);
|
|
88
|
+
return outcome;
|
|
89
|
+
}
|
|
90
|
+
pendingAdaptiveChallenge = null;
|
|
91
|
+
transition({
|
|
92
|
+
step: "email_challenge_sent",
|
|
93
|
+
transactionId: state.transactionId,
|
|
94
|
+
challengeId: outcome.value.challenge_id,
|
|
95
|
+
email,
|
|
96
|
+
challengeExpiresAt: outcome.value.expires_at,
|
|
97
|
+
});
|
|
98
|
+
return ok({ challengeId: outcome.value.challenge_id });
|
|
63
99
|
}
|
|
64
|
-
transition(
|
|
65
|
-
|
|
66
|
-
transactionId: state.transactionId,
|
|
67
|
-
challengeId: outcome.value.challenge_id,
|
|
68
|
-
email,
|
|
69
|
-
challengeExpiresAt: outcome.value.expires_at,
|
|
70
|
-
});
|
|
71
|
-
return ok({ challengeId: outcome.value.challenge_id });
|
|
100
|
+
transition(state, "send_email");
|
|
101
|
+
return run(undefined, challengeAttemptId);
|
|
72
102
|
};
|
|
73
103
|
const resendEmailCode = async () => {
|
|
74
|
-
const
|
|
75
|
-
if (
|
|
76
|
-
return invalidState("email_challenge_sent",
|
|
104
|
+
const flowState = snapshot.state;
|
|
105
|
+
if (flowState.step !== "email_challenge_sent") {
|
|
106
|
+
return invalidState("email_challenge_sent", flowState.step);
|
|
77
107
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
108
|
+
const state = flowState;
|
|
109
|
+
async function run(challengeToken, attemptId) {
|
|
110
|
+
const outcome = await client.resendEmailOtp(state.transactionId, challengeToken === undefined
|
|
111
|
+
? { challenge_id: state.challengeId }
|
|
112
|
+
: { challenge_id: state.challengeId, challenge_token: challengeToken });
|
|
113
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
114
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
115
|
+
}
|
|
116
|
+
if (!outcome.ok) {
|
|
117
|
+
handleOperationFailure(outcome.error, "email_resend", state, "resend_email", run);
|
|
118
|
+
return outcome;
|
|
119
|
+
}
|
|
120
|
+
pendingAdaptiveChallenge = null;
|
|
121
|
+
// design.md §18.2: resend invalidates the prior code — adopt the new challenge.
|
|
122
|
+
transition({
|
|
123
|
+
...state,
|
|
124
|
+
challengeId: outcome.value.challenge_id,
|
|
125
|
+
challengeExpiresAt: outcome.value.expires_at,
|
|
126
|
+
});
|
|
127
|
+
return ok({ challengeId: outcome.value.challenge_id });
|
|
85
128
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
...state,
|
|
89
|
-
challengeId: outcome.value.challenge_id,
|
|
90
|
-
challengeExpiresAt: outcome.value.expires_at,
|
|
91
|
-
});
|
|
92
|
-
return ok({ challengeId: outcome.value.challenge_id });
|
|
129
|
+
transition(state, "resend_email");
|
|
130
|
+
return run(undefined, challengeAttemptId);
|
|
93
131
|
};
|
|
94
132
|
const verifyEmailCode = async (code) => {
|
|
95
133
|
const state = snapshot.state;
|
|
@@ -108,17 +146,34 @@ export const createSignInFlow = (client) => {
|
|
|
108
146
|
return finalize(state.transactionId);
|
|
109
147
|
};
|
|
110
148
|
const signInWithPasskey = async () => {
|
|
111
|
-
const
|
|
112
|
-
if (
|
|
113
|
-
return invalidState("awaiting_method",
|
|
149
|
+
const flowState = snapshot.state;
|
|
150
|
+
if (flowState.step !== "awaiting_method") {
|
|
151
|
+
return invalidState("awaiting_method", flowState.step);
|
|
114
152
|
}
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
153
|
+
const state = flowState;
|
|
154
|
+
const resumeState = {
|
|
155
|
+
step: "ceremony",
|
|
156
|
+
transactionId: state.transactionId,
|
|
157
|
+
ceremony: "passkey_authentication",
|
|
158
|
+
};
|
|
159
|
+
async function run(challengeToken, attemptId) {
|
|
160
|
+
const outcome = challengeToken === undefined
|
|
161
|
+
? await client.startPasskeyAuthentication(state.transactionId)
|
|
162
|
+
: await client.startPasskeyAuthentication(state.transactionId, {
|
|
163
|
+
challenge_token: challengeToken,
|
|
164
|
+
});
|
|
165
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
166
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
167
|
+
}
|
|
168
|
+
if (!outcome.ok) {
|
|
169
|
+
handleOperationFailure(outcome.error, "passkey_auth_options", resumeState, "passkey", run);
|
|
170
|
+
return outcome;
|
|
171
|
+
}
|
|
172
|
+
pendingAdaptiveChallenge = null;
|
|
173
|
+
return finalize(state.transactionId);
|
|
120
174
|
}
|
|
121
|
-
|
|
175
|
+
transition(resumeState, "passkey");
|
|
176
|
+
return run(undefined, challengeAttemptId);
|
|
122
177
|
};
|
|
123
178
|
const registerPasskey = async (input) => {
|
|
124
179
|
const state = snapshot.state;
|
|
@@ -134,30 +189,56 @@ export const createSignInFlow = (client) => {
|
|
|
134
189
|
return finalize(state.transactionId);
|
|
135
190
|
};
|
|
136
191
|
const signInWithSocial = async (provider) => {
|
|
137
|
-
const
|
|
138
|
-
if (
|
|
139
|
-
return invalidState("awaiting_method",
|
|
192
|
+
const flowState = snapshot.state;
|
|
193
|
+
if (flowState.step !== "awaiting_method") {
|
|
194
|
+
return invalidState("awaiting_method", flowState.step);
|
|
195
|
+
}
|
|
196
|
+
const state = flowState;
|
|
197
|
+
async function run(challengeToken, attemptId) {
|
|
198
|
+
const outcome = challengeToken === undefined
|
|
199
|
+
? await client.startSocialSignIn(state.transactionId, provider)
|
|
200
|
+
: await client.startSocialSignIn(state.transactionId, provider, {
|
|
201
|
+
challenge_token: challengeToken,
|
|
202
|
+
});
|
|
203
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
204
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
205
|
+
}
|
|
206
|
+
if (!outcome.ok) {
|
|
207
|
+
handleOperationFailure(outcome.error, "social_start", state, "social", run);
|
|
208
|
+
return outcome;
|
|
209
|
+
}
|
|
210
|
+
pendingAdaptiveChallenge = null;
|
|
211
|
+
// Success = the browser is navigating away (top-level redirect). The flow
|
|
212
|
+
// intentionally stays at method selection until the page reloads.
|
|
213
|
+
transition(state);
|
|
214
|
+
return ok({ redirected: true });
|
|
140
215
|
}
|
|
141
216
|
transition(state, "social");
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
217
|
+
return run(undefined, challengeAttemptId);
|
|
218
|
+
};
|
|
219
|
+
const retryAdaptiveChallenge = (challengeToken) => {
|
|
220
|
+
const state = snapshot.state;
|
|
221
|
+
const pending = pendingAdaptiveChallenge;
|
|
222
|
+
if (state.step !== "challenge_required" ||
|
|
223
|
+
pending === null ||
|
|
224
|
+
snapshot.pendingAction !== null) {
|
|
225
|
+
return Promise.resolve(invalidState("challenge_required", state.step));
|
|
146
226
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
transition(state);
|
|
151
|
-
return ok({ redirected: true });
|
|
227
|
+
const attemptId = ++challengeAttemptId;
|
|
228
|
+
transition(state, pending.pendingAction);
|
|
229
|
+
return pending.retry(challengeToken, attemptId);
|
|
152
230
|
};
|
|
153
231
|
const cancel = async () => {
|
|
154
232
|
const state = snapshot.state;
|
|
155
233
|
const transactionId = state.step === "awaiting_method" ||
|
|
156
234
|
state.step === "email_challenge_sent" ||
|
|
157
235
|
state.step === "ceremony" ||
|
|
236
|
+
state.step === "challenge_required" ||
|
|
158
237
|
state.step === "completing"
|
|
159
238
|
? state.transactionId
|
|
160
239
|
: null;
|
|
240
|
+
pendingAdaptiveChallenge = null;
|
|
241
|
+
challengeAttemptId += 1;
|
|
161
242
|
if (transactionId === null) {
|
|
162
243
|
transition({ step: "idle" });
|
|
163
244
|
return ok({ cancelled: true });
|
|
@@ -172,6 +253,8 @@ export const createSignInFlow = (client) => {
|
|
|
172
253
|
return ok({ cancelled: true });
|
|
173
254
|
};
|
|
174
255
|
const reset = () => {
|
|
256
|
+
pendingAdaptiveChallenge = null;
|
|
257
|
+
challengeAttemptId += 1;
|
|
175
258
|
transition({ step: "idle" });
|
|
176
259
|
};
|
|
177
260
|
return {
|
|
@@ -187,6 +270,7 @@ export const createSignInFlow = (client) => {
|
|
|
187
270
|
signInWithPasskey,
|
|
188
271
|
registerPasskey,
|
|
189
272
|
signInWithSocial,
|
|
273
|
+
retryAdaptiveChallenge,
|
|
190
274
|
cancel,
|
|
191
275
|
reset,
|
|
192
276
|
};
|
package/dist/user-controller.js
CHANGED
|
@@ -51,9 +51,6 @@ export const createUserProfileController = (client) => {
|
|
|
51
51
|
transition({ status: "loaded", user: outcome.value, error: null });
|
|
52
52
|
return ok(outcome.value);
|
|
53
53
|
};
|
|
54
|
-
const clear = () => {
|
|
55
|
-
transition(IDLE);
|
|
56
|
-
};
|
|
57
54
|
return {
|
|
58
55
|
getSnapshot: () => snapshot,
|
|
59
56
|
subscribe: (listener) => {
|
|
@@ -62,6 +59,6 @@ export const createUserProfileController = (client) => {
|
|
|
62
59
|
},
|
|
63
60
|
load,
|
|
64
61
|
update,
|
|
65
|
-
clear,
|
|
62
|
+
clear: () => transition(IDLE),
|
|
66
63
|
};
|
|
67
64
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smartcrab/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0-next-20261017141739",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"access": "public"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@smartcrab/browser": "0.
|
|
30
|
+
"@smartcrab/browser": "0.4.0-next-20261017141739"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"react": ">=19"
|