Skip to content

Commit bf73a7e

Browse files
authored
fix: refactor attemptsignin [#147] (#148)
1 parent 4bb9a8a commit bf73a7e

File tree

2 files changed

+47
-42
lines changed

2 files changed

+47
-42
lines changed

packages/clerk_auth/lib/src/clerk_auth/auth.dart

+25-18
Original file line numberDiff line numberDiff line change
@@ -250,23 +250,28 @@ class Auth {
250250
String? token,
251251
String? redirectUrl,
252252
}) async {
253-
if ((client.signIn == null ||
254-
client.signIn?.status == Status.needsIdentifier) &&
255-
identifier is String) {
256-
// if a password has been presented, we can immediately attempt a sign in
257-
// if `password` is null it will be ignored
253+
if (client.signIn == null) {
254+
// if password and identifier been presented, we can immediately attempt
255+
// a sign in; if null they will be ignored
258256
await _api
259257
.createSignIn(identifier: identifier, password: password)
260258
.then(_housekeeping);
261-
262-
// Did we just complete sign in with password?
263-
if (isSignedIn) {
264-
return;
265-
}
266259
}
267260

268261
switch (client.signIn) {
269-
case SignIn signIn when strategy.isOauth == true && token is String:
262+
case null when client.user is User:
263+
// We have signed in - possibly when creating the [SignIn] above
264+
break;
265+
266+
case SignIn signIn
267+
when signIn.status == Status.needsIdentifier && identifier is String:
268+
// if a password has been presented, we can immediately attempt a
269+
// sign in; if `password` is null it will be ignored
270+
await _api
271+
.createSignIn(identifier: identifier, password: password)
272+
.then(_housekeeping);
273+
274+
case SignIn signIn when strategy.isOauth && token is String:
270275
await _api
271276
.sendOauthToken(signIn, strategy: strategy, token: token)
272277
.then(_housekeeping);
@@ -298,7 +303,7 @@ class Auth {
298303
return signInCompleter.future;
299304

300305
case SignIn signIn
301-
when (signIn.status == Status.needsFirstFactor) &&
306+
when signIn.status == Status.needsFirstFactor &&
302307
strategy == Strategy.password:
303308
await _api
304309
.attemptSignIn(
@@ -310,7 +315,7 @@ class Auth {
310315
.then(_housekeeping);
311316

312317
case SignIn signIn
313-
when signIn.status.needsFactor && strategy.requiresCode == true:
318+
when signIn.status.needsFactor && strategy.requiresCode:
314319
final stage = Stage.forStatus(signIn.status);
315320
if (signIn.verificationFor(stage) is! Verification) {
316321
await _api
@@ -338,11 +343,13 @@ class Auth {
338343
// No matching sign-in sequence, reset loading state
339344
default:
340345
final status = signIn?.status ?? Status.unknown;
341-
addError(AuthError(
342-
code: AuthErrorCode.signInError,
343-
message: 'Unsupported sign in attempt: {arg}',
344-
argument: status.name,
345-
));
346+
addError(
347+
AuthError(
348+
code: AuthErrorCode.signInError,
349+
message: 'Unsupported sign in attempt: {arg}',
350+
argument: status.name,
351+
),
352+
);
346353
}
347354

348355
update();

packages/clerk_flutter/lib/src/utils/localization_extensions.dart

+22-24
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,28 @@ typedef LocalizedMessage = String Function(ClerkSdkLocalizations localizations);
99
extension ClerkAuthErrorExtension on clerk.AuthError {
1010
/// Allow localization of an [clerk.AuthError]
1111
String localizedMessage(ClerkSdkLocalizations localizations) {
12-
switch (code) {
13-
case clerk.AuthErrorCode.noStageForStatus:
14-
return localizations.noStageForStatus(argument.toString());
15-
case clerk.AuthErrorCode.noSessionTokenRetrieved:
16-
return localizations.noSessionTokenRetrieved;
17-
case clerk.AuthErrorCode.noAssociatedStrategy:
18-
return localizations.noAssociatedStrategy(argument.toString());
19-
case clerk.AuthErrorCode.passwordMatchError:
20-
return localizations.passwordAndPasswordConfirmationMustMatch;
21-
case clerk.AuthErrorCode.jwtPoorlyFormatted:
22-
return localizations.jwtPoorlyFormatted(argument.toString());
23-
case clerk.AuthErrorCode.actionNotTimely:
24-
return localizations.actionNotTimely;
25-
case clerk.AuthErrorCode.noSessionFoundForUser:
26-
return localizations.noSessionFoundForUser(argument.toString());
27-
case clerk.AuthErrorCode.noSuchFirstFactorStrategy:
28-
return localizations.noSuchFirstFactorStrategy(argument.toString());
29-
case clerk.AuthErrorCode.noSuchSecondFactorStrategy:
30-
return localizations.noSuchSecondFactorStrategy(argument.toString());
31-
case clerk.AuthErrorCode.signInError:
32-
return localizations.signInError(argument.toString());
33-
default:
34-
return toString();
35-
}
12+
return switch (code) {
13+
clerk.AuthErrorCode.noStageForStatus =>
14+
localizations.noStageForStatus(argument.toString()),
15+
clerk.AuthErrorCode.noSessionTokenRetrieved =>
16+
localizations.noSessionTokenRetrieved,
17+
clerk.AuthErrorCode.noAssociatedStrategy =>
18+
localizations.noAssociatedStrategy(argument.toString()),
19+
clerk.AuthErrorCode.passwordMatchError =>
20+
localizations.passwordAndPasswordConfirmationMustMatch,
21+
clerk.AuthErrorCode.jwtPoorlyFormatted =>
22+
localizations.jwtPoorlyFormatted(argument.toString()),
23+
clerk.AuthErrorCode.actionNotTimely => localizations.actionNotTimely,
24+
clerk.AuthErrorCode.noSessionFoundForUser =>
25+
localizations.noSessionFoundForUser(argument.toString()),
26+
clerk.AuthErrorCode.noSuchFirstFactorStrategy =>
27+
localizations.noSuchFirstFactorStrategy(argument.toString()),
28+
clerk.AuthErrorCode.noSuchSecondFactorStrategy =>
29+
localizations.noSuchSecondFactorStrategy(argument.toString()),
30+
clerk.AuthErrorCode.signInError =>
31+
localizations.signInError(argument.toString()),
32+
_ => toString(),
33+
};
3634
}
3735
}
3836

0 commit comments

Comments
 (0)