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 all commits
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
186 changes: 68 additions & 118 deletions packages/auth/amplify_auth_cognito_dart/lib/src/auth_plugin_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -546,96 +546,80 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
return CognitoResendSignUpCodeResult(codeDeliveryDetails);
}

CognitoSignInResult _processSignInResult(SignInState result) {
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,
),
additionalInfo: result.challengeParameters,
missingAttributes: result.requiredAttributes,
),
);
case SignInStateType.success:
return const CognitoSignInResult(
isSignedIn: true,
nextStep: AuthNextSignInStep(
signInStep: AuthSignInStep.done,
),
);
}
}

@override
Future<CognitoSignInResult> signIn({
required String username,
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,
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) {
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,
),
);
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;
}
return _processSignInResult(result);
} 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,16 @@ 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,
),
);
case SignInStateType.failure:
throw (state as SignInFailure).exception;
}
}
final result = await _stateMachine.acceptAndComplete<SignInState>(
SignInEvent.respondToChallenge(
answer: confirmationValue,
clientMetadata: pluginOptions.clientMetadata,
userAttributes: pluginOptions.userAttributes,
),
);

// This should never happen.
throw const UnknownException('Sign in could not be completed');
return _processSignInResult(result);
}

@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