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.

已提问 2 年前800 查看次数
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 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则