How to retrieve a user identifier using ngOnInit with Angular 9

182 Views Asked by At

I'm new to Angular and I'm creating a forgot password link, which when the user interacts with it will send out an email to their email address with a unique password token in order so they can reset their password. I want to be able to recognize the user, using their user identifier and match their email address with this and send out the email.

  1. Do I need to pass in the user identifier or user email address into the service?
  2. How do I structure the ngOnInit?

// user.service.ts

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

// forgot-password.component.ts

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

There are 1 best solutions below

0
On BEST ANSWER

I don't see a need to create a component for this. Try to implement this logic inside the userService:

Pass the user ID to your forgotUserPasswordVerification(userId) method, inside it you should be able to do 2 operations: first

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

then

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

And call this your forgotUserPasswordVerification(userId) method in the login component once the user click the link.