The functionality of the Angular 5 router seems to be affected when handling the callback from Google login

I'm currently in the process of developing a web application that includes features like user login, a dashboard, and straightforward routing;

There are two options for logging in and routes in my app:

Login methods:

  1. Using email:

    login(u) {
        this._loginService.login(u)
           .subscribe(data => {
              console.log('Successfully logged in with email!');
              this._router.navigate(['/dashboard']);
           });
    
  2. Through Google:

    googleLogin() {
    gapi.auth2.authorize({
        client_id: *
        scope: 'email',
        response_type: 'code',
        prompt: 'select_account'
      },
      (response) => {
        if (response.error) {
          return;
        }
        this._loginService.loginWithGoogle(response)
          .subscribe(response1 => {
            console.log('Successfully logged in with Google!');
            console.log(response);
            this._router.navigate(['/dashboard']);
          });
      });
    
  3. Router configurations:

    const appRoutes: Routes = [
    { path: 'dashboard', component: MainComponent},
    { path: 'login', component: LoginComponent }

After successfully logging in via email, the router navigates without any issues.

The problem arises when logging in using Google sign-in and attempting to navigate to /dashboard, causing the app to crash in the following ways:

  1. Pressing a button to change routes only displays the view and does not remove my dashboard from the DOM.
  2. Sometimes, the login page remains in the DOM.
  3. Other functions in my code stop working.
  4. NO ERRORS IN CONSOLE

Refreshing the page resolves these issues and restores functionality.

Answer №1

If you want to resolve this issue, give this a try!

initialize(private area: NgZone) {
    ...
    ...

      this.area.run(() => {
         this._router.navigate(['/dashboard']);
       });

For more information, visit: Angular 2 ngOnInit not called

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

Exploring Angular 2's Child Routing

I am in the process of setting up routing for LocationComponent. My goal is to nest LocationComponent under FoodComponent, so that the URL appears as "localhost:4200/admin/food/location". How can I achieve this? Thank you. export const PanelRoutes: Rout ...

Issue: (SystemJS) the exports variable is not defined

In the process of developing a .net core mvc + angular application, I encountered an interesting situation. The MVC framework handles user management, and Angular takes over when users navigate to specific areas of the application. Initially, I integrated ...

Tips for implementing generics in an abstract class that extends a React Component

I am in the process of developing a unique abstract class that extends a React Component. My goal is to establish default Props while allowing the components that extend the abstract class to supply their own props. interface Props { ...

Retrieving 'this' within the catch clause following the execution of a promise

When using the this keyword within a catch clause after initiating a promise, it displays "this" as undefined. Here is my scenario: I have a service with a Router object injected into its constructor. From a method in the service, an HTTP request is made t ...

Encountering an undefined provider in Ionic 3 is a common issue that can often be attributed to the use of 'barrel' index.ts files, leading to circular dependencies

I've encountered an error in my Angular/TypeScript application that seems a bit useless. While we wait for a better error message, what can be done to address this issue? What are the common scenarios that lead to this error? Here are snippets from my ...

Error: Prettier is expecting a semi-colon in .css files, but encountering an unexpected token

I'm currently attempting to implement Prettier with eslint and TypeScript. Upon running npm run prettier -- --list-different, I encountered an error in all of my css files stating SyntaxError: Unexpected token, expected ";". It seems like there might ...

An issue within the component.ts file is preventing Angular from correctly rendering the content

As a newcomer to Angular, I encountered an issue when trying to run my angular app. Instead of displaying the content as expected, all I see is a blank page. Upon inspecting it, I noticed that the app-root element was empty. So, I decided to take a look at ...

Tips for invoking both a typescript arrow function and a regular javascript function within one event

Is it possible to call both a JavaScript function and a TypeScript function from the same onClick event in a chart? I am new to TypeScript and Angular, so I'm not sure if this is achievable. The issue at hand is that I need to invoke a JavaScript fun ...

Why is passing data:{} to a route essential for achieving the desired outcome?

Check out the Angular Material Documentation Site passing {} to the Homepage route: {path: '', component: HomePage, pathMatch: 'full', data: {}} I'm interested in knowing the significance of data: {}. Recent Discovery Closer ex ...

A guide on how to define prop types for mobx using TypeScript, React, and mobx

I have a fully functional root component structured like this const RootPage: React.FC = () => { const classes = useStyles(); return ( <React.Fragment> <Sidebar/> <Grid container className={classe ...

Angular: avoid API errors from being displayed in the HTML template

While working with HTTP requests in my Angular app, I encountered an issue where a status code of 400 is returned for invalid data input, causing the HTML template not to be displayed on the view. Currently, I am handling this situation by: this.myServic ...

What strategies should be followed for managing constant types effectively in TypeScript?

enum ENUM_POSITION_TYPE { LEFT = 1, RIGHT = 2 } // type PositionType = 1 | 2 type PositionType = ??? export let a1: PositionType = ENUM_POSITION_TYPE.RIGHT //correct export let a2: PositionType = 1 as const //correct export let a3: PositionType = 3 / ...

Angular - Implementing two-way data binding with observables in HTML

In the application I am currently developing, there is a top bar menu that displays user information. If the user is logged in, their picture and name are shown. If the user is not logged in, a "log in" button appears. However, to see these changes, the p ...

Making an Angular POST request and then navigating to a new component

Hello, I am a beginner in Angular and struggling to make my code work. It seems like a basic functionality issue that I can't seem to figure out. My goal is to send a post request to my .NET core API to register a user, and once that is done, navigat ...

Improved error messages for anticipated scenarios in Protractor

During my form testing, I utilized ExpectedConditions.and method to verify if all fields display the expected values. expect( await browser.wait( ec.and( ec.textToBePresentInElementValue(edit.driverFirstName, 'te ...

Exploring various formatter and linter settings for specific subdirectories within Visual Studio Code

I am working on a project that is organized as follows: project/ backend-in-python/ frontend-in-typescript/ .eslintrc.js .prettierrc.js ... Can anyone guide me on how to configure VSCode extensions (Python, ESlint, Prettier) to utilize the ...

Error encountered during conversion from JavaScript to TypeScript

I am currently in the process of converting JavaScript to TypeScript and I've encountered the following error: Type '(props: PropsWithChildren) => (any[] | ((e: any) => void))[]' is not assignable to type 'FC'. Type '(a ...

Exploring a Firestore collection to extract data fields for storage and utilization

I'm currently facing a challenge while attempting to access my Firestore collection and iterate through all documents to extract the valenceId field in each document. Despite trying various approaches, I keep encountering an error message stating: "c ...

Issue with dependencies: Incorrect value passed to `ts.resolveTypeReferenceDirective` causing a problem

This issue is really frustrating me. I'm currently working on this repository. Everything seems to be fine on the client side, but when it comes to the server side, I encountered the following problem: MacBook-Pro$ yarn dev yarn run v1.22.19 warning . ...

ReactJS Typescript Material UI Modular Dialog Component

Hello, I need help with creating a Reusable Material UI Modal Dialog component. It's supposed to show up whenever I click the button on any component, but for some reason, it's not displaying. Here's the code snippet: *********************TH ...