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 年前檢視次數 772 次
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 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南