1 import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2 import { FormBuilder, FormGroup, Validators } from '@angular/forms'
3 import { Router } from '@angular/router'
4 import { UserService } from '@app/shared'
5 import { NotificationsService } from 'angular2-notifications'
6 import { ModalDirective } from 'ngx-bootstrap/modal'
7 import { AuthService } from '../core'
8 import { FormReactive } from '../shared'
12 templateUrl: './login.component.html',
13 styleUrls: [ './login.component.scss' ]
16 export class LoginComponent extends FormReactive implements OnInit {
17 @ViewChild('forgotPasswordModal') forgotPasswordModal: ModalDirective
18 @ViewChild('forgotPasswordEmailInput') forgotPasswordEmailInput: ElementRef
27 validationMessages = {
29 'required': 'Username is required.'
32 'required': 'Password is required.'
35 forgotPasswordEmail = ''
38 private authService: AuthService,
39 private userService: UserService,
40 private notificationsService: NotificationsService,
41 private formBuilder: FormBuilder,
42 private router: Router
48 this.form = this.formBuilder.group({
49 username: [ '', Validators.required ],
50 password: [ '', Validators.required ]
53 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
63 const { username, password } = this.form.value
65 this.authService.login(username, password).subscribe(
66 () => this.router.navigate(['/videos/list']),
68 err => this.error = err.message
73 this.userService.askResetPassword(this.forgotPasswordEmail)
76 const message = `An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.`
77 this.notificationsService.success('Success', message)
78 this.hideForgotPasswordModal()
81 err => this.notificationsService.error('Error', err.message)
85 onForgotPasswordModalShown () {
86 this.forgotPasswordEmailInput.nativeElement.focus()
89 openForgotPasswordModal () {
90 this.forgotPasswordModal.show()
93 hideForgotPasswordModal () {
94 this.forgotPasswordModal.hide()