Skip to content
login.component.ts 3.16 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import {Component, AfterViewInit, Renderer, ElementRef, OnInit} from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { Router } from '@angular/router';
import { JhiEventManager } from 'ng-jhipster';

import { LoginService } from './login.service';
import { StateStorageService } from '../auth/state-storage.service';

import {
    AbstractControl,
    FormBuilder,
    FormGroup,
    Validators
} from '@angular/forms';
import {EmitService} from "../event/eventEmitter";

@Component({
    selector: 'jhi-login-modal',
    templateUrl: './login.component.html'
})
export class JhiLoginModalComponent implements AfterViewInit,OnInit {
    validateForm: FormGroup;

wangqinghua's avatar
wangqinghua committed
    isSpinning = false;
wangqinghua's avatar
wangqinghua committed
    authenticationError: boolean;
    password: string;
    rememberMe: boolean;
    username: string;
    credentials: any;

    constructor(
        private fb: FormBuilder,
        private eventManager: JhiEventManager,
        private loginService: LoginService,
        private stateStorageService: StateStorageService,
        private elementRef: ElementRef,
        private renderer: Renderer,
        private router: Router,
        public activeModal: NgbActiveModal,
        public emitService:EmitService
    ) {
        this.credentials = {};
        this.emitService.eventEmit.subscribe((value: any) => {
            if(value == "logout") {
                this.router.navigate(['app/login']);
            }else{
                // this.token = false;
            }
        });
    }

    submitForm(): void {
wangqinghua's avatar
wangqinghua committed
        for(const i in this.validateForm.controls) {
wangqinghua's avatar
wangqinghua committed
            this.validateForm.controls[ i ].markAsDirty();
            this.validateForm.controls[ i ].updateValueAndValidity();
        }
    }

    ngOnInit(): void {
        this.validateForm = this.fb.group({
            userName: [ null, [ Validators.required ] ],
            password: [ null, [ Validators.required ] ],
            remember: [ true ]
        });
    }

    ngAfterViewInit() {
        // this.renderer.invokeElementMethod(this.elementRef.nativeElement.querySelector('#username'), 'focus', []);
    }

    cancel() {
        this.credentials = {
            username: null,
            password: null,
            rememberMe: true
        };
        this.authenticationError = false;
        this.activeModal.dismiss('cancel');
    }

    login() {
wangqinghua's avatar
wangqinghua committed
        this.isSpinning = true;
wangqinghua's avatar
wangqinghua committed
        this.loginService.login({
            loginName: this.username,
            password: this.password,
            rememberMe: true
        }).then(() => {
wangqinghua's avatar
wangqinghua committed
            this.isSpinning = false;
            this.router.navigate(['app/main/basic']);
            // this.authenticationError = false;
            // this.activeModal.dismiss('login success');
wangqinghua's avatar
wangqinghua committed
            // if (this.router.url === '/register' || (/^\/activate\//.test(this.router.url)) ||
            //     (/^\/reset\//.test(this.router.url))) {
            //     this.router.navigate(['app/main/basic']);
            // }
wangqinghua's avatar
wangqinghua committed

        }).catch(() => {
            this.authenticationError = true;
        });
    }



    requestResetPassword() {
        this.activeModal.dismiss('to state requestReset');
        this.router.navigate(['/reset', 'request']);
    }
}