React Native auto fill and suggestions for email and password inputs

Viewed 141

I'm using React Native 0.63.4 and my goal is to have auto fill working both on Android and iOS:

  • get email suggestions (for example, when filling the email input on sign up and when logging in for the first time)
  • get asked to save the password after successfully logged in
  • get the saved credentials when logging in again

Here's the component I am using for the inputs:


const InputField: React.FC<Props> = ({
  placeholder,
  onTextChange,
  keyboardType = "default",
  securedTextEntry,
  isMissing,
  textContentType = "none",
  autoCompleteType = "off"
}) => {

  const [thisSecureTextEntry, setThisSecureTextEntry] = useState(false);

  const isAutoCapitalizeDisabled =
    keyboardType === 'email-address' || 'password' ? 'none' : 'sentences';

  const importantForAutofill = autoCompleteType ? 'yes' : 'no';
  

  const updateThisSecureTextEntry = (value: boolean) => {
    if(securedTextEntry) {
      setThisSecureTextEntry(value);
    }
  }

  return (
    Platform.OS === 'ios' ?
        <TextInput
          style={styles.text}
          placeholder={placeholder}
          placeholderTextColor={isMissing ? ERROR_TEXT : PLACEHOLDER_GRAY}
          onChangeText={onTextChange}
          onFocus={() => updateThisSecureTextEntry(true)}
          onBlur={() => updateThisSecureTextEntry(true)}
          secureTextEntry={thisSecureTextEntry}
          keyboardType={keyboardType}
          autoCapitalize={isAutoCapitalizeDisabled}
          
          textContentType={textContentType}
        /> : 
        <TextInput
          style={styles.text}
          placeholder={placeholder}
          placeholderTextColor={isMissing ? ERROR_TEXT : PLACEHOLDER_GRAY}
          onChangeText={onTextChange}
          onFocus={() => updateThisSecureTextEntry(true)}
          onBlur={() => updateThisSecureTextEntry(true)}
          secureTextEntry={thisSecureTextEntry}
          keyboardType={keyboardType}
          autoCapitalize={isAutoCapitalizeDisabled}
          
          autoCompleteType={autoCompleteType}
          importantForAutofill={importantForAutofill}
        />
  );
};

export default InputField;

And here's how I use it for login:

<InputField
          placeholder={translations.EMAIL}
          onTextChange={setEmail}
          keyboardType={'email-address'}
          autoCompleteType={'username'}
          textContentType={'username'}
        />
        <InputField
          placeholder={translations.PASSWORD}
          onTextChange={setPassword}
          securedTextEntry={true}
          autoCompleteType={'password'}
          textContentType={'password'}
        />
  • I had to maintain the state of secureTextEntry and change the value onFocus and onBlur to correctly get email suggestions on iOS (link)

iOS problem: If I set textContentType as username, I get asked to save the password on iOS but I don't get email suggestions the first time I am filling the login. If I set it to emailAddress, I get the suggestions but then I am not asked to save the password.

Android problem: Even with autoCompleteType and importantForAutofill set, I am never asked to save the password.

I tried to add a permission to AndroidManifest.xml <uses-permission android:name="android.permission.BIND_AUTOFILL_SERVICE" /> but it made no difference.

I have auto fill enabled on the settings of the two devices (android and ios) that I am using.

Does anyone have a suggestion on how to fix this? Thank you.

0 Answers
Related