alim1990
alim1990

Reputation: 4972

Angular 6 Redirect to startup component of an application if a user changed his password using navigateByUrl is not working

I am currently at the following url of my angular 6 app:

http://localhost:4200/dashboard/admin/users

If an admin changed his credentials, i need to redirect him to login component again.

this.auth.updateCredentials(credentials).subscribe(
  (data) => {
    if (data == "success") {
      this.showError = false;
      if (credentials['user_id'] == this.loggedUser) {
        //Redirect
        localStorage.setItem('jwt', '')
        localStorage.setItem('user_id', '')
        localStorage.setItem('user_role', '')
        this.router.navigateByUrl('login');
      } else {
        this.dialogRef.close();
      }
    }
    if (data == "error") {
      this.showError = true;
    }

  },
  (error) => {
    this.showError = true;
    console.log(error);
  }
);

I tried this.router.navigateByUrl('login');, this.router.navigateByUrl('/login');

but no errors and no redirections

Upvotes: 0

Views: 69

Answers (3)

Rogelio
Rogelio

Reputation: 1094

if you're using

import { Router } from '@angular/router'

make sure you have it in your contructor as

constructor(private _router: Router) {}

and use the forward slash to specify a route

 this._router.navigateByUrl("/login")

perhaps that is what you where missing. I hope it helps.

Upvotes: 1

bhaumik shah
bhaumik shah

Reputation: 206

import import {Router} from '@angular/router'; and in constructor private router: Router

If you are passing your service request as a json than your code is like

this.auth.updateCredentials(credentials).subscribe(
  (data) => {
    if (data) {
      this.showError = false;
      if (credentials['user_id'] == this.loggedUser) {
        //Redirect
        localStorage.setItem('jwt', '')
        localStorage.setItem('user_id', '')
        localStorage.setItem('user_role', '')
         this.router.navigate(['your link']).then(() => {
                  your toaster
                });
      } else {
        this.dialogRef.close();
      }
    }
    if (data == "error") {
      this.showError = true;
    }

  },
  (error) => {
    this.showError = true;
    console.log(error);
  }
);

And if you are not parsing your data to json in advance then you have to do this

this.auth.updateCredentials(credentials).subscribe(
  (data) => {
    if (data['status'] == 200) {
      this.showError = false;
      if (credentials['user_id'] == this.loggedUser) {
        //Redirect
        localStorage.setItem('jwt', '')
        localStorage.setItem('user_id', '')
        localStorage.setItem('user_role', '')
        this.router.navigate(['your link']).then(() => {
                    your toaster
      } else {
        this.dialogRef.close();
      }
    }
    if (data == "error") {
      this.showError = true;
    }

  },
  (error) => {
    this.showError = true;
    console.log(error);
  }
);

Upvotes: 1

SiddAjmera
SiddAjmera

Reputation: 39452

Try this:

constructor(..., private router: Router, ...) {}
...
this.router.navigate(['/login']);
...

Upvotes: 1

Related Questions