1

I have built a loginPageGuard which simply not letting user lo login page if it is already logged in.

Lets say user is on

http://localhost:4200/#/testUrl

and tries to go to http://localhost:4200/#/login

LoginPageGuard works as i want it to and user stays on

http://localhost:4200/#/testUrl

The problem is when user is logged in and opens a new tab and enter following URL in browser

http://localhost:4200/#/login

App doesn`t let the user to login page but goes to

http://localhost:4200/#/

What i want is to take to user to

http://localhost:4200/#/someDesiredUrl 

Is there any way to achieve this ? Thanks

LoginPageGuard

@Injectable()
export class LoginPageGuard implements CanActivate {


  constructor(private router: Router, private authenticationService: AuthenticationService) {
  }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {

    return this.authenticationService.isAuthorized()
      .map((user:EUser)=>{
      return !user.isAuthorized;
    })
  }
}

app.routing.ts

  {
    path: 'login',
    canActivate:[LoginPageGuard],
    component: LoginComponent
  }
SAMUEL
  • 8,098
  • 3
  • 42
  • 42
rematnarab
  • 1,277
  • 4
  • 22
  • 42

1 Answers1

0

Just use the injected router instance to navigate to someDesiredUrl?

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
      //Authentication logic here
      //If already logged in
      this.router.navigateByUrl('someDesiredUrl');
}
Aamir Khan
  • 2,945
  • 2
  • 25
  • 32
  • 1
    I only want user to go to some desired URL if users opens a new tab and tries to go to login page. Otherwise the system works fine.. – rematnarab Nov 08 '17 at 12:23