Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Authentication in Angular 2 - part 9
// 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