Home > Enterprise >  Password must be at least 6 characters Firebase error in flutter
Password must be at least 6 characters Firebase error in flutter

Time:03-21

I am at my wits end. Code that worked before now stops working with no changes made. Main issue is with Firebase throwing error that password must be at least 6 characters. However, I have a validation of at least 8 characters on the password form field that is correctly validated before passing the value to the account registration module. That's when Firebase returns the error of "password must be at least 6 chars". How could this be; especially when this worked before and was able to create new users? Now I can no longer create an account. Here are the relevant code snippets and I thank you for your help.

Registration module:

 Future<User?> register(
    context,
     String name,
     String phone,
     String email,
     String password,
    String mediaUrl,
  ) async {
    var date = DateTime.now().toString();
    var parsedDate = DateTime.parse(date);
    var formattedDate =
        '${parsedDate.month}/${parsedDate.day}/${parsedDate.year}';
  

    try {
      await _read(firebaseAuthProvider).createUserWithEmailAndPassword(
        email: email.toLowerCase().trim(),
        password: password.trim(),
      );
      final User? user = _read(firebaseAuthProvider).currentUser;
      final _uid = user!.uid;

      await _read(firebaseFirestoreProvider).collection('users').doc(_uid).set(
        {
          'id': _uid,
          'name': name,
          'phone': phone,
          'email': email,
          'mediaUrl': '',
          'joinedDate': formattedDate,
          // 'createdAt': Timestamp.now(),
        },
      );
      Navigator.canPop(context) ? Navigator.pop(context) : null;
      // .then((value) =>
      //     Navigator.canPop(context) ? Navigator.pop(context) : null);
    } on FirebaseAuthException catch (e) {
      throw CustomException(message: e.message);
    }
  }

The form part relevant to password:

 TextFormField(
    key: const ValueKey('password'),
    controller: _passwordController,
    focusNode: _passwordFocusScope,
    // onEditingComplete: _submit,
    onEditingComplete: () => FocusScope.of(context)
        .requestFocus(_confirmFocusScope),
    obscureText: _isVisible.value,
    validator: (value) {
      if (value!.isEmpty || value.length < 8) {
        return 'Password cannot be empty and must be at least 8 chars!';
      }
      return null;
    },
    onSaved: (value) {
      _password = value!.trim();
    },
    textInputAction: _authMode.value == AuthMode.register
        ? TextInputAction.next
        : TextInputAction.done,
    onFieldSubmitted: (_) {
      FocusScope.of(context)
          .requestFocus(_confirmFocusScope);
    },
    decoration: kTextInputDecoration.copyWith(
      labelText: 'Password',
      labelStyle: TextStyle(
        fontSize: Device.screenType == ScreenType.mobile
            ? 16.sp
            : 18.sp,
      ),
      filled: true,
      fillColor: Theme.of(context).cardColor,
      prefixIcon: Icon(
        Icons.lock,
        size: Device.screenType == ScreenType.mobile
            ? 4.w
            : 6.w, //30,
      ),
      suffixIcon: IconButton(
        icon: _isVisible.value
            ? Icon(
                Icons.visibility_off,
                size: Device.screenType == ScreenType.mobile
                    ? 3.w
                    : 4.w, //30,
              )
            : Icon(
                Icons.visibility,
                size: Device.screenType == ScreenType.mobile
                    ? 3.w
                    : 4.w, //30,
              ),
        onPressed: () {
          _isVisible.value = !_isVisible.value;
        },
      ),
      contentPadding: const EdgeInsets.all(10),
    ),
  ),
  SizedBox(
    height: 1.8.h, //10,
  ),
  if (_authMode.value == AuthMode.register)
    TextFormField(
      focusNode: _confirmFocusScope,
      // controller: _passwordController,
      textInputAction: TextInputAction.done,
      // onFieldSubmitted: (_) {
      //   FocusScope.of(context)
      //       .requestFocus(_confirmFocusScope);
      // },
      // onEditingComplete: FocusScope.of(context)
      //     .unfocus(),
      key: const ValueKey('confirm'),
      obscureText: _isVisible.value,
      validator: (value) {
        if (value != _passwordController.text) {
          return 'Passwords do not match!';
        }
        return null;
      },
      decoration: kTextInputDecoration.copyWith(
        labelText: 'Confirm Password',
        labelStyle: TextStyle(
          fontSize: Device.screenType == ScreenType.mobile
              ? 16.sp
              : 18.sp,
        ),
        filled: true,
        fillColor: Theme.of(context).cardColor,
        prefixIcon: Icon(
          Icons.lock,
          size: Device.screenType == ScreenType.mobile
              ? 4.w
              : 6.w, //30,
        ),
        suffixIcon: IconButton(
          icon: _isVisible.value
              ? Icon(
                  Icons.visibility_off,
                  size:
                      Device.screenType == ScreenType.mobile
                          ? 3.w
                          : 3.5.w, //30,
                )
              : Icon(
                  Icons.visibility,
                  size:
                      Device.screenType == ScreenType.mobile
                          ? 3.w
                          : 4.w, //30,
                ),
          onPressed: () {
            _isVisible.value = !_isVisible.value;
          },
        ),
        contentPadding: const EdgeInsets.all(10),
      ),
    ),
  SizedBox(
    height: 1.8.h, //10,
  ),
  MyButton(
    text: _authMode.value == AuthMode.login
        ? 'Login'
        : 'Sign Up',
    txtColor: Colors.white,
    txtSize: Device.screenType == ScreenType.mobile
        ? 19.sp
        : 24.sp,
    bgColor: Colors.deepPurple,
    onTap: () {
      FocusScope.of(context)
          .unfocus(); // used to dismiss the keyboard
      // _submit();
      final _isValid = _formKey.currentState!.validate();
      if (_isValid) {
        _formKey.currentState?.save();
        _formKey.currentState?.reset();
        _authMode.value == AuthMode.login
            ? ref
                .read(authRepositoryProvider)
                .login(context, _email, _password)
            : ref.read(authRepositoryProvider).register(
                  context,
                  _name,
                  _email,
                  _password,
                  _mediaUrl,
                  _phone,
                );
        Navigator.pushReplacementNamed(
            context, BottomNavScreen.routeName);
      }
    },
  ),

Referenced providers:

final storageRefProvider = Provider<FirebaseStorage>((ref) => FirebaseStorage.instance);
final firebaseAuthProvider =
    Provider<FirebaseAuth>((ref) => FirebaseAuth.instance);
final firebaseFirestoreProvider =
    Provider<FirebaseFirestore>((ref) => FirebaseFirestore.instance);

SignUp Form

CodePudding user response:

This means that the password must be at least 6 characters long. Please read this.

CodePudding user response:

The issue is that the positional arguments as passed and value returned are out of order. The params were called in this order: name, phone, email, password, and mediaUrl. But when register function was called from the Sign Up button, phone value was passed after email which I think messed up what value was being returned for password (most likely a null value from mediaUrl. This resolved the problem.

However, another issue surfaced "Null operator used on a null value" which didn't affect the user creation. But looking at where this error is coming from. Thanks

  • Related