Advertisement
Guest User

Untitled

a guest
Oct 13th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. import {Component, OnDestroy} from '@angular/core';
  2. import {Router} from '@angular/router';
  3. import { Subscription } from 'rxjs/Subscription';
  4. import {Location} from '@angular/common';
  5.  
  6.  
  7. import {AuthService} from './services/auth.service';
  8. import {RoutingService} from './services/routing.service';
  9.  
  10. import {ToastyService, ToastyConfig, ToastOptions, ToastData} from 'ng2-toasty';
  11.  
  12. import {LoadingBarModule, LoadingBarService} from 'ng2-loading-bar';
  13.  
  14.  
  15. @Component({
  16. selector: 'login',
  17. template: `
  18. <loading-bar color="#FF0000" [height]="3" [animationTime]="0.3" [runInterval]="100" [progress]="0"></loading-bar>
  19. <h3> {{'LOGIN' | translate }} </h3>
  20. <p> {{Message}} </p>
  21.  
  22. <div *ngIf="!authService.isLoggedIn">
  23. <input [(ngModel)]="username" placeholder="{{'USERNAME' | translate}}" /><br />
  24. <input type="password" [(ngModel)]="password" placeholder="{{'PASSWORD' | translate}}" />
  25. </div>
  26. <div>
  27. <button (click)="login()" *ngIf="!authService.isLoggedIn">{{'LOGIN' | translate }}</button>
  28. </div>
  29.  
  30. <label class="label label-danger">{{errorMessage}}</label>
  31.  
  32. <ng2-toasty [position]="'top-center'"></ng2-toasty>
  33. `
  34. })
  35.  
  36. export class LoginComponent implements OnDestroy {
  37. username: string;
  38. password: string;
  39. subscription: Subscription;
  40.  
  41. constructor(private authService: AuthService, private router: Router, private toastyService: ToastyService, private toastyConfig: ToastyConfig, private loadingBarService: LoadingBarService, private routingService: RoutingService, private location:Location) {
  42. this.toastyConfig.theme = 'material';
  43. }
  44.  
  45. login() {
  46.  
  47. this.loadingBarService.start();
  48.  
  49. this.subscription = this.authService.login(this.username, this.password).subscribe(() => {
  50.  
  51. if (this.authService.isLoggedIn) {
  52. this.toastyService.default('Hi');
  53.  
  54. this.routingService.logged = false;
  55.  
  56. let redirect = this.authService.redirectUrl ? this.authService.redirectUrl : this.routingService.lang + '/apphomecomponent';
  57.  
  58. this.router.navigate([redirect]);
  59. }
  60. else {
  61. this.toastyService.default('Login failed');
  62. }
  63. });
  64. }
  65.  
  66. ngOnDestroy() {
  67. this.subscription.unsubscribe();
  68. }
  69. }
  70.  
  71. import {Injectable} from '@angular/core';
  72.  
  73. import {Observable} from 'rxjs/Observable';
  74. import 'rxjs/add/observable/of';
  75. import 'rxjs/add/operator/do';
  76. import 'rxjs/add/operator/delay';
  77.  
  78.  
  79.  
  80. @Injectable()
  81. export class AuthService {
  82. isLoggedIn: boolean = false;
  83.  
  84. redirectUrl: string;
  85.  
  86. login(username: string, password: string): Observable<boolean> {
  87. if (username === 'test' && password === 'test') {
  88. return Observable.of(true).delay(1000).do(val => this.isLoggedIn = true);
  89. }
  90. else {
  91. return Observable.of(false).delay(1000).do(val => this.isLoggedIn = false);
  92. }
  93. }
  94.  
  95. logout(): void {
  96. this.isLoggedIn = false;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement