Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { ISecurityService } from './ISecurity.service';
- import { NgModule, ModuleWithProviders, Optional, SkipSelf, InjectionToken } from '@angular/core';
- import { CommonModule } from '@angular/common';
- import { UserComponent } from './user/user.component';
- export const SECURITY_SERVICE = new InjectionToken<ISecurityService>('security.service');
- @NgModule({
- imports: [
- CommonModule
- ],
- exports: [ UserComponent ],
- declarations: [ UserComponent ]
- })
- export class SecurityModule {
- static forRoot(securityService: ISecurityService): ModuleWithProviders {
- return {
- ngModule: SecurityModule,
- providers: [{
- provide: SECURITY_SERVICE,
- useValue: securityService
- }]
- };
- }
- constructor (@Optional() @SkipSelf() parentModule: SecurityModule) {
- if (parentModule) {
- throw new Error(
- 'SecurityModule is already loaded. Import it in the AppModule only');
- }
- }
- }
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
- import { AppComponent } from './app.component';
- import { SecurityModule, SecurityMockService } from '@savantly/ngx-security';
- export const mockSecurity = new SecurityMockService();
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- BrowserModule,
- SecurityModule.forRoot(mockSecurity)
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
- @Injectable()
- export class SecurityMockService implements ISecurityService {
- logout: () => void;
- login: (username: string, password: string) => boolean;
- user: IUser;
- constructor() {
- this.user = {
- principal: 'demoUser',
- displayName: 'Demo User',
- authenticated: false,
- roles: ['ADMIN', 'USER']
- };
- this.login = (_username: string, _password: string) => {
- this.user.authenticated = true;
- return true;
- };
- this.logout = () => {
- this.user.authenticated = false;
- };
- }
- }
Add Comment
Please, Sign In to add comment