The redirection code is not being executed when calling .pipe() before .subscribe()

My AuthService has the following methods:

signUp = (data: SignUp): Observable<AuthResponseData> => {
  const endpoint = `${env.authBaseUrl}:signUp?key=${env.firebaseKey}`;
  return this._signInOrSignUp(endpoint, data);
};

signIn = (data: SignIn): Observable<AuthResponseData> => {
  const endpoint = `${env.authBaseUrl}:signInWithPassword?key=${env.firebaseKey}`;
  return this._signInOrSignUp(endpoint, data);
};

private _signInOrSignUp = (endpoint: string, data: SignIn | SignUp): Observable<AuthResponseData> => {
  return this.http.post<AuthResponseData>(endpoint, {
    ...data,
    returnSecureToken: true
  }).pipe(
    catchError(error => this._throwError(error)),
    tap(response => this._createAndEmitUserSubject(response)),
  );
}

private _throwError = error => {
  const errorMessage = error.error.error.message;
  return throwError(errorMessage);
};

private _createAndEmitUserSubject = (response: AuthResponseData) => {
  const expirationDate = new Date(new Date().getTime() + +response.expiresIn * 1000);
  const user = new User(
    response.idToken,
    response.email,
    response.idToken,
    expirationDate
  );

  localStorage.setItem("user", JSON.stringify(user));
  this.user$.next(user);
};

In my sign-in and sign-up components, I make use of these methods as shown below:

submit = (): void => {
  if (this.loginForm.invalid) {
    return;
  }

  this.authService
    .signIn(this.loginForm.value)
    .subscribe({
      next: () => this.router.navigate(["/recipes"]),
      error: error => this.error = error
    });
};

However, the code within next is not executed. It works when I remove .pipe(), but I was hoping to avoid using it in multiple places.

Answer №1

It seems that the function this._throwError is not actually throwing an error as expected. If it were, the function next would not be able to be called subsequently. It is likely that you are not using the throw statement to throw an error within the function, but rather returning a value that is then passed into next

Answer №2

After removing the line this.user$.next(user), I discovered that it was unnecessary all along. However, I am curious as to why it was blocking the flow.

private _createAndEmitUserSubject = (response: AuthResponseData) => {
  // ...

  localStorage.setItem("user", JSON.stringify(user));
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Struggling with implementing Angular and TypeScript in this particular method

I'm dealing with a code snippet that looks like this: myMethod(data: any, layerId: string, dataSubstrings): void { someObject.on('click', function(e) { this.api.getSomething(a).subscribe((result: any) => { // ERROR CALL 1. It ...

Problem with sidebar animation: Functioning properly when open, but closes abruptly without animation in React using Tailwind CSS

When interacting with my Menu react component by clicking on the 'hamburger' icon that I created manually, the sidebar menu opens smoothly with an animation. However, the issue arises when trying to close the sidebar as it vanishes instantly with ...

Encountering a mixed content error in Internet Explorer 8 due to the Nivo slider jQuery?

I am encountering an issue with the Nivo jQuery slider on my HTTPS website, as it appears to be generating a mixed content error in Internet Explorer 8. Despite posting on the Dev7 Studios forum and conducting extensive research on the IE 8 mixed content ...

Setting a blank value or null equivalent to a field: Tips and tricks

Here is the component state that I am working with: interface Person { name: string; surname: string; } interface CompState{ //...fields ... person?: Person; } render() { if(this.state.person){ const comp = <div>{this. ...

Access and retrieve pkpass files from a server using React

I've exhausted all options but still can't get it to work. I'm attempting to create an Apple wallet pass using https://github.com/walletpass/pass-js. When I download the pass on the node server where I've implemented it, everything work ...

Issue encountered during the creation of a new Angular application (npm ERROR! 404 Not Found: @angular/animations@~7.1.0.)

I am currently using Windows 10, Node v11.0.0, and Angular CLI: 7.1.4. I encountered an error when trying to create a new Angular application. The error message is npm ERR! code E404 npm ERR! 404 Not Found: @angular/animations@~7.1.0. Error stack: 0 info ...

Find all relevant employee information at once without the need for iteration

I have structured two JSON arrays for employee personal and company details. By inputting a value in the field, I compare both tables and present the corresponding employees' personal and company information in a unified table. <html> ...

Sending a POST request in Angular5 using the HTTP module

Angular 5 Attempting to create a function that will generate a resource on my API using Angular has proven to be a challenge. The "employee.service.ts" file contains a method named "saveEmployee" which is triggered by a function called "addEmployee" locate ...

Is there a way to make my red div switch its background color from red to green when I press the swap button?

How can I make the red div change its background color to toggle between red and green when I click on the swap button in the following code? $(document).ready(onReady); var numberOfClicks = 0; function onReady() { console.log('inside on ready ...

Angular promise did not assign a value to a variable

Can someone assist me with a problem I'm facing? After the first callback, the variable doesn't seem to change as expected. Below is my code snippet: this.handlerLocalDef = function(defer) { var hash = {}; defer.then( ...

How to Maintain Spacing Between Two Elements While Ensuring the Right Element Stays to the Right Even if the First One is Hidden in CSS

Presently, I have two icons being displayed with a space between them using the flex-box property justify-content and value space-between. The issue arises when only one icon is displayed, as I need the V-icon to always remain on the left and the urgent-ic ...

ngFor is failing to show the array data, which is being fetched from Firebase

Hi there, I understand that this question has been asked frequently, but the regular solutions are not working for me. ts handleChangeFormType(formType) { this.serverData.getData('questionnaire/' + formType) .subscribe( (response: Respons ...

Configuring bitfinex-api-node with Node.js to efficiently handle data from the websocket connection

Apologies for the vague title of this question, as I am not well-versed in programming and even less so in node.js My goal is simple: I aim to utilize the bitfinex-api-node package (a node.js wrapper for the bitfinex cryptocurrency exchange) that I instal ...

What could be causing my update function to not run when I refresh the page?

As a non-developer trying to learn react.js, I am working on a section of my website that needs to update every few seconds with new content like a photo, header, and body text. I've encountered an issue where the data refreshes correctly after the p ...

Tips for showcasing retrieved JSON with jQuery's ajax functionality

Below is the jquery code I am working with: $.ajax({ type: "POST", url: "Ajax/getTableRecord", data:{ i : id, t: 'mylist'}, dataType: 'json', success: function(data){ alert(data); ...

Tips for creating a new terminal window and interacting with it?

I have a node server that generates logs of activities in the terminal. To have more control over the server during runtime, I aim to integrate a repl server within it. However, I need to ensure that the logs and repl server do not interfere with each ot ...

"Troubleshooting: Resolving 404 Errors with JavaScript and CSS Files in a Vue.js and Node.js Application Deploy

I successfully developed a Vue.js + Node.js application on my local server. However, upon deploying it to a live server, I am facing a 404 error for the JavaScript and CSS files. I have double-checked that the files are indeed in the correct directories on ...

tips for patiently awaiting an ajax response before setting the object

I am currently working on a basic todo app using React. Initially, everything was running smoothly when I stored my data in a pre-defined object. However, now that I am retrieving my data from a link (rest) using AJAX, I seem to be encountering some issues ...

Bringing in CSS variables to a Typescript document

In order to streamline the styling process for our app, we have established a theme.css :root file containing a set of commonly used variables that can be accessed across all other .css files. However, some of our older code follows a similar structure bu ...

Are there any benefits to utilizing the Mantra.js architectural framework?

I have found that integrating Meteor.js into a Mantra.js architecture works seamlessly. However, I am questioning the advantages of using it since it seems to slow down the running of my requests. For example, when making a dummy request in GraphQL (such ...