Sign-up
Amplify provides a client library that enables you to interact with backend resources such as Amplify Auth.
To get started, you can use the signUp()
API to create a new user in your backend:
func signUp(username: String, password: String, email: String, phonenumber: String) async { let userAttributes = [AuthUserAttribute(.email, value: email), AuthUserAttribute(.phoneNumber, value: phonenumber)] let options = AuthSignUpRequest.Options(userAttributes: userAttributes)
do { let signUpResult = try await Amplify.Auth.signUp( username: username, password: password, options: options )
if case let .confirmUser(deliveryDetails, _, userId) = signUpResult.nextStep { print("Delivery details \(String(describing: deliveryDetails)) for userId: \(String(describing: userId)))") } else { print("SignUp Complete") } } catch let error as AuthError { print("An error occurred while registering a user \(error)") } catch { print("Unexpected error: \(error)") }}
func signUp(username: String, password: String, email: String, phonenumber: String) -> AnyCancellable { let userAttributes = [ AuthUserAttribute(.email, value: email), AuthUserAttribute(.phoneNumber, value: phonenumber) ] let options = AuthSignUpRequest.Options(userAttributes: userAttributes) Amplify.Publisher.create { try await Amplify.Auth.signUp( username: username, password: password, options: options ) }.sink { if case let .failure(authError) = $0 { print("An error occurred while registering a user \(authError)") } } receiveValue: { signUpResult in if case let .confirmUser(deliveryDetails, _, userId) = signUpResult.nextStep { print("Delivery details \(String(describing: deliveryDetails)) for userId: \(String(describing: userId)))") } else { print("SignUp Complete") } } return sink}
The signUp
API response will include a nextStep
property, which can be used to determine if further action is required. It may return the following next steps:
Next Step | Description |
---|---|
confirmSignUp | The sign up needs to be confirmed by collecting a code from the user and calling confirmSignUp . |
done | The sign up process has been fully completed. |
Confirm sign-up
By default, each user that signs up remains in the unconfirmed status until they verify with a confirmation code that was sent to their email or phone number. The following are the default verification methods used when either phone
or email
are used as loginWith
options.
Login option | User account verification channel |
---|---|
phone | Phone Number |
email | |
email and phone |
You can confirm the sign-up after receiving a confirmation code from the user:
func confirmSignUp(for username: String, with confirmationCode: String) async { do { let confirmSignUpResult = try await Amplify.Auth.confirmSignUp( for: username, confirmationCode: confirmationCode ) print("Confirm sign up result completed: \(confirmSignUpResult.isSignUpComplete)") } catch let error as AuthError { print("An error occurred while confirming sign up \(error)") } catch { print("Unexpected error: \(error)") }}
func confirmSignUp(for username: String, with confirmationCode: String) -> AnyCancellable { Amplify.Publisher.create { try await Amplify.Auth.confirmSignUp( for: username, confirmationCode: confirmationCode ) }.sink { if case let .failure(authError) = $0 { print("An error occurred while confirming sign up \(authError)") } } receiveValue: { _ in print("Confirm signUp succeeded") }}