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") }}
Sign up with passwordless methods
Your application's users can also sign up using passwordless methods. To learn more, visit the concepts page for passwordless.
SMS OTP
// Sign up using an phone numberfunc signUp(username: String, phonenumber: String) async { let userAttributes = [ AuthUserAttribute(.phoneNumber, value: phonenumber) ] let options = AuthSignUpRequest.Options(userAttributes: userAttributes) do { let signUpResult = try await Amplify.Auth.signUp( username: username, 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)") }}
// Confirm sign up with the OTP receivedfunc 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)") }}
// Sign up using a phone numberfunc signUp(username: String, phonenumber: String) -> AnyCancellable { let userAttributes = [ AuthUserAttribute(.phoneNumber, value: phonenumber) ] let options = AuthSignUpRequest.Options(userAttributes: userAttributes) let sink = Amplify.Publisher.create { try await Amplify.Auth.signUp( username: username, 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}
// Confirm sign up with the OTP receivedfunc 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") }}
Email OTP
// Sign up using an emailfunc signUp(username: String, email: String) async { let userAttributes = [ AuthUserAttribute(.email, value: email) ] let options = AuthSignUpRequest.Options(userAttributes: userAttributes) do { let signUpResult = try await Amplify.Auth.signUp( username: username, 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)") }}
// Confirm sign up with the OTP receivedfunc 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)") }}
// Sign up using an emailfunc signUp(username: String, email: String) -> AnyCancellable { let userAttributes = [ AuthUserAttribute(.email, value: email) ] let options = AuthSignUpRequest.Options(userAttributes: userAttributes) let sink = Amplify.Publisher.create { try await Amplify.Auth.signUp( username: username, 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}
// Confirm sign up with the OTP receivedfunc 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") }}
Auto Sign In
// Confirm sign up with the OTP received and auto sign infunc confirmSignUp(for username: String, with confirmationCode: String) async { do { let confirmSignUpResult = try await Amplify.Auth.confirmSignUp( for: username, confirmationCode: confirmationCode ) if case .completeAutoSignIn(let session) = confirmSignUpResult.nextStep { let autoSignInResult = try await Amplify.Auth.autoSignIn() print("Auto sign in result: \(autoSignInResult.isSignedIn)") } else { 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)") }}
// Confirm sign up with the OTP received and auto sign infunc 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: { confirmSignUpResult in if case let .completeAutoSignIn(session) = confirmSignUpResult.nextStep { print("Confirm Sign Up succeeded. Next step is auto sign in") // call `autoSignIn()` API to complete sign in } else { print("Confirm sign up result completed: \(confirmSignUpResult.isSignUpComplete)") } }}
func autoSignIn() -> AnyCancellable { Amplify.Publisher.create { try await Amplify.Auth.autoSignIn() }.sink { if case let .failure(authError) = $0 { print("Auto Sign in failed \(authError)") } } receiveValue: { autoSignInResult in if autoSignInResult.isSignedIn { print("Auto Sign in succeeded") } }}