Hitting url directly takes again to login page even after login Angular

Firebase authenticaion is integrated in an angular app. Even after logged in to the app, hitting a url or route directly at a new tab or the same tab of browser, chrome, e.g. http://localhost:4200/landing takes again to the login page.

Is there a way around not to skip login page if it is already log in.

Steps to reproduce

  1. ng serve in the angular CLI project takes me to http://localhost:4200/login
  2. After login it takes to http://localhost:4200/landing
  3. Open a new tab or in the existing tab type, http://localhost:4200/landing hit enter

Expected: It stays at http://localhost:4200/landing

Actual: It goes back to http://localhost:4200/login

1 answer

  • answered 2018-07-11 02:58 Vicky Kumar

    What i understood from your question is you want to navigate to login page initially, once user is already logged it you want to go to the route which ever he has entered.If not looged in go to login page You should use localstorage, if you want to preserve any data in browser for new tab or new window Once you login store some token in local storage. like this

    localStorage.setItem('userId', 'USERID');

    Now in your appComponent on init check if local storage doesnot exist then only direct to login.

      ngOnInit() {
    if (!localStorage.getItem('userId')) {
      this.router.navigate(['/login']); // i think initially you are redirecting to login so put condition there in app component

    Note: Be careful this will even preserve once you close the browser