Skip to content

Instantly share code, notes, and snippets.

@salihgueler
Created August 26, 2022 15:01
Show Gist options
  • Save salihgueler/66cd0a542bd12ce090a75a68f2234ee1 to your computer and use it in GitHub Desktop.
Save salihgueler/66cd0a542bd12ce090a75a68f2234ee1 to your computer and use it in GitHub Desktop.
Future<void> _signUpUser() async {
if (_formKey.currentState!.validate()) {
// (1)
setState(() {
_isLoading = true;
_errorMessage = null;
});
try {
final result = await Amplify.Auth.signUp(
username: _usernameController.text,
password: _passwordController.text,
options: CognitoSignUpOptions(
userAttributes: <CognitoUserAttributeKey, String>{
CognitoUserAttributeKey.email: _emailController.text,
},
),
);
// (2)
if (result.nextStep.signUpStep == CognitoSignUpStep.confirmSignUp.value) {
// (3)
if (mounted) {
context.go(
'/confirm-user',
extra: _usernameController.text,
);
}
}
// (4)
setState(() {
_isLoading = false;
});
// (5)
_usernameController.clear();
_emailController.clear();
_passwordController.clear();
// (6)
} on UsernameExistsException {
setState(() {
_isLoading = false;
_errorMessage = 'Username already exists. Please pick a different one.';
});
} on Exception catch(error) {
setState(() {
_isLoading = false;
_errorMessage = 'An unexpected error has happened. Check the logs for detail';
});
logger.error(error.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment