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'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
14 templateUrl: './login.component.html',
15 styleUrls: [ './login.component.scss' ]
18 export class LoginComponent extends FormReactive implements OnInit {
19 @ViewChild('forgotPasswordModal') forgotPasswordModal: ModalDirective
20 @ViewChild('forgotPasswordEmailInput') forgotPasswordEmailInput: ElementRef
29 validationMessages = {
31 'required': 'Username is required.'
34 'required': 'Password is required.'
37 forgotPasswordEmail = ''
40 private authService: AuthService,
41 private userService: UserService,
42 private serverService: ServerService,
43 private redirectService: RedirectService,
44 private notificationsService: NotificationsService,
45 private formBuilder: FormBuilder,
51 get signupAllowed () {
52 return this.serverService.getConfig().signup.allowed === true
56 this.form = this.formBuilder.group({
57 username: [ '', Validators.required ],
58 password: [ '', Validators.required ]
61 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
71 const { username, password } = this.form.value
73 this.authService.login(username, password)
75 () => this.redirectService.redirectToHomepage(),
77 err => this.error = err.message
82 this.userService.askResetPassword(this.forgotPasswordEmail)
85 const message = this.i18n(
86 'An email with the reset password instructions will be sent to {{ email }}.',
87 { email: this.forgotPasswordEmail }
89 this.notificationsService.success(this.i18n('Success'), message)
90 this.hideForgotPasswordModal()
93 err => this.notificationsService.error(this.i18n('Error'), err.message)
97 onForgotPasswordModalShown () {
98 this.forgotPasswordEmailInput.nativeElement.focus()
101 openForgotPasswordModal () {
102 this.forgotPasswordModal.show()
105 hideForgotPasswordModal () {
106 this.forgotPasswordModal.hide()