2

In my app

I have loginComponent, UserService

By default user will go to login page

There he can type his credentials and I am making an API call and using the retrieved userdata to populate one property in the service class

After this, I am redirecting the user to the dashboard (Now service is working fine. It has user data )

But now if I go to browse URL bar and manually change the URL to login the app is loading loginComponent and data inside service has lost. (probably creating new instance of service)

Is this a bug...? or can we get rid of this.

  • 7
    It's not just creating a new instance of service, it's recreating the whole app - think of it like restarting the app. If you need data to persist, you'll want to consider storing it somewhere like cookies, localStorage, etc. – Kirk Larkin Aug 22 '18 at 07:28
  • 2
    this is not a bug, this is the way how angular behaves, it just recreates whole app, so you results in lost in states, to persist data you can use localStorage, cookies, session – Prasanna Aug 22 '18 at 07:33

1 Answers1

1

There is no way to lock the url bar or prevent manual navigation.

But you can try to catch such an issues in your Angular code and warn the visitor (with a popup) to not do this. I found an other question where a possible solution is suggested.

But you can also use localStorage (as mentioned by Kirk Larkin and Prasanna Sasne) to hold the login info.

Stef Geysels
  • 1,023
  • 11
  • 27