Advertisement
Guest User

Untitled

a guest
Apr 16th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, AfterViewInit, Renderer, ElementRef } from '@angular/core';
  2. import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
  3. import { Router } from '@angular/router';
  4. import { JhiEventManager } from 'ng-jhipster';
  5.  
  6. import { LoginService } from 'app/core/login/login.service';
  7. import { StateStorageService } from 'app/core/auth/state-storage.service';
  8.  
  9. @Component({
  10.     selector: 'jhi-login-modal',
  11.     templateUrl: './login.component.html'
  12. })
  13. export class JhiLoginModalComponent implements AfterViewInit {
  14.     authenticationError: boolean;
  15.     password: string;
  16.     rememberMe: boolean;
  17.     username: string;
  18.     credentials: any;
  19.  
  20.     constructor(
  21.         private eventManager: JhiEventManager,
  22.         private loginService: LoginService,
  23.         private stateStorageService: StateStorageService,
  24.         private elementRef: ElementRef,
  25.         private renderer: Renderer,
  26.         private router: Router,
  27.         public activeModal: NgbActiveModal
  28.     ) {
  29.         this.credentials = {};
  30.     }
  31.  
  32.     ngAfterViewInit() {
  33.         setTimeout(() => this.renderer.invokeElementMethod(this.elementRef.nativeElement.querySelector('#username'), 'focus', []), 0);
  34.     }
  35.  
  36.     cancel() {
  37.         this.credentials = {
  38.             username: null,
  39.             password: null,
  40.             rememberMe: true
  41.         };
  42.         this.authenticationError = false;
  43.         this.activeModal.dismiss('cancel');
  44.     }
  45.  
  46.     login() {
  47.         this.loginService
  48.             .login({
  49.                 username: this.username,
  50.                 password: this.password,
  51.                 rememberMe: this.rememberMe
  52.             })
  53.             .then(() => {
  54.                 this.authenticationError = false;
  55.                 this.activeModal.dismiss('login success');
  56.                 if (this.router.url === '/register' || /^\/activate\//.test(this.router.url) || /^\/reset\//.test(this.router.url)) {
  57.                     this.router.navigate(['']);
  58.                 }
  59.  
  60.                 this.eventManager.broadcast({
  61.                     name: 'authenticationSuccess',
  62.                     content: 'Sending Authentication Success'
  63.                 });
  64.  
  65.                 // previousState was set in the authExpiredInterceptor before being redirected to login modal.
  66.                 // since login is succesful, go to stored previousState and clear previousState
  67.                 const redirect = this.stateStorageService.getUrl();
  68.                 if (redirect) {
  69.                     this.stateStorageService.storeUrl(null);
  70.                     this.router.navigate([redirect]);
  71.                 }
  72.             })
  73.             .catch(err => {
  74.                 this.authenticationError = true;
  75.                 console.log(err.error.detail + ' xD');
  76.             });
  77.     }
  78.  
  79.     register() {
  80.         this.activeModal.dismiss('to state register');
  81.         this.router.navigate(['/register']);
  82.     }
  83.  
  84.     requestResetPassword() {
  85.         this.activeModal.dismiss('to state requestReset');
  86.         this.router.navigate(['/reset', 'request']);
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement