当前位置: 首页 > 知识库问答 >
问题:

用于显示登录错误消息的角度表单验证

谢宸
2023-03-14

所以我想尝试在登录页面上显示“无效用户名或密码”的错误信息,每当用户得到他们是错误的。

signinUser(email: string, password: string) {
    firebase.auth().signInWithEmailAndPassword(email, password).then(
        response => {
            this.router.navigate(['/recipes']);
            firebase.auth().currentUser.getIdToken()
            .then(
                (token: string) => this.token = token
            );
        }
    )
    .catch (
        error => console.log(error)

    );
}
import { NgForm } from '@angular/forms';
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../auth.service';

@Component({
  selector: 'app-signin',
  templateUrl: './signin.component.html',
  styleUrls: ['./signin.component.css']
})
export class SigninComponent implements OnInit {


  constructor(private authService: AuthService,) { }

  ngOnInit() {
  }

  onSignIn(form: NgForm) {
    const email = form.value.email;
    const password = form.value.password;
    this.authService.signinUser(email, password);

  }

}
<div class="row">
    <div class="col-xs-12 col-sm-10 col-md-8 col-sm-offset-1 col-md-offset-2">
      <h1 class="display-3" style="text-align: center"> Login  </h1>
    <form  (ngSubmit)="onSignIn(f)" #f ="ngForm"  >
      <div class="form-group" style="margin-top: 20px">
        <label for="email"> Email </label> <!--for firebase you auth with email and password -->
        <input type="email" name = "email" class="form-control" id="EmailId" ngModel required>
      </div>
      <div class="form-group"  style="margin-top: 20px">
        <label for="password"> Password </label> <!--for firebase you auth with email and password -->
        <input type="password" name = "password" class="form-control" id="Password" ngModel required>
        <button style="margin-top: 20px" class="btn btn-primary" type="submit" [disabled]="!f.valid"> Sign In </button>
      </div>
    </form>
    </div>
  </div>

到目前为止,我只是将它记录在控制台中,在那里我看到错误:“auth/invalce-email”,其消息属性是:“电子邮件地址格式化得很糟糕。”当输入错误的电子邮件时(密码也是如此)。有什么方法可以在我的singin组件的html中的ngIf div元素中显示这些(或者更确切地说是自定义消息“无效的用户名或密码”)消息,这些消息来自在我的auth.service中捕获的错误?

共有1个答案

任繁
2023-03-14

是的。你可以,你有两种方法做这件事。

首先,您应该在组件上执行然后,如下所示:

您的服务:

signinUser(email: string, password: string) {
   return firebase.auth().signInWithEmailAndPassword(email, password)      
}
onSignIn(form: NgForm) {
    const email = form.value.email;
    const password = form.value.password;
    this.authService.signinUser(email, password)
     .then(response => {
        this.router.navigate(['/recipes']);
        firebase.auth().currentUser.getIdToken()
         .then(
            (token: string) => this.token = token
        );
     })
     .catch (
         error => this.errorMessage = error.message;
    );
}

您的服务:

import {Subject} from 'rxjs/Subject';

     private logInErrorSubject = new Subject<string>();

     public getLoginErrors():Subject<string>{
            return this.logInErrorSubject;
     }

     signinUser(email: string, password: string) {
         firebase.auth().signInWithEmailAndPassword(email, password).then(
         response => {
           this.router.navigate(['/recipes']);
           firebase.auth().currentUser.getIdToken()
           .then(
              (token: string) => this.token = token
           );
          }
      )
     .catch (
         error => this.logInErrorSubject.next(error.message);

     );
      }

您的组件:

     private errorMessage:string;

     constructor(private signupService:SignupService){
        this.signupService.getLoginErrors().subscribe(error => {
               this.errorMessage = error;
          });
     }
 类似资料:
  • 我试图让我的javascript验证电话号码输入,并在用户输入信息不正确时显示消息。我还设置了另外两个输入,所以我复制了其中一个,并用phoneNumber替换了名称,但没有结果。没有HTML的替代品,请它是我必须使用我的评估。 这里我只包含了电话号码输入的代码。http://jsfiddle.net/29ZNV/ 超文本标记语言 JAVASCRIPT

  • 我正在将Spring jsp应用程序迁移到Thymeleaf,但在显示表单错误时遇到问题。 当我尝试使用以下方法显示错误时: 它不会显示任何错误。 我尝试了http://www.thymeleaf.org/doc/html/thymeleaf-spring3.html#validation-and-error-messages#validation-and-error-messages上的各种替代

  • 我需要创建一个验证指令来自动显示每个输入的所有输入错误。此验证指令应该显示当前时刻的所有错误,错误列表应该在用户键入时自动更新。 链接到JSFiddle,带有一些用于测试的代码。 附言。UI-UTIL也有类似的功能,但它们的指令不能在一个地方设置类似的错误消息。

  • 我正在尝试为wordpress开发一个登录ajax表单。这个插件似乎工作得很好。如果我插入了正确的用户名和密码,登录将正常工作并显示正确的消息,然后重定向到正确的页面,但是如果我插入了错误的用户名和密码值,则不会发生任何情况,错误消息也不会出现。 似乎该函数是_wp_error,不回显该错误。 你知道为什么吗?低于我的代码 感谢在adv. PHP 超文本标记语言 JS

  • 因此,很明显,它识别出输入的名称太短,但我在视图中没有得到任何错误消息,正如这里所预期的:。