Skip to content
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 3 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
Copy link
Contributor

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 the SignInEvent.initiate. why do we need to cancel sign in flow if any, isn't it taken care by the SignInStateMachine?

Copy link
Contributor Author

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 👍

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:
Expand All @@ -614,28 +604,22 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
signInStep: AuthSignInStep.done,
),
);
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ class SignInInitiate extends SignInEvent {

@override
PreconditionException? checkPrecondition(SignInState currentState) {
if (currentState.type != SignInStateType.notStarted) {
return const AuthPreconditionException(
'Auth flow has already been initiated',
);
}
return null;
}
}
Expand Down