Firebase: signInWithEmailAndPassword failed: Second argument "password" must be a Valid string

Asked

Viewed 226 times

0

inserir a descrição da imagem aqui I’m getting a re-turn null for my second parameter along with signInWithEmailAndPassword method. This error inhibiting my authentication.

public autenticar(email: string, senha: string): void {
    firebase.auth().signInWithEmailAndPassword(email, senha)
        .then((resposta: any) => {
            firebase.auth().currentUser.getIdToken()
                .then((idToken: string) => {
                    this.token_id = idToken
                    localStorage.setItem('idToken', idToken)
                    this.router.navigate(['/home'])
                })
        })
        .catch((error: Error) => console.log(error))
}

Template code:

<form [formGroup]="formulario" (ngSubmit)="autenticar()">
  <div class="form-group">
    <input
      type="email"
      class="form-control"
      placeholder="E-mail"
      formControlName="email"
    >
  </div>
  <div class="form-group">
    <input
      type="password"
      class="form-control"
      placeholder="Senha"
      formGroupName="senha"
    >
  </div>
  <button type="submit" class="btn btn-primary btn-sm btn-block">Entrar</button>
</form>

1 answer

1

The error was occurring because in the app template formControlName for the "password" field, was set to formGroupName="password" and the correct would be formControlName="password", this occurred due to an oversight with auto-complete. It is worth learning to always check the structure of the reactive-form in the template to avoid this type of error.

 <div class="form-group">
    <input
      type="password"
      class="form-control"
      placeholder="Senha"
      formControlName="senha">
  </div>

Details about Reactive Forms on documentation angular officer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.