import { Component , AfterViewInit , Renderer , ElementRef } from '@angular/core' ; import { FormBuilder , FormGroup , Validators } from '@angular/forms' ; import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap' ; import { Router } from '@angular/router' ; import { JhiEventManager } from 'ng-jhipster' ; import { LoginService } from 'app/core/login/login.service' ; import { StateStorageService } from 'app/core/auth/state-storage.service' ; @ Component ( { selector : 'jhi-login-modal' , templateUrl : './login.component.html' } ) export class JhiLoginModalComponent implements AfterViewInit { authenticationError : boolean ; loginForm = this . fb . group ( { username : [ '' ] , password : [ '' ] , rememberMe : [ true ] } ) ; constructor ( private eventManager , private loginService , private stateStorageService , private elementRef , private renderer , private router , public activeModal , private fb ) { } ngAfterViewInit ( ) { setTimeout ( ( ) => this . renderer . invokeElementMethod ( this . elementRef . nativeElement . querySelector ( '#username' ) , 'focus' , [ ] ) , 0 ) ; } cancel ( ) { this . authenticationError = false ; this . loginForm . patchValue ( { username : '' , password : '' } ) ; this . activeModal . dismiss ( 'cancel' ) ; } login ( ) { this . loginService . login ( { username : this . loginForm . get ( 'username' ) . value , password : this . loginForm . get ( 'password' ) . value , rememberMe : this . loginForm . get ( 'rememberMe' ) . value } ) . then ( ( ) => { this . authenticationError = false ; this . activeModal . dismiss ( 'login success' ) ; if ( this . router . url === '/register' || /^\/activate\// . test ( this . router . url ) || /^\/reset\// . test ( this . router . url ) ) { this . router . navigate ( [ '' ] ) ; } this . eventManager . broadcast ( { name : 'authenticationSuccess' , content : 'Sending Authentication Success' } ) ; const redirect = this . stateStorageService . getUrl ( ) ; if ( redirect ) { this . stateStorageService . storeUrl ( null ) ; this . router . navigateByUrl ( redirect ) ; } } ) . catch ( ( ) => { this . authenticationError = true ; } ) ; } register ( ) { this . activeModal . dismiss ( 'to state register' ) ; this . router . navigate ( [ '/register' ] ) ; } requestResetPassword ( ) { this . activeModal . dismiss ( 'to state requestReset' ) ; this . router . navigate ( [ '/reset' , 'request' ] ) ; } }