Configure Amplify Cognito in Android and iOS to switch programmatically between USERNAME_PASSWORD and CUSTOM_AUTHENTICATION

0

Hi,

I want to implement login with username password as well as login with otp(custom_auth) and provide user the option to select their mode of authentication just like the amazon shopping app. See screenshot.

In the android and ios docs of AWS amplify, I am supposed to provide a json file with the configuration but it accepts only one value for authenticationFlowType.

This was possible and currently implemented in my app using original cognito library but can't find the equivalent of it using aws amplify for Android and iOS.

demandé il y a 2 ans798 vues
1 réponse
0

You can not only set the Amplify configuration initially but also adjust per use.

So you can call Amplify.configure passing a patch to the settings sometimes.

Below you see an example where I used USER_SRP_AUTH and used a fallback to USER_PASSWORD_AUTH. (This is because when migrating users using Cognito Migration Triggers passwords need to be cleartext to be rehashed). But you can switch to custom auth.

The code is from a React project:

import Amplify from '@aws-amplify/core';
import Auth from '@aws-amplify/auth';

...

    handleSubmitLogin = async values => {
        const {
            login: { email, password }
        } = values;
        const { location: { search } } = this.props;
        try {
            let failed = false;
            try {
                Amplify.configure({
                    Auth: {
                    authenticationFlowType: 'USER_SRP_AUTH'
                    }
                });
                await Auth.signIn(email.trim().toLowerCase(), password);
            } catch(error) {
                console.log(error);
                failed = true;
            }
            if (failed) {
                Amplify.configure({
                    Auth: {
                        authenticationFlowType: 'USER_PASSWORD_AUTH'
                    }
                });
                await Auth.signIn(email.trim().toLowerCase(), password);   
            }      
            const params = queryString.parse((search || '?').substr(1));
            await this.props.handleLogin(params.ReturnUrl || '/profile/');
        } catch (e) {
            this.setState({ loginError: e.message });
        }
    };
profile picture
JaccoPK
répondu il y a 2 ans

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions