@smartcrab/react 0.3.2 → 0.4.0-next-20261016193833
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/sign-in-flow.d.ts +33 -10
- package/dist/sign-in-flow.js +142 -54
- 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/sign-in-flow.adaptive.test.ts +374 -0
- package/src/sign-in-flow.ts +251 -78
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";
|
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,63 @@ 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
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
80
|
+
const state = flowState;
|
|
81
|
+
const retry = (token, attemptId) => run(token, attemptId);
|
|
82
|
+
async function run(challengeToken, attemptId) {
|
|
83
|
+
const outcome = await client.startEmailOtp(state.transactionId, challengeToken === undefined ? { email } : { email, challenge_token: challengeToken });
|
|
84
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
85
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
86
|
+
}
|
|
87
|
+
if (!outcome.ok) {
|
|
88
|
+
handleOperationFailure(outcome.error, "email_start", state, "send_email", retry);
|
|
89
|
+
return outcome;
|
|
90
|
+
}
|
|
91
|
+
pendingAdaptiveChallenge = null;
|
|
92
|
+
transition({
|
|
93
|
+
step: "email_challenge_sent",
|
|
94
|
+
transactionId: state.transactionId,
|
|
95
|
+
challengeId: outcome.value.challenge_id,
|
|
96
|
+
email,
|
|
97
|
+
challengeExpiresAt: outcome.value.expires_at,
|
|
98
|
+
});
|
|
99
|
+
return ok({ challengeId: outcome.value.challenge_id });
|
|
63
100
|
}
|
|
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 });
|
|
101
|
+
transition(state, "send_email");
|
|
102
|
+
return run(undefined, challengeAttemptId);
|
|
72
103
|
};
|
|
73
104
|
const resendEmailCode = async () => {
|
|
74
|
-
const
|
|
75
|
-
if (
|
|
76
|
-
return invalidState("email_challenge_sent",
|
|
105
|
+
const flowState = snapshot.state;
|
|
106
|
+
if (flowState.step !== "email_challenge_sent") {
|
|
107
|
+
return invalidState("email_challenge_sent", flowState.step);
|
|
77
108
|
}
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
109
|
+
const state = flowState;
|
|
110
|
+
const retry = (token, attemptId) => run(token, attemptId);
|
|
111
|
+
async function run(challengeToken, attemptId) {
|
|
112
|
+
const outcome = await client.resendEmailOtp(state.transactionId, challengeToken === undefined
|
|
113
|
+
? { challenge_id: state.challengeId }
|
|
114
|
+
: { challenge_id: state.challengeId, challenge_token: challengeToken });
|
|
115
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
116
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
117
|
+
}
|
|
118
|
+
if (!outcome.ok) {
|
|
119
|
+
handleOperationFailure(outcome.error, "email_resend", state, "resend_email", retry);
|
|
120
|
+
return outcome;
|
|
121
|
+
}
|
|
122
|
+
pendingAdaptiveChallenge = null;
|
|
123
|
+
// design.md §18.2: resend invalidates the prior code — adopt the new challenge.
|
|
124
|
+
transition({
|
|
125
|
+
...state,
|
|
126
|
+
challengeId: outcome.value.challenge_id,
|
|
127
|
+
challengeExpiresAt: outcome.value.expires_at,
|
|
128
|
+
});
|
|
129
|
+
return ok({ challengeId: outcome.value.challenge_id });
|
|
85
130
|
}
|
|
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 });
|
|
131
|
+
transition(state, "resend_email");
|
|
132
|
+
return run(undefined, challengeAttemptId);
|
|
93
133
|
};
|
|
94
134
|
const verifyEmailCode = async (code) => {
|
|
95
135
|
const state = snapshot.state;
|
|
@@ -108,17 +148,35 @@ export const createSignInFlow = (client) => {
|
|
|
108
148
|
return finalize(state.transactionId);
|
|
109
149
|
};
|
|
110
150
|
const signInWithPasskey = async () => {
|
|
111
|
-
const
|
|
112
|
-
if (
|
|
113
|
-
return invalidState("awaiting_method",
|
|
151
|
+
const flowState = snapshot.state;
|
|
152
|
+
if (flowState.step !== "awaiting_method") {
|
|
153
|
+
return invalidState("awaiting_method", flowState.step);
|
|
114
154
|
}
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
155
|
+
const state = flowState;
|
|
156
|
+
const resumeState = {
|
|
157
|
+
step: "ceremony",
|
|
158
|
+
transactionId: state.transactionId,
|
|
159
|
+
ceremony: "passkey_authentication",
|
|
160
|
+
};
|
|
161
|
+
const retry = (token, attemptId) => run(token, attemptId);
|
|
162
|
+
async function run(challengeToken, attemptId) {
|
|
163
|
+
const outcome = challengeToken === undefined
|
|
164
|
+
? await client.startPasskeyAuthentication(state.transactionId)
|
|
165
|
+
: await client.startPasskeyAuthentication(state.transactionId, {
|
|
166
|
+
challenge_token: challengeToken,
|
|
167
|
+
});
|
|
168
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
169
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
170
|
+
}
|
|
171
|
+
if (!outcome.ok) {
|
|
172
|
+
handleOperationFailure(outcome.error, "passkey_auth_options", resumeState, "passkey", retry);
|
|
173
|
+
return outcome;
|
|
174
|
+
}
|
|
175
|
+
pendingAdaptiveChallenge = null;
|
|
176
|
+
return finalize(state.transactionId);
|
|
120
177
|
}
|
|
121
|
-
|
|
178
|
+
transition(resumeState, "passkey");
|
|
179
|
+
return run(undefined, challengeAttemptId);
|
|
122
180
|
};
|
|
123
181
|
const registerPasskey = async (input) => {
|
|
124
182
|
const state = snapshot.state;
|
|
@@ -134,30 +192,57 @@ export const createSignInFlow = (client) => {
|
|
|
134
192
|
return finalize(state.transactionId);
|
|
135
193
|
};
|
|
136
194
|
const signInWithSocial = async (provider) => {
|
|
137
|
-
const
|
|
138
|
-
if (
|
|
139
|
-
return invalidState("awaiting_method",
|
|
195
|
+
const flowState = snapshot.state;
|
|
196
|
+
if (flowState.step !== "awaiting_method") {
|
|
197
|
+
return invalidState("awaiting_method", flowState.step);
|
|
198
|
+
}
|
|
199
|
+
const state = flowState;
|
|
200
|
+
const retry = (token, attemptId) => run(token, attemptId);
|
|
201
|
+
async function run(challengeToken, attemptId) {
|
|
202
|
+
const outcome = challengeToken === undefined
|
|
203
|
+
? await client.startSocialSignIn(state.transactionId, provider)
|
|
204
|
+
: await client.startSocialSignIn(state.transactionId, provider, {
|
|
205
|
+
challenge_token: challengeToken,
|
|
206
|
+
});
|
|
207
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
208
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
209
|
+
}
|
|
210
|
+
if (!outcome.ok) {
|
|
211
|
+
handleOperationFailure(outcome.error, "social_start", state, "social", retry);
|
|
212
|
+
return outcome;
|
|
213
|
+
}
|
|
214
|
+
pendingAdaptiveChallenge = null;
|
|
215
|
+
// Success = the browser is navigating away (top-level redirect). The flow
|
|
216
|
+
// intentionally stays at method selection until the page reloads.
|
|
217
|
+
transition(state);
|
|
218
|
+
return ok({ redirected: true });
|
|
140
219
|
}
|
|
141
220
|
transition(state, "social");
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
221
|
+
return run(undefined, challengeAttemptId);
|
|
222
|
+
};
|
|
223
|
+
const retryAdaptiveChallenge = (challengeToken) => {
|
|
224
|
+
const state = snapshot.state;
|
|
225
|
+
const pending = pendingAdaptiveChallenge;
|
|
226
|
+
if (state.step !== "challenge_required" ||
|
|
227
|
+
pending === null ||
|
|
228
|
+
snapshot.pendingAction !== null) {
|
|
229
|
+
return Promise.resolve(invalidState("challenge_required", state.step));
|
|
146
230
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
transition(state);
|
|
151
|
-
return ok({ redirected: true });
|
|
231
|
+
const attemptId = ++challengeAttemptId;
|
|
232
|
+
transition(state, pending.pendingAction);
|
|
233
|
+
return pending.retry(challengeToken, attemptId);
|
|
152
234
|
};
|
|
153
235
|
const cancel = async () => {
|
|
154
236
|
const state = snapshot.state;
|
|
155
237
|
const transactionId = state.step === "awaiting_method" ||
|
|
156
238
|
state.step === "email_challenge_sent" ||
|
|
157
239
|
state.step === "ceremony" ||
|
|
240
|
+
state.step === "challenge_required" ||
|
|
158
241
|
state.step === "completing"
|
|
159
242
|
? state.transactionId
|
|
160
243
|
: null;
|
|
244
|
+
pendingAdaptiveChallenge = null;
|
|
245
|
+
challengeAttemptId += 1;
|
|
161
246
|
if (transactionId === null) {
|
|
162
247
|
transition({ step: "idle" });
|
|
163
248
|
return ok({ cancelled: true });
|
|
@@ -172,6 +257,8 @@ export const createSignInFlow = (client) => {
|
|
|
172
257
|
return ok({ cancelled: true });
|
|
173
258
|
};
|
|
174
259
|
const reset = () => {
|
|
260
|
+
pendingAdaptiveChallenge = null;
|
|
261
|
+
challengeAttemptId += 1;
|
|
175
262
|
transition({ step: "idle" });
|
|
176
263
|
};
|
|
177
264
|
return {
|
|
@@ -187,6 +274,7 @@ export const createSignInFlow = (client) => {
|
|
|
187
274
|
signInWithPasskey,
|
|
188
275
|
registerPasskey,
|
|
189
276
|
signInWithSocial,
|
|
277
|
+
retryAdaptiveChallenge,
|
|
190
278
|
cancel,
|
|
191
279
|
reset,
|
|
192
280
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smartcrab/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0-next-20261016193833",
|
|
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-20261016193833"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"react": ">=19"
|