enableMfa method

Future<bool> enableMfa()

This is used by authenticated users to enable SMS-MFA for him/herself. A verified phone number is required.

Implementation

Future<bool> enableMfa() async {
  _signInUserSessionCheck();

  bool phoneNumberVerified = false;
  final getUserParamsReq = {
    'AccessToken': _signInUserSession!.getAccessToken().getJwtToken(),
  };
  final userData = await client!.request('GetUser', getUserParamsReq);

  if (userData['UserAttributes'] != null) {
    dynamic userAttributes = userData['UserAttributes'];
    phoneNumberVerified = null !=
        userAttributes.firstWhere(
            (attribute) =>
                attribute['Name'] == 'phone_number_verified' &&
                attribute['Value'] == 'true', orElse: () {
          return null;
        });
  }

  if (!phoneNumberVerified) {
    throw CognitoUserPhoneNumberVerificationNecessaryException(
        signInUserSession: _signInUserSession);
  }

  final mfaOptions = [];
  final mfaEnabled = {
    'DeliveryMedium': 'SMS',
    'AttributeName': 'phone_number',
  };
  mfaOptions.add(mfaEnabled);

  final paramsReq = {
    'MFAOptions': mfaOptions,
    'AccessToken': _signInUserSession!.getAccessToken().getJwtToken(),
  };

  await client!.request('SetUserSettings', paramsReq);
  return true;
}