Return back to url after login

86 views Asked by At

I want to make users return back to previous URL after login like if you search any hotels on oyorooms.com or fabhotels.com and then suddenly you login there.

After login you are able to see the previous page or say the previous activity like what you are searching at the site. So, I want to do that in my Angular application.

How to do that?

1

There are 1 answers

0
Brijesh On

One thing you can do in this condition, save your previous url in service or localStorage and redirect after login page to the saved previous url.

In my case, I have handled like this:

and In guard :

 canActivate(
    return this.checkAuth();
  }

checkAuth(): Observable<boolean> {
    if (this.isUserLogged()) {

      return of(true);
    } else {
      const url = window.location.pathname;
      if (
        url === '/setting'
      ) {
        this.router.navigate(['/home']);
        return of(false);
      } else {

        return this.openLoginModal();
      }
      return false;
    }
  }


  openLoginModal(): Observable<boolean> {
    this.bsModalRef = this.modalService.show(LoginComponent, {
      class: 'modal-lg registerpopup'
    });
    this.bsModalRef.content.title = 'New Client';

    return this.bsModalRef.content.loginEmitter.pipe(
      tap((isLoggedIn: boolean) => {
        alert(isLoggedIn);
        this.isLoggedIn = isLoggedIn;
        this.bsModalRef.hide();
        this.bsModalRef = null;
      })
    );
  }