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 { LOGIN_PASSWORD, LOGIN_USERNAME } from '@app/shared/forms/form-validators/login'
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 authService: AuthService,
28 private userService: UserService,
29 private serverService: ServerService,
30 private redirectService: RedirectService,
31 private notificationsService: NotificationsService,
37 get signupAllowed () {
38 return this.serverService.getConfig().signup.allowed === true
43 username: LOGIN_USERNAME,
44 password: LOGIN_PASSWORD
51 const { username, password } = this.form.value
53 this.authService.login(username, password)
55 () => this.redirectService.redirectToHomepage(),
57 err => this.error = err.message
62 this.userService.askResetPassword(this.forgotPasswordEmail)
65 const message = this.i18n(
66 'An email with the reset password instructions will be sent to {{ email }}.',
67 { email: this.forgotPasswordEmail }
69 this.notificationsService.success(this.i18n('Success'), message)
70 this.hideForgotPasswordModal()
73 err => this.notificationsService.error(this.i18n('Error'), err.message)
77 onForgotPasswordModalShown () {
78 this.forgotPasswordEmailInput.nativeElement.focus()
81 openForgotPasswordModal () {
82 this.forgotPasswordModal.show()
85 hideForgotPasswordModal () {
86 this.forgotPasswordModal.hide()