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,
44 private router: Router) {
48 get signupAllowed () {
49 return this.serverService.getConfig().signup.allowed === true
53 this.form = this.formBuilder.group({
54 username: [ '', Validators.required ],
55 password: [ '', Validators.required ]
58 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
68 const { username, password } = this.form.value
70 this.authService.login(username, password)
72 () => this.redirectService.redirectToHomepage(),
74 err => this.error = err.message
79 this.userService.askResetPassword(this.forgotPasswordEmail)
82 const message = `An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.`
83 this.notificationsService.success('Success', message)
84 this.hideForgotPasswordModal()
87 err => this.notificationsService.error('Error', err.message)
91 onForgotPasswordModalShown () {
92 this.forgotPasswordEmailInput.nativeElement.focus()
95 openForgotPasswordModal () {
96 this.forgotPasswordModal.show()
99 hideForgotPasswordModal () {
100 this.forgotPasswordModal.hide()