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.

1개 답변
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
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠