Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 5 - redirect the previous page after sign in

Angular 5 redirect to previous page after sign in.

When I am hitting a url manually, if it is not logged in, it will redirect to login page but after login, it should redirect to url which entered but now I just can redirect to homepage by using this:

this.router.navigate(['/homepage']);

Have some ideas?

like image 590
shakell Avatar asked Jun 04 '18 07:06

shakell


3 Answers

When you redirect to the login page with a guard, you could put the source url in the queryParams as such:

canActivate(next: ActivatedRouteSnapshot,
            state: RouterStateSnapshot) {
    if (this.authService.isAuthenticated()) {
        return true;
    } else {
        console.log('Could not authenticate');
        this.router.navigate(['login'],{queryParams:{'redirectURL':state.url}});
        return false;
    }
}

After you log in you can get the original page url from the query params:

let params = this.route.snapshot.queryParams;
    if (params['redirectURL']) {
        this.redirectURL = params['redirectURL'];
    }

...

if (this.redirectURL) {        
    this.router.navigateByUrl(this.redirectURL,)
        .catch(() => this.router.navigate(['homepage']))
} else {

    this.router.navigate(['homepage'])
}
like image 125
Venomy Avatar answered Sep 21 '22 07:09

Venomy


You can send the URL before going to the homepage and use it there

@Component({ ... })
class SomePageComponent {
  constructor(private route: ActivatedRoute, private router: Router) {}
  checkLogin() {
    if (!this.auth.loggedIn()) {
      this.router.navigate(['/homepage`], { queryParams: { redirectTo: this.route.snapshot.url } });
    }
  }
}

So then in your login Component you can access to the queryParams like this

@Component({...})
class LoginComponent {
  constructor(private router: Router, private route: ActivatedRoute) {}

  backToPreviousPage() {
    this.router.navigate(
         [this.route.snapshot.queryParams.get('redirectTo')]
    );
  }
}
like image 30
nicowernli Avatar answered Sep 21 '22 07:09

nicowernli


for me queryParams not work properly because if the user goes to log in, and next go to the signup route, so you need to preserve query params. But if you have more steps before login, or user refreshes the page, or the internet was bad on a subway or something else happened and he lost his params and will be redirected to who knows... But if you set needed URL to redirect to localStorage and after login/signup/someelse step you can redirect him to needed URL previously delete the string from localStorage.

// needed page
localStorage.setItem('redirectTo', this.router.url);
this.router.navigateByUrl('/login');
// login/signup/else step page
success => {
  const redirect = localStorage.getItem('redirectTo');
  if (redirect) {
    localStorage.removeItem('redirectTo');
    this.router.navigate(redirect);
  } else {
    this.router.navigate('defaultpath');
  }
}
like image 39
Kurkov Igor Avatar answered Sep 22 '22 07:09

Kurkov Igor