How to retrieve information from one httpOptions in another Angular component?

Asked

Viewed 49 times

0

I basically have a component user-login-component and in it I have the following function::

userLogin() {
    this.restApi.auth(this.userDetails).subscribe(dados => {
      var headers_object = new HttpHeaders();
      headers_object.append("Content-Type", "application/json");
      headers_object.append("X-token", "Basic" + dados);

      const httpOptions = {
        headers: headers_object
      };

      this.router.navigate(["main-page"]);
    });
  }

Whereas in the restApi.auth have:

auth(user): Observable<User> {
    return this.http
      .post<User>(this.apiURL + "/auth", JSON.stringify(user), this.httpOptions)
      .pipe(
        map(user => {
          // login successful if there's a jwt token in the response
          if (user && user.token) {
            // store user details and jwt token in local storage to keep user logged in between page refreshes
            localStorage.setItem("currentUser", JSON.stringify(user));
            this.currentUserSubject.next(user);
          }
          return user;
        })
      );
  }

It works and only logs in if there is a registration.

The problem is that I couldn’t use it in Component main-page something to recover the user.

I also know that on the page main-page I have access to a auth.jwt to check permissions on the page.

  • Dude, check this link, it has an example of how to get information via Components: https://answall.com/questions/383927/enviar-id-via-componentes-independentes-a-parti-de-um-link-e-emiter-angular-2

  • I’ve solved it, thank you

  • then puts the answer to us. To share with the crowd.

No answers

Browser other questions tagged

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