Broker System for Supercredit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.8KB

  1. import { Component, OnDestroy, OnInit } from '@angular/core';
  2. import { FormControl, FormGroup, Validators } from '@angular/forms';
  3. import { Router } from '@angular/router';
  4. import { NotificationService } from '@progress/kendo-angular-notification';
  5. import { Subscription } from 'rxjs';
  6. import { AuthService } from '../service/auth.service';
  7. import {ApiV1LoginResponse} from '../models/api-v1-login-response';
  8. import {SessionService} from '../service/session.service';
  9. import {environment} from '../../environments/environment';
  10. @Component({
  11. selector: 'app-auth',
  12. templateUrl: './auth.component.html',
  13. styleUrls: ['./auth.component.scss']
  14. })
  15. export class AuthComponent implements OnInit, OnDestroy{
  16. loading: boolean ; // a state that user is currently loading loggin
  17. loginSub: Subscription;
  18. currentApplicationVersion = environment.appVersion;
  19. public userForm: FormGroup = new FormGroup({
  20. password: new FormControl('pass', [Validators.minLength(3), Validators.maxLength(20)]),
  21. email: new FormControl('admin@supercredit.com.au', Validators.email)
  22. });
  23. constructor(private authService: AuthService, private ss: SessionService,
  24. private router: Router, private notificationService: NotificationService) { }
  25. ngOnInit(): void {
  26. this.ss.logout();
  27. // this.ss.logoutAndClearLocalStorage();
  28. this.loginSub = this.ss.loginResult.subscribe(
  29. responseData => {
  30. // console.log(responseData);
  31. this.onLogin(responseData);
  32. }
  33. );
  34. }
  35. ngOnDestroy(): void {
  36. this.loginSub.unsubscribe();
  37. }
  38. // tslint:disable-next-line:typedef
  39. submitForm(): void {
  40. this.loading = true;
  41. this.authService.login(this.userForm.value.email, this.userForm.value.password);
  42. }
  43. public onLogin(rsp: ApiV1LoginResponse): void {
  44. this.loading = false;
  45. // console.log (' auth event login ' , rsp );
  46. if (rsp.login) {
  47. switch ( rsp.role ) {
  48. case 'admin':
  49. this.router.navigate(['/dashboard']);
  50. break;
  51. case 'manager':
  52. this.router.navigate(['/dashboard']);
  53. break;
  54. case 'accountant':
  55. this.router.navigate(['/dashboard']);
  56. break;
  57. case 'broker':
  58. this.router.navigate(['/broker-loan-list']);
  59. break;
  60. case 'user':
  61. this.router.navigate(['/client-loan-list']);
  62. break;
  63. default:
  64. this.router.navigate(['/e403']);
  65. break;
  66. }
  67. }
  68. else {
  69. this.show();
  70. }
  71. }
  72. public show(): void {
  73. this.notificationService.show({
  74. content: 'Your loggin is failed, please try again.',
  75. cssClass: 'button-notification',
  76. animation: { type: 'slide', duration: 400 },
  77. position: { horizontal: 'center', vertical: 'top' },
  78. type: { style: 'error', icon: true },
  79. hideAfter : 2000
  80. });
  81. }
  82. }