The code below is not working as it should be to redirect to the home page after logging in using Angular. Follow these steps to troubleshoot and properly

When looking at this snippet of code:

this.router.navigate(['/login'],{queryParams:{returnUrl:state.url}});

An error is displayed stating that "Property 'url' does not exist on type '(name: string, styles: AnimationStyleMetadata". Below you can find the import statements and a segment of code from an Angular AuthService:

import { Injectable } from "@angular/core";
import { GoogleAuthProvider } from 'firebase/auth';
import { AngularFireAuth } from '@angular/fire/compat/auth';
import { Observable } from "rxjs/internal/Observable";
import firebase from 'firebase/compat/app';
import { Router, RouterLink, RouterStateSnapshot } from "@angular/router";
import { startAfter } from "@angular/fire/firestore";
import { state } from "@angular/animations";


@Injectable({ providedIn: 'root' }) 
    
export class AuthService {
  user$: Observable<firebase.User|null> | undefined;

  constructor(private afAuth:AngularFireAuth, private router:Router) {
    this.user$ = afAuth.authState;
   }
  login() {
    this.afAuth.signInWithRedirect(new GoogleAuthProvider());
  }
  logout(){
    this.afAuth.signOut();
  }
  isLoggedIn(){
    this.user$?.subscribe(user =>{
      if(user) return true;

      this.router.navigate(['/login'],{queryParams:{returnUrl: state.url}});
      return false;
    })
  }
}

Answer №1

You have called upon the state object, which was brought in from the animation module, yet it lacks the url property.

Instead of using state.url, consider utilizing this.router.url:

this.router.navigate(['/login'],{queryParams:{returnUrl: this.router.url});

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

Update WooCommerce Mini-cart with ajax refresh

I'm having an issue with my custom plugin where everything is working properly, except for the fact that the mini cart is not updating after adding items. I have tried various methods to trigger a refresh, but so far nothing has worked. Below is a sni ...

I am currently working on developing a web application for a class assignment and I am unsure whether I should opt for PHP7 Non Thread Safe or Thread Safe version

While I consider myself a beginner in the realm of programming, my knowledge is limited to Python. However, I am eager to explore using PHP7+ and Typescript with Angular 2+. I've come across recommendations for these languages online, but I'm uns ...

Creating an error handler in PHP for dynamically adding or removing input fields is a crucial step in ensuring smooth and

I designed a form with multiple fields, including a basic input text field and dynamic add/remove input fields. I successfully set the first field as required using PHP arguments, but I'm struggling to do the same for the additional fields. I need as ...

Create a new instance of the parent class in TypeScript to achieve class inheritance

Looking for a solution to extending a base class Collection in JavaScript/TypeScript to handle domain-specific use cases by implementing a "destructing" method like filter that returns a new instance with filtered elements. In PHP, you can achieve this usi ...

The login using NextAuth Credentials does not capture the username in the session

Recently, I integrated NextAuth Credentials provider into my app for authentication purposes. So far, I have been following this helpful tutorial here. However, even after successfully signing in the user, when attempting to access the user info in the se ...

The surprising twist of hasOwnProperty's behavior

I am currently working on a function that is designed to check whether an object contains keys such as 'id' or 'serif:id'. However, I have encountered some issues with its functionality. function returnIdPreferSerifId(object) { if ...

When attempting to execute a script that includes document.write, it will not function as expected

Our web program is utilizing ajax and jquery, specifically Netsuite. I've been attempting to adjust elements on a page using document.ready and window.load methods in order to load an external script onto the page. Regardless of whether I load this ex ...

Discover the power of native script's two-way data binding in your classes

Is there a way to implement two-way binding in Nativescript? Here is my attempt: The variable CompModel is set to "FA I Test". I am looking for a solution where the data can be bound both ways - initially displaying the value "FA I Test" at the class lev ...

Implementing a sibling combinator technique within Material UI's useStyles framework

Is there a way to change the background of one div when hovering over another using material ui? The traditional CSS method is: #a:hover ~ #b { background: #ccc; } Here is my attempt with Material UI: const useStyles = makeStyles(theme => ({ ...

How is it possible that this event listener is able to capture an event that was already sent before it was

I am facing an issue with my Vue JS code. When I click on the account <a> tag, it triggers the toggle method. The toggle method adds an event listener to the document. However, as soon as the toggle method is executed, the event listener fires and ...

Having an issue with discord.js and node.js modules not exporting correctly? The returned statement is not functioning as

Trying to retrieve specific messages from a channel to gather information about registered 'clans' has been quite challenging for me. I am able to fetch the channel, filter and loop through the messages, but strangely, I cannot return the fetched ...

Retrieve the weekday dates for a specific year, month, and relative week number using Javascript or Typescript

I am in need of a custom function called getDaysOfWeekDates that can take a year, a month (ranging from 0 to 11), and the week number of each month (usually 4-5 weeks per month) as parameters, and return a list of dates containing each day of that particul ...

The Angular 2 routerLink doesn't update the component after the initial click, even though the URL changes in the browser

When using Angular 2, I encountered an issue where clicking a routerLink in the App module successfully navigates to a parameterised route (e.g. /events/2) and loads the correct component (event-details) on the initial click. However, subsequent clicks on ...

Error message: Unable to modify the 'cflags' property of object '#<Object>' in Angular CLI and node-gyp

@angular/cli has a dependency on node-gyp, which is evident from the following: npm ls node-gyp <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="16776666653b7477757d7970707f7573562738263826">[email protected]</a> /h ...

What methods does Angular use to display custom HTML tags in IE9/10 without causing any issues for the browser?

Exploring web components and utilizing customElements.define tends to cause issues in IE9/10. I've noticed that Angular is compatible with IE9/10, and upon inspecting the DOM tree, it seems like Angular successfully displays the custom element tags. ...

The API response was blocked due to the CORS header "Access-control-allow-origin."

I have an index.html file that is used for making HTML and Ajax calls. When I run the file through localhost, the API works perfectly. However, if I directly open the index.html file in Google Chrome or Mozilla Firefox, I encounter a CORS (Cross-Origin Re ...

D3.js Issue: The <g> element's transform attribute is expecting a number, but instead received "translate(NaN,NaN)"

I encountered an error message in my console while attempting to create a data visualization using d3. The specific error is as follows: Error: <g> attribute transform: Expected number, "translate(NaN,NaN)". To build this visualization, I ...

How can I prevent all permission requests in Electron Security?

I'm currently in the process of enhancing the security of my Angular/Electron application. For this purpose, I decided to utilize electrongravity which has proven to be effective in identifying misconfigurations and prompting me to establish a permis ...

How can you establish a default value on a paper select in Polymer?

I have a paper-select element that I want to customize with a default value when the page loads or after a specific event occurs. <dom-module id="custom-paper-select"> <template> <paper-select id="select-input-1" multiple ...

Welcome to the JavaScript NodeJs Open Library!

I am trying to open multiple images simultaneously in the default Windows Photo Viewer that I have stored in my folder using the npm open library: let files = ['Dog.gif', 'Cat.jpeg']; for(let i=0; i<files.length; i++){ open(`${file ...