How to enable Auto Sign-in after successfully Sign-up in Amplify AWS Cognito?

489 views Asked by At

I'm trying to do on-page passwordless authentication with only phone number, and I want to implement auto sign-in after sign-up automatically, any ideas?

I have enabled mfa for both login and signup, but amplify not signing the user after signup!

1

There are 1 answers

0
Ilyas Darwesh On

you are working with AWS Amplify and trying to implement on-page passwordless authentication with phone numbers in an Android Studio project, and you want users to be automatically signed in after signing up, even with MFA enabled. The automatic sign-in after sign-up might not be the default behavior, so you'll need to handle this logic in your code.

When a user signs up with their phone number, use the Amplify Auth category to initiate the sign-up process.

Amplify.Auth.signUp(
    phoneNumber, // The phone number provided by the user
    password,    // A temporary password (required for MFA)
    AuthSignUpOptions.builder().build(),
    result -> {
        // Sign-up successful, proceed to verification
        String confirmationCode = result.getNextStep().getCodeDeliveryDetails().getDestination();
        // Show a confirmation code entry UI to the user
    },
    error -> {
        // Handle sign-up error
    }
);

After sign-up, Amplify will send a confirmation code to the user's phone. Once the user enters the confirmation code, you can use Amplify.Auth.confirmSignUp to confirm the user.

Amplify.Auth.confirmSignUp(
    phoneNumber,
    confirmationCode,
    result -> {
        // Confirmation successful, now sign in the user
        Amplify.Auth.signIn(
            phoneNumber,  // The phone number used for sign-up
            password,
            authResult -> {
                // User is signed in
            },
            authError -> {
                // Handle sign-in error
            }
        );
    },
    error -> {
        // Handle confirmation error
    }
);

Hopefully, it will help!!!