0

So in my login component, I generate one token that gives me access to Tablaue dashboard from the angular app

so when I log in at that time only the home component method should call/run. but when I refresh the home page(component) should not run/call that method

Example

login-page.component.ts
public getToken(){
// i received token here
// then i move to the home page component
// saving token into the session
this.router.navigate(["/home"]);

}

home-page.component.ts
public  tokenAccess(){
// this method will be run only once when I come from the login page.
// should not run when the page is refreshed or reloaded
}

Thank you

Shree Batale
  • 227
  • 8
  • 20

1 Answers1

1

Expanding from your comment, you could send a query parameter in the route to denote if the token has indeed been fetched in the login-page component.

login-page.component.ts

public getToken(){
  this.router.navigate(["/home"], { queryParams: { token: true }});
}

The resulting routing URL would be of the form

http://example/home?token=true

The query parameter token would only be present when routed from the login-page component and not on page refresh or reload.

home-page.component.ts

import { ActivatedRoute } from '@angular/router';

constructor(private _actRoute: ActivatedRoute) { }

ngOnInit() {
  this._actRoute.queryParams.subscribe(params => {
    if (params['token']) {      // <-- condition would fail on page refresh/reload
      this.tokenAccess();
    }
  });
};
ruth
  • 29,535
  • 4
  • 30
  • 57