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年前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン