Skip to content

Instantly share code, notes, and snippets.

@jorgeunimicro
Created January 3, 2016 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgeunimicro/43c17a7c8a4b51143f25 to your computer and use it in GitHub Desktop.
Save jorgeunimicro/43c17a7c8a4b51143f25 to your computer and use it in GitHub Desktop.
rewrite angular2 outlet
port {Directive, Attribute, ElementRef, DynamicComponentLoader} from 'angular2/core';
import {Router, RouterOutlet, ComponentInstruction} from 'angular2/router';
import {AuthService} from '../framework/authentication/authService'
import {Login} from './components/login/login';
@Directive({
selector: 'uni-router-outlet',
providers: [AuthService]
})
export class UniRouterOutlet extends RouterOutlet {
private parentRouter:Router;
private authService: AuthService;
constructor(elementRef:ElementRef, loader:DynamicComponentLoader, parentRouter:Router, @Attribute('name') nameAttr:string, authService: AuthService) {
super(elementRef, loader, parentRouter, nameAttr);
this.parentRouter = parentRouter;
this.authService = authService;
}
activate(instruction: ComponentInstruction) {
var url = '/' + instruction.urlPath;
if (url !== '/login' && !this.authService.authenticated) {
localStorage.setItem('lastNavigationAttempt', url); // so we can redirect to it after loggin in
this.parentRouter.navigateByUrl('/login');
}
return super.activate(instruction);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment