1 import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2 import { RedirectService, ServerService } from '@app/core'
3 import { UserService } from '@app/shared'
4 import { NotificationsService } from 'angular2-notifications'
5 import { ModalDirective } from 'ngx-bootstrap/modal'
6 import { AuthService } from '../core'
7 import { FormReactive } from '../shared'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
10 import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service'
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
23 forgotPasswordEmail = ''
26 protected formValidatorService: FormValidatorService,
27 private loginValidatorsService: LoginValidatorsService,
28 private authService: AuthService,
29 private userService: UserService,
30 private serverService: ServerService,
31 private redirectService: RedirectService,
32 private notificationsService: NotificationsService,
38 get signupAllowed () {
39 return this.serverService.getConfig().signup.allowed === true
44 username: this.loginValidatorsService.LOGIN_USERNAME,
45 password: this.loginValidatorsService.LOGIN_PASSWORD
52 const { username, password } = this.form.value
54 this.authService.login(username, password)
56 () => this.redirectService.redirectToHomepage(),
58 err => this.error = err.message
63 this.userService.askResetPassword(this.forgotPasswordEmail)
66 const message = this.i18n(
67 'An email with the reset password instructions will be sent to {{ email }}.',
68 { email: this.forgotPasswordEmail }
70 this.notificationsService.success(this.i18n('Success'), message)
71 this.hideForgotPasswordModal()
74 err => this.notificationsService.error(this.i18n('Error'), err.message)
78 onForgotPasswordModalShown () {
79 this.forgotPasswordEmailInput.nativeElement.focus()
82 openForgotPasswordModal () {
83 this.forgotPasswordModal.show()
86 hideForgotPasswordModal () {
87 this.forgotPasswordModal.hide()