Created
June 25, 2016 09:09
-
-
Save sonicoder86/10853ea4d82d21e2047942e4cf1681d3 to your computer and use it in GitHub Desktop.
Authentication in Angular 2 - part 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// can-activate-private-metadata.ts | |
import { Router } from '@angular/router-deprecated'; | |
import { makeDecorator } from '@angular/core/src/util/decorators'; | |
import { CanActivate as CanActivateMetadata } | |
from '@angular/router-deprecated/src/lifecycle/lifecycle_annotations_impl'; | |
import { appInjector } from './app-injector'; | |
import { UserService } from './user.service'; | |
class PrivateMetadata extends CanActivateMetadata { | |
constructor() { | |
super((next, prev) => { | |
let injector = appInjector(); | |
if (!injector.get(UserService).isLoggedIn()) { | |
injector.get(Router).navigate(['Login']); | |
return false; | |
} | |
return true; | |
}); | |
} | |
} | |
export const CanActivatePrivateMetadata = makeDecorator(PrivateMetadata); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment