Skip to content

Commit 475bc1f

Browse files
committed
fix: refactor factors lists into single code block [#325]
1 parent c98c294 commit 475bc1f

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

packages/clerk_auth/lib/src/models/client/sign_in.dart

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,8 @@ class SignIn extends AuthObject with InformativeToStringMixin {
106106
/// Is this [SignIn] transferable to a [SignUp]?
107107
bool get isTransferable => verification?.status.isTransferable == true;
108108

109-
/// fromJson
110-
static SignIn fromJson(Map<String, dynamic> json) => _$SignInFromJson(json);
111-
112-
/// toJson
113-
@override
114-
Map<String, dynamic> toJson() => _$SignInToJson(this);
115-
116109
/// Find a [Verification] if one exists for this [SignIn]
117-
/// at the giver [Stage]
110+
/// at the given [Stage]
118111
///
119112
Verification? verificationFor(Stage stage) {
120113
return switch (stage) {
@@ -123,7 +116,26 @@ class SignIn extends AuthObject with InformativeToStringMixin {
123116
};
124117
}
125118

126-
/// The factors for the current stage
119+
/// Find a list of [Factor]s for this [SignIn]
120+
/// at the given [Stage]
121+
///
122+
List<Factor> factorsFor(Stage stage) {
123+
return switch (stage) {
124+
Stage.first => supportedFirstFactors,
125+
Stage.second => supportedSecondFactors,
126+
};
127+
}
128+
129+
/// Do we need factors for the given [Stage]?
130+
///
131+
bool needsFactorsFor(Stage stage) {
132+
return switch (stage) {
133+
Stage.first => needsFirstFactor,
134+
Stage.second => needsSecondFactor,
135+
};
136+
}
137+
138+
/// The factors for the current status
127139
List<Factor> get factors => switch (status) {
128140
Status.needsFirstFactor => supportedFirstFactors,
129141
Status.needsSecondFactor => supportedSecondFactors,
@@ -134,9 +146,7 @@ class SignIn extends AuthObject with InformativeToStringMixin {
134146
bool get canUsePassword => factors.any((f) => f.strategy.isPassword);
135147

136148
/// Find the [Factor] for this [SignIn] that matches
137-
/// the [strategy] and [stage]
138-
///
139-
/// Throw an error on failure
149+
/// the [strategy] and optional [stage], or null
140150
///
141151
Factor? factorFor(
142152
Strategy strategy, {
@@ -153,4 +163,11 @@ class SignIn extends AuthObject with InformativeToStringMixin {
153163

154164
return null;
155165
}
166+
167+
/// fromJson
168+
static SignIn fromJson(Map<String, dynamic> json) => _$SignInFromJson(json);
169+
170+
/// toJson
171+
@override
172+
Map<String, dynamic> toJson() => _$SignInToJson(this);
156173
}

packages/clerk_flutter/lib/src/widgets/authentication/clerk_sign_in_panel.dart

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class _ClerkSignInPanelState extends State<ClerkSignInPanel>
9393
clerk.Strategy.enterpriseSSO,
9494
identifier: _identifier.orNullIfEmpty,
9595
);
96-
} else if (signIn.needsSecondFactor && factors.length == 1) {
96+
} else if (signIn.needsFactor && factors.length == 1) {
9797
await authState.attemptSignIn(strategy: factors.first.strategy);
9898
}
9999
if (signIn.needsFactor && signIn.factors.length == 1) {
@@ -152,8 +152,6 @@ class _ClerkSignInPanelState extends State<ClerkSignInPanel>
152152
final themeExtension = ClerkAuth.themeExtensionOf(context);
153153

154154
final signIn = authState.signIn ?? clerk.SignIn.empty;
155-
final firstFactors = signIn.supportedFirstFactors;
156-
final secondFactors = signIn.supportedSecondFactors;
157155
final safeIdentifier = signIn.factorFor(_strategy)?.safeIdentifier;
158156

159157
return Column(
@@ -199,27 +197,18 @@ class _ClerkSignInPanelState extends State<ClerkSignInPanel>
199197
onSubmit: (code) => _submitCode(code, authState),
200198
),
201199

202-
// First factors
203-
if (firstFactors.isNotEmpty) //
204-
_FactorList(
205-
key: const Key('firstFactors'),
206-
open: signIn.needsFirstFactor &&
207-
_externalActionFactorChosen(firstFactors) == false,
208-
factors: firstFactors,
209-
onPasswordChanged: _updatePassword,
210-
onSubmit: (strategy) => _continue(authState, strategy: strategy),
211-
),
212-
213-
// Second factors
214-
if (secondFactors.length > 1) //
215-
_FactorList(
216-
key: const Key('secondFactors'),
217-
open: signIn.needsSecondFactor &&
218-
_externalActionFactorChosen(secondFactors) == false,
219-
factors: secondFactors,
220-
onPasswordChanged: _updatePassword,
221-
onSubmit: (strategy) => _continue(authState, strategy: strategy),
222-
),
200+
// Factors for each stage
201+
for (final stage in clerk.Stage.values) //
202+
if (signIn.factorsFor(stage) case final factors
203+
when factors.isNotEmpty) //
204+
_FactorList(
205+
key: ValueKey<clerk.Stage>(stage),
206+
open: signIn.needsFactorsFor(stage) &&
207+
_externalActionFactorChosen(factors) == false,
208+
factors: factors,
209+
onPasswordChanged: _updatePassword,
210+
onSubmit: (strategy) => _continue(authState, strategy: strategy),
211+
),
223212

224213
verticalMargin8,
225214

0 commit comments

Comments
 (0)