Skip to content

Commit

Permalink
Replace router outlet override with @CanActivate check
Browse files Browse the repository at this point in the history
  • Loading branch information
timdp committed Mar 13, 2016
1 parent e7d473d commit 849dec8
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 39 deletions.
3 changes: 1 addition & 2 deletions client/app.ts
Expand Up @@ -8,11 +8,10 @@ import MenuCmp from './components/menu';
import HeaderCmp from './components/header';
import FooterCmp from './components/footer';
import SubDetailCmp from './components/sub-detail';
import LoggedInRouterOutlet from './logged-in-router-outlet';

@Component({
selector: 'sb-app',
directives: [ROUTER_DIRECTIVES, LoggedInRouterOutlet, HeaderCmp, FooterCmp],
directives: [ROUTER_DIRECTIVES, HeaderCmp, FooterCmp],
template: `
<sb-header></sb-header>
<router-outlet></router-outlet>
Expand Down
4 changes: 3 additions & 1 deletion client/bootstrap.ts
Expand Up @@ -5,6 +5,7 @@ import {provide} from 'angular2/core';
import {HTTP_PROVIDERS, BrowserXhr} from 'angular2/http';
import {ROUTER_PROVIDERS} from 'angular2/router';
import App from './app';
import Context from './context';
import BrowserXhrWithCredentials from './services/browser-xhr-with-credentials';
import AuthenticationService from './services/authentication';
import SubsService from './services/subs';
Expand All @@ -16,4 +17,5 @@ bootstrap(App, [
provide(AuthenticationService, {useClass: AuthenticationService}),
provide(SubsService, {useClass: SubsService}),
provide(BrowserXhr, {useClass: BrowserXhrWithCredentials})
]).catch((err: any) => console.log(err));
]).then((app: any) => { Context.injector = app.injector; })
.catch((err: any) => console.log(err));
2 changes: 2 additions & 0 deletions client/components/home.ts
@@ -1,7 +1,9 @@
import {Component} from 'angular2/core';
import {ROUTER_DIRECTIVES} from 'angular2/router';
import AuthenticationService from '../services/authentication';
import Auth from './util/auth';

@Auth
@Component({
selector: 'sb-home',
directives: [ROUTER_DIRECTIVES],
Expand Down
2 changes: 2 additions & 0 deletions client/components/logout.ts
@@ -1,7 +1,9 @@
import {Component} from 'angular2/core';
import {Router, ROUTER_DIRECTIVES} from 'angular2/router';
import AuthenticationService from '../services/authentication';
import Auth from './util/auth';

@Auth
@Component({
selector: 'sb-logout',
directives: [ROUTER_DIRECTIVES],
Expand Down
2 changes: 2 additions & 0 deletions client/components/menu.ts
@@ -1,6 +1,8 @@
import {Component} from 'angular2/core';
import SubListCmp from './util/sub-list';
import Auth from './util/auth';

@Auth
@Component({
selector: 'sb-menu',
directives: [SubListCmp],
Expand Down
2 changes: 2 additions & 0 deletions client/components/sub-detail.ts
Expand Up @@ -3,7 +3,9 @@ import {RouteParams, ROUTER_DIRECTIVES} from 'angular2/router';
import SubPriceCmp from './util/sub-price';
import SubsService from '../services/subs';
import Sub from '../models/sub';
import Auth from './util/auth';

@Auth
@Component({
selector: 'sb-subdetail',
directives: [SubPriceCmp, ROUTER_DIRECTIVES],
Expand Down
14 changes: 14 additions & 0 deletions client/components/util/auth.ts
@@ -0,0 +1,14 @@
import {Router, CanActivate} from 'angular2/router';
import AuthenticationService from '../../services/authentication';
import Context from '../../context';

export default function (constr: Function): any {
return CanActivate(() => {
if (Context.injector.get(AuthenticationService).authenticated) {
return true;
} else {
Context.injector.get(Router).navigate(['Login']);
return false;
}
})(constr);
}
13 changes: 13 additions & 0 deletions client/context.ts
@@ -0,0 +1,13 @@
import {Injector} from 'angular2/core';

export default class Context {
private static _injector: Injector;

static get injector (): Injector {
return this._injector;
}

static set injector (value: Injector) {
this._injector = value;
}
}
36 changes: 0 additions & 36 deletions client/logged-in-router-outlet.ts

This file was deleted.

0 comments on commit 849dec8

Please sign in to comment.