javascript - Angular2: how to "reload" page with router (recheck canActivate)? -
i have routers canactivate: [ authguard ]
, validation inside authguard
how force check canactivate
in same router url ?
for example: current route /admin
, have got event session expired
. have session check in authguard
check activates when execute .navigate(...)
. how force run canactivate
in same location?
i've tried: this.router.navigate([ this.router.url ]);
angular checks same location , nothing.
p.s. can locate "login page" or other pages when got session expired event
, have redirects inside authguard
, not want repeat same redirects in other events, need location.reload()
in angular2 routes.
main question sounds like: how force rerun canactivate
guards in current location?
my temporary solution:
auth.service.ts
import { injectable, injector } '@angular/core'; import { activatedroute, router, routerstatesnapshot } '@angular/router'; @injectable() export class authservice { constructor(private route: activatedroute, private router: router, private injector: injector) { this.forcerunauthguard(); } // dirty hack angular2 routing recheck private forcerunauthguard() { if (this.route.root.children.length) { // gets current route const curr_route = this.route.root.children[ '0' ]; // gets first guard class const authguard = curr_route.snapshot.routeconfig.canactivate[ '0' ]; // injects guard const authguard = this.injector.get(authguard); // makes custom routerstatesnapshot object const routerstatesnapshot: routerstatesnapshot = object.assign({}, curr_route.snapshot, { url: this.router.url }); // runs canactivate authguard.canactivate(curr_route.snapshot, routerstatesnapshot); } } }
app.routes.ts
{ path: 'faq', canactivate: [ authguard ], component: faqcomponent }, { path: 'about', canactivate: [ authguard ], component: aboutuscomponent }, { path: 'upgrade', canactivate: [ authguard ], component: upgradecomponent },
this code runs authguard
again.
Comments
Post a Comment