【问题标题】:How to retrieve a user identifier using ngOnInit with Angular 9如何使用 ngOnInit 和 Angular 9 检索用户标识符
【发布时间】:2021-03-24 22:39:33
【问题描述】:

我是 Angular 的新手,我正在创建一个忘记密码的链接,当用户与之交互时,它会向他们的电子邮件地址发送一封带有唯一密码令牌的电子邮件,以便他们可以重置密码。我希望能够识别用户,使用他们的用户标识符并将他们的电子邮件地址与此匹配并发送电子邮件。

  1. 是否需要将用户标识符或用户电子邮件地址传递到服务中?
  2. 如何构建 ngOnInit?

// user.service.ts

public forgotUserPasswordVerification(email: string): Observable<void> { // email or identifier?? 
        return this.http.post<void>('api/verifications/forgotpasswordverification', email);
    } 

//忘记密码.component.ts

ngOnInit() {
this.userService.forgotUserPasswordVerification(this.email) // this.email or this.identifier?  
.subscribe(data => {
this.success = true;
})};

【问题讨论】:

    标签: angular forgot-password ngoninit


    【解决方案1】:

    我认为不需要为此创建组件。尝试在 userService 中实现这个逻辑:

    将用户 ID 传递给您的 forgotUserPasswordVerification(userId) 方法,在其中您应该能够执行 2 个操作: 首先

    let userEmail= this.userService.getUserEmailById(this.id);
    

    然后

    this.userService.forgotUserPasswordVerification(this.userEmail)
          .pipe(first())
          .subscribe(
            () => {
              this.success = true;
            },
            (error) => {
              // display an error toast maybe
            }
          );
      }
    

    一旦用户点击链接,就在登录组件中调用你的 forgotUserPasswordVerification(userId) 方法。

    【讨论】:

      猜你喜欢
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多