CanActivate causing throttling history state error












0















Whenever I navigate to /about page my app ends up in an infinite loop with the error Throttling history state changes to prevent the browser from hanging.



Whenever I navigate to / page my app doesn't navigate to dashboard, it just displays a blank page.



I am expecting my page to navigate to /dashboard if a token is found and /home or / when it isn't.



Furthermore, I would like the remaining pages linked below to be inaccessible (apart from dashboard), if a token is found.



How can I fix this?



app.routing.component.ts



const routes: Routes = [
{ path: 'about', component: AboutComponent, canActivate: [AuthGuard] },
{ path: 'home', component: HomeComponent },
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
{ path: 'careers', component: CareersComponent },
{ path: 'contact', component: ContactComponent },
{ path: 'legal', component: LegalComponent },
{ path: 'login', component: LoginComponent },
{ path: 'press', component: PressComponent },
{ path: 'markets', component: MarketsComponent },
{ path: 'markets/:symbol', component: PriceComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'status', component: StatusComponent },
{ path: 'services', component: ServicesComponent },
{ path: 'support', component: SupportComponent },
{ path: 'support/account-management', component: AccountManagementComponent },
{ path: 'support/transactions', component: TransactionsComponent },
{ path: 'support/payment-methods', component: PaymentMethodsComponent },
{ path: 'support/security', component: SecurityComponent },
{ path: 'support/task-administration', component: TaskAdministrationComponent },
{ path: 'support/wallet-services', component: WalletServicesComponent },
{ path: '**', redirectTo: '', pathMatch: 'full' },
{ path: '', component: HomeComponent },
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule implements OnInit {
constructor() { }

ngOnInit() {
RouterModule.forRoot(routes, { scrollPositionRestoration: 'enabled' });
}
}


auth.guard.ts



@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService,
private router: Router) {
}

canActivate(
_next: ActivatedRouteSnapshot,
_state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (this.authService.isLoggedIn()) {
this.router.navigate(['dashboard']);
if (environment.production === false) {
console.log('Token found, redirecting to dashboard');
}
return true;
} else {
this.router.navigate(['home']);
if (environment.production === false) {
console.log('Token not found, redirecting to home');
}
return false;
}
}
}


auth.service.ts



@Injectable()
export class AuthService {
constructor(private myRoute: Router) { }

getToken() {
return localStorage.getItem('token');
}

isLoggedIn() {
return this.getToken() !== null;
}

logout() {
localStorage.removeItem('token');
this.myRoute.navigate(['login']);
}
}









share|improve this question























  • Only navigate to a route if it is not equal to your current route,

    – Silvermind
    Nov 23 '18 at 16:30











  • Sorry I don’t understand. How does that work?

    – methuselah
    Nov 23 '18 at 22:16
















0















Whenever I navigate to /about page my app ends up in an infinite loop with the error Throttling history state changes to prevent the browser from hanging.



Whenever I navigate to / page my app doesn't navigate to dashboard, it just displays a blank page.



I am expecting my page to navigate to /dashboard if a token is found and /home or / when it isn't.



Furthermore, I would like the remaining pages linked below to be inaccessible (apart from dashboard), if a token is found.



How can I fix this?



app.routing.component.ts



const routes: Routes = [
{ path: 'about', component: AboutComponent, canActivate: [AuthGuard] },
{ path: 'home', component: HomeComponent },
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
{ path: 'careers', component: CareersComponent },
{ path: 'contact', component: ContactComponent },
{ path: 'legal', component: LegalComponent },
{ path: 'login', component: LoginComponent },
{ path: 'press', component: PressComponent },
{ path: 'markets', component: MarketsComponent },
{ path: 'markets/:symbol', component: PriceComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'status', component: StatusComponent },
{ path: 'services', component: ServicesComponent },
{ path: 'support', component: SupportComponent },
{ path: 'support/account-management', component: AccountManagementComponent },
{ path: 'support/transactions', component: TransactionsComponent },
{ path: 'support/payment-methods', component: PaymentMethodsComponent },
{ path: 'support/security', component: SecurityComponent },
{ path: 'support/task-administration', component: TaskAdministrationComponent },
{ path: 'support/wallet-services', component: WalletServicesComponent },
{ path: '**', redirectTo: '', pathMatch: 'full' },
{ path: '', component: HomeComponent },
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule implements OnInit {
constructor() { }

ngOnInit() {
RouterModule.forRoot(routes, { scrollPositionRestoration: 'enabled' });
}
}


auth.guard.ts



@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService,
private router: Router) {
}

canActivate(
_next: ActivatedRouteSnapshot,
_state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (this.authService.isLoggedIn()) {
this.router.navigate(['dashboard']);
if (environment.production === false) {
console.log('Token found, redirecting to dashboard');
}
return true;
} else {
this.router.navigate(['home']);
if (environment.production === false) {
console.log('Token not found, redirecting to home');
}
return false;
}
}
}


auth.service.ts



@Injectable()
export class AuthService {
constructor(private myRoute: Router) { }

getToken() {
return localStorage.getItem('token');
}

isLoggedIn() {
return this.getToken() !== null;
}

logout() {
localStorage.removeItem('token');
this.myRoute.navigate(['login']);
}
}









share|improve this question























  • Only navigate to a route if it is not equal to your current route,

    – Silvermind
    Nov 23 '18 at 16:30











  • Sorry I don’t understand. How does that work?

    – methuselah
    Nov 23 '18 at 22:16














0












0








0








Whenever I navigate to /about page my app ends up in an infinite loop with the error Throttling history state changes to prevent the browser from hanging.



Whenever I navigate to / page my app doesn't navigate to dashboard, it just displays a blank page.



I am expecting my page to navigate to /dashboard if a token is found and /home or / when it isn't.



Furthermore, I would like the remaining pages linked below to be inaccessible (apart from dashboard), if a token is found.



How can I fix this?



app.routing.component.ts



const routes: Routes = [
{ path: 'about', component: AboutComponent, canActivate: [AuthGuard] },
{ path: 'home', component: HomeComponent },
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
{ path: 'careers', component: CareersComponent },
{ path: 'contact', component: ContactComponent },
{ path: 'legal', component: LegalComponent },
{ path: 'login', component: LoginComponent },
{ path: 'press', component: PressComponent },
{ path: 'markets', component: MarketsComponent },
{ path: 'markets/:symbol', component: PriceComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'status', component: StatusComponent },
{ path: 'services', component: ServicesComponent },
{ path: 'support', component: SupportComponent },
{ path: 'support/account-management', component: AccountManagementComponent },
{ path: 'support/transactions', component: TransactionsComponent },
{ path: 'support/payment-methods', component: PaymentMethodsComponent },
{ path: 'support/security', component: SecurityComponent },
{ path: 'support/task-administration', component: TaskAdministrationComponent },
{ path: 'support/wallet-services', component: WalletServicesComponent },
{ path: '**', redirectTo: '', pathMatch: 'full' },
{ path: '', component: HomeComponent },
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule implements OnInit {
constructor() { }

ngOnInit() {
RouterModule.forRoot(routes, { scrollPositionRestoration: 'enabled' });
}
}


auth.guard.ts



@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService,
private router: Router) {
}

canActivate(
_next: ActivatedRouteSnapshot,
_state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (this.authService.isLoggedIn()) {
this.router.navigate(['dashboard']);
if (environment.production === false) {
console.log('Token found, redirecting to dashboard');
}
return true;
} else {
this.router.navigate(['home']);
if (environment.production === false) {
console.log('Token not found, redirecting to home');
}
return false;
}
}
}


auth.service.ts



@Injectable()
export class AuthService {
constructor(private myRoute: Router) { }

getToken() {
return localStorage.getItem('token');
}

isLoggedIn() {
return this.getToken() !== null;
}

logout() {
localStorage.removeItem('token');
this.myRoute.navigate(['login']);
}
}









share|improve this question














Whenever I navigate to /about page my app ends up in an infinite loop with the error Throttling history state changes to prevent the browser from hanging.



Whenever I navigate to / page my app doesn't navigate to dashboard, it just displays a blank page.



I am expecting my page to navigate to /dashboard if a token is found and /home or / when it isn't.



Furthermore, I would like the remaining pages linked below to be inaccessible (apart from dashboard), if a token is found.



How can I fix this?



app.routing.component.ts



const routes: Routes = [
{ path: 'about', component: AboutComponent, canActivate: [AuthGuard] },
{ path: 'home', component: HomeComponent },
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
{ path: 'careers', component: CareersComponent },
{ path: 'contact', component: ContactComponent },
{ path: 'legal', component: LegalComponent },
{ path: 'login', component: LoginComponent },
{ path: 'press', component: PressComponent },
{ path: 'markets', component: MarketsComponent },
{ path: 'markets/:symbol', component: PriceComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'status', component: StatusComponent },
{ path: 'services', component: ServicesComponent },
{ path: 'support', component: SupportComponent },
{ path: 'support/account-management', component: AccountManagementComponent },
{ path: 'support/transactions', component: TransactionsComponent },
{ path: 'support/payment-methods', component: PaymentMethodsComponent },
{ path: 'support/security', component: SecurityComponent },
{ path: 'support/task-administration', component: TaskAdministrationComponent },
{ path: 'support/wallet-services', component: WalletServicesComponent },
{ path: '**', redirectTo: '', pathMatch: 'full' },
{ path: '', component: HomeComponent },
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule implements OnInit {
constructor() { }

ngOnInit() {
RouterModule.forRoot(routes, { scrollPositionRestoration: 'enabled' });
}
}


auth.guard.ts



@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService,
private router: Router) {
}

canActivate(
_next: ActivatedRouteSnapshot,
_state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (this.authService.isLoggedIn()) {
this.router.navigate(['dashboard']);
if (environment.production === false) {
console.log('Token found, redirecting to dashboard');
}
return true;
} else {
this.router.navigate(['home']);
if (environment.production === false) {
console.log('Token not found, redirecting to home');
}
return false;
}
}
}


auth.service.ts



@Injectable()
export class AuthService {
constructor(private myRoute: Router) { }

getToken() {
return localStorage.getItem('token');
}

isLoggedIn() {
return this.getToken() !== null;
}

logout() {
localStorage.removeItem('token');
this.myRoute.navigate(['login']);
}
}






angular






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 15:47









methuselahmethuselah

5,01834105189




5,01834105189













  • Only navigate to a route if it is not equal to your current route,

    – Silvermind
    Nov 23 '18 at 16:30











  • Sorry I don’t understand. How does that work?

    – methuselah
    Nov 23 '18 at 22:16



















  • Only navigate to a route if it is not equal to your current route,

    – Silvermind
    Nov 23 '18 at 16:30











  • Sorry I don’t understand. How does that work?

    – methuselah
    Nov 23 '18 at 22:16

















Only navigate to a route if it is not equal to your current route,

– Silvermind
Nov 23 '18 at 16:30





Only navigate to a route if it is not equal to your current route,

– Silvermind
Nov 23 '18 at 16:30













Sorry I don’t understand. How does that work?

– methuselah
Nov 23 '18 at 22:16





Sorry I don’t understand. How does that work?

– methuselah
Nov 23 '18 at 22:16












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449570%2fcanactivate-causing-throttling-history-state-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449570%2fcanactivate-causing-throttling-history-state-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin