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 { RedirectService, ServerService } from '@app/core'
5 import { UserService } from '@app/shared'
6 import { NotificationsService } from 'angular2-notifications'
7 import { ModalDirective } from 'ngx-bootstrap/modal'
8 import { AuthService } from '../core'
9 import { FormReactive } from '../shared'
13 templateUrl: './login.component.html',
14 styleUrls: [ './login.component.scss' ]
17 export class LoginComponent extends FormReactive implements OnInit {
18 @ViewChild('forgotPasswordModal') forgotPasswordModal: ModalDirective
19 @ViewChild('forgotPasswordEmailInput') forgotPasswordEmailInput: ElementRef
28 validationMessages = {
30 'required': 'Username is required.'
33 'required': 'Password is required.'
36 forgotPasswordEmail = ''
38 constructor (private authService: AuthService,
39 private userService: UserService,
40 private serverService: ServerService,
41 private redirectService: RedirectService,
42 private notificationsService: NotificationsService,
43 private formBuilder: FormBuilder) {
47 get signupAllowed () {
48 return this.serverService.getConfig().signup.allowed === true
52 this.form = this.formBuilder.group({
53 username: [ '', Validators.required ],
54 password: [ '', Validators.required ]
57 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
67 const { username, password } = this.form.value
69 this.authService.login(username, password)
71 () => this.redirectService.redirectToHomepage(),
73 err => this.error = err.message
78 this.userService.askResetPassword(this.forgotPasswordEmail)
81 const message = `An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.`
82 this.notificationsService.success('Success', message)
83 this.hideForgotPasswordModal()
86 err => this.notificationsService.error('Error', err.message)
90 onForgotPasswordModalShown () {
91 this.forgotPasswordEmailInput.nativeElement.focus()
94 openForgotPasswordModal () {
95 this.forgotPasswordModal.show()
98 hideForgotPasswordModal () {
99 this.forgotPasswordModal.hide()