signUp method
Future<SignUpResult>
signUp({
- required String username,
- required String password,
- SignUpOptions? options,
Create a new user with the given username
and password
.
Optionally accepts plugin options
which allow customizing provider-specific
behavior, e.g. the Cognito User Pool.
For more information, see the Amplify docs.
Examples
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
/// Signs a user up with a username, password, and email. The required
/// attributes may be different depending on your app's configuration.
Future<void> signUpUser({
required String username,
required String password,
required String email,
String? phoneNumber,
}) async {
try {
final userAttributes = {
AuthUserAttributeKey.email: email,
if (phoneNumber != null) AuthUserAttributeKey.phoneNumber: phoneNumber,
// additional attributes as needed
};
final result = await Amplify.Auth.signUp(
username: username,
password: password,
options: SignUpOptions(
userAttributes: userAttributes,
),
);
return _handleSignUpResult(result);
} on AuthException catch (e) {
safePrint('Error signing up user: ${e.message}');
}
}
The next step in the sign up flow is to confirm the user. A confirmation code will be sent to the email or phone number provided during sign up. Prompt the user to check their device for the code sent by Cognito.
Future<void> _handleSignUpResult(SignUpResult result) async {
switch (result.nextStep.signUpStep) {
case AuthSignUpStep.confirmSignUp:
final codeDeliveryDetails = result.nextStep.codeDeliveryDetails!;
_handleCodeDelivery(codeDeliveryDetails);
case AuthSignUpStep.done:
safePrint('Sign up is complete');
}
}
void _handleCodeDelivery(AuthCodeDeliveryDetails codeDeliveryDetails) {
safePrint(
'A confirmation code has been sent to ${codeDeliveryDetails.destination}. '
'Please check your ${codeDeliveryDetails.deliveryMedium.name} for the code.',
);
}
If you need to resend the sign up code at any point in the sign up process, call resendSignUpCode.
Implementation
Future<SignUpResult> signUp({
required String username,
required String password,
SignUpOptions? options,
}) =>
identifyCall(
AuthCategoryMethod.signUp,
() => defaultPlugin.signUp(
username: username,
password: password,
options: options,
),
);