Advertisement
Guest User

Untitled

a guest
Jun 12th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2. import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
  3. import { JhiEventManager } from 'ng-jhipster';
  4. import { Router } from '@angular/router';
  5.  
  6. import { LoginModalService, AccountService, Account } from 'app/core';
  7. import {FormBuilder} from '@angular/forms';
  8. import { LoginService } from 'app/core/login/login.service';
  9. import { StateStorageService } from 'app/core/auth/state-storage.service';
  10. import {NotificationService} from 'app/entities/notification';
  11. import {HttpErrorResponse, HttpHeaders, HttpParams, HttpResponse} from '@angular/common/http';
  12. import {INotification} from 'app/shared/model/notification.model';
  13.  
  14. @Component({
  15. selector: 'jhi-home',
  16. templateUrl: './home.component.html',
  17. styleUrls: ['home.scss']
  18. })
  19. export class HomeComponent implements OnInit {
  20. authenticationError: boolean;
  21. account: Account;
  22. modalRef: NgbModalRef;
  23. notifications: INotification[];
  24.  
  25. loginForm = this.fb.group({
  26. username: [''],
  27. password: [''],
  28. rememberMe: [true]
  29. });
  30.  
  31. itemsPerPage: any;
  32. page: any;
  33. predicate: any;
  34. reverse: any;
  35.  
  36. constructor(
  37. private accountService: AccountService,
  38. private loginModalService: LoginModalService,
  39. private eventManager: JhiEventManager,
  40. private fb: FormBuilder,
  41. private loginService: LoginService,
  42. private stateStorageService: StateStorageService,
  43. private router: Router,
  44. protected notificationService: NotificationService,
  45. ) {}
  46.  
  47. ngOnInit() {
  48. this.loadAllNotifications();
  49. this.accountService.identity().then((account: Account) => {
  50. this.account = account;
  51. });
  52. this.registerAuthenticationSuccess();
  53. }
  54.  
  55. registerAuthenticationSuccess() {
  56. this.eventManager.subscribe('authenticationSuccess', message => {
  57. this.accountService.identity().then(account => {
  58. this.account = account;
  59. });
  60. });
  61. }
  62.  
  63. isAuthenticated() {
  64. return this.accountService.isAuthenticated();
  65. }
  66. cancel() {
  67. this.authenticationError = false;
  68. this.loginForm.patchValue({
  69. username: '',
  70. password: ''
  71. });
  72. }
  73.  
  74. login() {
  75. this.loginService
  76. .login({
  77. username: this.loginForm.get('username').value,
  78. password: this.loginForm.get('password').value,
  79. rememberMe: this.loginForm.get('rememberMe').value
  80. })
  81. .then(() => {
  82. this.authenticationError = false;
  83. if (this.router.url === '/register' || /^\/activate\//.test(this.router.url) || /^\/reset\//.test(this.router.url)) {
  84. this.router.navigate(['']);
  85. }
  86.  
  87. this.eventManager.broadcast({
  88. name: 'authenticationSuccess',
  89. content: 'Sending Authentication Success'
  90. });
  91.  
  92. // previousState was set in the authExpiredInterceptor before being redirected to login modal.
  93. // since login is successful, go to stored previousState and clear previousState
  94. const redirect = this.stateStorageService.getUrl();
  95. if (redirect) {
  96. this.stateStorageService.storeUrl(null);
  97. this.router.navigateByUrl(redirect);
  98. }
  99. })
  100. .catch(() => {
  101. this.authenticationError = true;
  102. });
  103.  
  104. }
  105. register() {
  106. this.router.navigate(['/register']);
  107. }
  108.  
  109. requestResetPassword() {
  110. this.router.navigate(['/reset', 'request']);
  111. }
  112.  
  113. loadAllNotifications() {
  114. this.notificationService.query({
  115. page: 0,
  116. size: 5,
  117. sort: ['id,desc']
  118.  
  119.  
  120. })
  121. .subscribe( (data) =>
  122. this.notifications = data.body
  123. );
  124. }
  125. sort() {
  126. const result = [this.predicate + ',' + (this.reverse ? 'asc' : 'desc')];
  127. if (this.predicate !== 'id') {
  128. result.push('id');
  129.  
  130. return result;
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement