Back in the Create A Flutter App module, we implemented our AuthService to handle the updating of our AuthState based on the function called. Now we need to update each of our functions to only update the state when the user successfully completes each process.
In auth_service.dart add an AuthCredentials property in AuthService:
... // import 'auth_credentials.dart'; (line 2)
import 'package:amplify_flutter/amplify.dart';
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
.. // final authStateController = StreamController<AuthState>(); (line 17)
AuthCredentials _credentials;
... // void showSignUp() {
This property will be used to keep the SignUpCredentials in memory during the sign up process so a user can be logged in immediately after verifying their email address. If we didn’t do this, the user would need to login manually by going to the login screen.
Update signUpWithCredentials to the following:
... // } Closing brace of loginWithCredentials (line 34)
// 1
void signUpWithCredentials(SignUpCredentials credentials) async {
try {
// 2
final userAttributes = {'email': credentials.email};
// 3
final result = await Amplify.Auth.signUp(
username: credentials.username,
password: credentials.password,
options: CognitoSignUpOptions(userAttributes: userAttributes));
// 4
if (result.isSignUpComplete) {
// 5
this._credentials = credentials;
// 6
final state = AuthState(authFlowStatus: AuthFlowStatus.verification);
authStateController.add(state);
}
// 7
} on AmplifyException catch (authError) {
print('Failed to sign up - ${authError.message}');
}
}
Update verifyCode to this:
... // } Closing brace of signUpWithCredentials (line 68)
// 1
void verifyCode(String verificationCode) async {
try {
// 2
final result = await Amplify.Auth.confirmSignUp(
username: _credentials.username, confirmationCode: verificationCode);
// 3
if (result.isSignUpComplete) {
loginWithCredentials(_credentials);
} else {
// 4
// Follow more steps
}
} on AmplifyException catch (authError) {
print('Failed to sign up - ${authError.message}');
}
}