Advertisement
Guest User

Untitled

a guest
Nov 9th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnDestroy, OnInit } from '@angular/core';
  2. import { User } from './user';
  3. import { SessionService } from '../session-service/session.service';
  4. import { Subscription } from 'rxjs';
  5. import { Router } from '@angular/router';
  6. import { TranslateService } from '@ngx-translate/core';
  7. import { UserSession } from './user-session';
  8.  
  9. const emptyUser: User = {
  10.   username: '',
  11.   password: ''
  12. };
  13. @Component({
  14.   selector: 'app-login',
  15.   templateUrl: './login.component.html',
  16.   styleUrls: ['./login.component.scss']
  17. })
  18. export class LoginComponent implements OnInit, OnDestroy {
  19.   loginSubscription: Subscription;
  20.   loginFailed = false;
  21.   loginUser: User = emptyUser;
  22.   userSession: UserSession = {};
  23.  
  24.   constructor(private sessionService: SessionService, private router: Router, public translate: TranslateService) {}
  25.  
  26.   ngOnDestroy(): void {
  27.     if (this.loginSubscription) {
  28.       this.loginSubscription.unsubscribe();
  29.     }
  30.   }
  31.  
  32.   ngOnInit() {}
  33.  
  34.   login(): void {
  35.     this.loginSubscription = this.sessionService.login(this.loginUser).subscribe(
  36.       result => {
  37.         if (result) {
  38.           this.userSession = result;
  39.           this.sessionService.setUser(this.userSession);
  40.           this.router.navigate(['bo/dashboard']);
  41.         }
  42.       },
  43.       error => {
  44.         this.loginFailed = true;
  45.         console.error('error in login', error);
  46.       }
  47.     );
  48.   }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement