angular - Angular2 CanActivate from beta to current RC3 -
i'm trying implement in angular2, auth0 login method video. using angular2-jwt
.
i read documentation, tried implement it, can't right.
tried export class navbarcomponent implements canactivate
, routercanactivate(){}
, canactivate(){}
. i'm stuck.
what changes needed canactivate work in current rc3?
import {canactivate} 'angular2/router'; import {tokennotexpired, jwthelper} 'angular2-jwt'; ... @canactivate (()=>tokennotexpired()) export class navbarcomponent{ ... }
see
- https://angular.io/docs/ts/latest/guide/router.html#!#can-activate-guard
- https://angular.io/docs/ts/latest/guide/router.html#!#can-deactivate-deactivate
create service class acts guard:
import { canactivate } '@angular/router'; export class authguard implements canactivate { canactivate() { console.log('authguard#canactivate called'); return true; } }
in new router supports dependency injection out of box
configure route use guard:
{ path: 'admin', component: crisisadmincomponent, canactivate: [authguard] },
the guard service can return observable resolves true or false
import { injectable } '@angular/core'; import { observable } 'rxjs/observable'; import 'rxjs/add/observable/of'; import 'rxjs/add/operator/do'; import 'rxjs/add/operator/delay'; @injectable() export class authservice { isloggedin: boolean = false; login() { return observable.of(true).delay(1000).do(val => this.isloggedin = true); } logout() { this.isloggedin = false; } }
the guard service needs provided other service. place example app_router_providers
export const app_router_providers = [ providerouter(routes), authguard ];
Comments
Post a Comment