-
Notifications
You must be signed in to change notification settings - Fork 239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(auth): Cancel sign in #2840
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -552,59 +552,49 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface | |
String? password, | ||
SignInOptions? options, | ||
}) async { | ||
bool isSignedIn; | ||
try { | ||
isSignedIn = (await fetchAuthSession()).isSignedIn; | ||
} on Exception { | ||
isSignedIn = false; | ||
} | ||
if (isSignedIn) { | ||
throw const InvalidStateException( | ||
'A user is already signed in.', | ||
recoverySuggestion: | ||
'Sign out the current user by calling `Amplify.Auth.signOut` and try the sign in again.', | ||
); | ||
} | ||
|
||
final pluginOptions = reifyPluginOptions( | ||
pluginOptions: options?.pluginOptions, | ||
defaultPluginOptions: const CognitoSignInPluginOptions(), | ||
); | ||
|
||
// Create a new state machine for every call since it caches values | ||
// internally on each run. | ||
final stream = _stateMachine.create(SignInStateMachine.type).stream; | ||
await _stateMachine | ||
.accept( | ||
SignInEvent.initiate( | ||
authFlowType: pluginOptions.authFlowType, | ||
parameters: SignInParameters( | ||
(p) => p | ||
..username = username | ||
..password = password, | ||
), | ||
clientMetadata: pluginOptions.clientMetadata, | ||
// Cancel the current sign in, if any. | ||
await _stateMachine.acceptAndComplete<SignInNotStarted>( | ||
const SignInEvent.cancelled(), | ||
); | ||
try { | ||
final result = await _stateMachine.acceptAndComplete<SignInState>( | ||
SignInEvent.initiate( | ||
authFlowType: pluginOptions.authFlowType, | ||
parameters: SignInParameters( | ||
(p) => p | ||
..username = username | ||
..password = password, | ||
), | ||
) | ||
.accepted; | ||
clientMetadata: pluginOptions.clientMetadata, | ||
), | ||
); | ||
|
||
await for (final state in stream) { | ||
switch (state.type) { | ||
switch (result.type) { | ||
case SignInStateType.notStarted: | ||
case SignInStateType.initiating: | ||
case SignInStateType.failure: | ||
// This should never happen. | ||
throw const UnknownException('Sign in could not be completed'); | ||
case SignInStateType.cancelling: | ||
continue; | ||
throw const UserCancelledException( | ||
'The user canceled the sign-in flow', | ||
); | ||
case SignInStateType.challenge: | ||
state as SignInChallenge; | ||
result as SignInChallenge; | ||
return CognitoSignInResult( | ||
isSignedIn: false, | ||
nextStep: AuthNextSignInStep( | ||
signInStep: state.challengeName.signInStep, | ||
signInStep: result.challengeName.signInStep, | ||
codeDeliveryDetails: _getChallengeDeliveryDetails( | ||
state.challengeParameters, | ||
result.challengeParameters, | ||
), | ||
additionalInfo: state.challengeParameters, | ||
missingAttributes: state.requiredAttributes, | ||
additionalInfo: result.challengeParameters, | ||
missingAttributes: result.requiredAttributes, | ||
), | ||
); | ||
case SignInStateType.success: | ||
|
@@ -614,28 +604,22 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface | |
signInStep: AuthSignInStep.done, | ||
), | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: maybe extract the switch-case to a function as it is used for signIn and confirmSignIn There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done 👍 |
||
case SignInStateType.failure: | ||
final exception = (state as SignInFailure).exception; | ||
if (exception is PasswordResetRequiredException) { | ||
return const CognitoSignInResult( | ||
isSignedIn: false, | ||
nextStep: AuthNextSignInStep( | ||
signInStep: AuthSignInStep.resetPassword, | ||
), | ||
); | ||
} else if (exception is UserNotConfirmedException) { | ||
return const CognitoSignInResult( | ||
isSignedIn: false, | ||
nextStep: AuthNextSignInStep( | ||
signInStep: AuthSignInStep.confirmSignUp, | ||
), | ||
); | ||
} | ||
throw exception; | ||
} | ||
} on PasswordResetRequiredException { | ||
return const CognitoSignInResult( | ||
isSignedIn: false, | ||
nextStep: AuthNextSignInStep( | ||
signInStep: AuthSignInStep.resetPassword, | ||
), | ||
); | ||
} on UserNotConfirmedException { | ||
return const CognitoSignInResult( | ||
isSignedIn: false, | ||
nextStep: AuthNextSignInStep( | ||
signInStep: AuthSignInStep.confirmSignUp, | ||
), | ||
); | ||
} | ||
|
||
throw const UserCancelledException('The user cancelled the sign-in flow'); | ||
} | ||
|
||
@override | ||
|
@@ -647,50 +631,46 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface | |
pluginOptions: options?.pluginOptions, | ||
defaultPluginOptions: const CognitoConfirmSignInPluginOptions(), | ||
); | ||
await _stateMachine | ||
.accept( | ||
SignInEvent.respondToChallenge( | ||
answer: confirmationValue, | ||
clientMetadata: pluginOptions.clientMetadata, | ||
userAttributes: pluginOptions.userAttributes, | ||
), | ||
) | ||
.accepted; | ||
|
||
final stream = _stateMachine.expect(SignInStateMachine.type).stream; | ||
await for (final state in stream) { | ||
switch (state.type) { | ||
case SignInStateType.notStarted: | ||
case SignInStateType.initiating: | ||
case SignInStateType.cancelling: | ||
continue; | ||
case SignInStateType.challenge: | ||
state as SignInChallenge; | ||
return CognitoSignInResult( | ||
isSignedIn: false, | ||
nextStep: AuthNextSignInStep( | ||
signInStep: state.challengeName.signInStep, | ||
codeDeliveryDetails: _getChallengeDeliveryDetails( | ||
state.challengeParameters, | ||
), | ||
additionalInfo: state.challengeParameters, | ||
missingAttributes: state.requiredAttributes, | ||
), | ||
); | ||
case SignInStateType.success: | ||
return const CognitoSignInResult( | ||
isSignedIn: true, | ||
nextStep: AuthNextSignInStep( | ||
signInStep: AuthSignInStep.done, | ||
final result = await _stateMachine.acceptAndComplete<SignInState>( | ||
SignInEvent.respondToChallenge( | ||
answer: confirmationValue, | ||
clientMetadata: pluginOptions.clientMetadata, | ||
userAttributes: pluginOptions.userAttributes, | ||
), | ||
); | ||
|
||
switch (result.type) { | ||
case SignInStateType.notStarted: | ||
case SignInStateType.initiating: | ||
case SignInStateType.failure: | ||
// This should never happen. | ||
throw const UnknownException('Sign in could not be completed'); | ||
case SignInStateType.cancelling: | ||
throw const UserCancelledException( | ||
'The user canceled the sign-in flow', | ||
); | ||
case SignInStateType.challenge: | ||
result as SignInChallenge; | ||
return CognitoSignInResult( | ||
isSignedIn: false, | ||
nextStep: AuthNextSignInStep( | ||
signInStep: result.challengeName.signInStep, | ||
codeDeliveryDetails: _getChallengeDeliveryDetails( | ||
result.challengeParameters, | ||
), | ||
); | ||
case SignInStateType.failure: | ||
throw (state as SignInFailure).exception; | ||
} | ||
additionalInfo: result.challengeParameters, | ||
missingAttributes: result.requiredAttributes, | ||
), | ||
); | ||
case SignInStateType.success: | ||
return const CognitoSignInResult( | ||
isSignedIn: true, | ||
nextStep: AuthNextSignInStep( | ||
signInStep: AuthSignInStep.done, | ||
), | ||
); | ||
} | ||
|
||
// This should never happen. | ||
throw const UnknownException('Sign in could not be completed'); | ||
} | ||
|
||
@override | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SignInStateMachine.resolve()
calls_assertSignedOut()
and_reset()
before running theSignInEvent.initiate
. why do we need to cancel sign in flow if any, isn't it taken care by the SignInStateMachine?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm yes this was leftover from earlier iterations. Removed 👍