import { Injector } from 'angular2/core' ; import { Router , ComponentInstruction } from 'angular2/router' ; import { AuthService } from '../model/AuthService' ; let appInjectorRef ; export const appInjector = ( injector ? ) => { if ( ! injector ) { return appInjectorRef ; } appInjectorRef = injector ; return appInjectorRef ; } ; export const shouldActivate = ( to , from ) => { let injector = appInjector ( ) ; let auth = injector . get ( AuthService ) ; let router = injector . get ( Router ) ; return new Promise ( ( resolve ) => { auth . token$ . subscribe ( ( result ) => { if ( result ) { resolve ( true ) ; } else { if ( to . urlPath === 'signup' || to . urlPath === 'login' ) { resolve ( true ) ; } else { router . navigate ( [ '/Login' ] ) ; resolve ( false ) ; } } } ) ; } ) ; } ;