1 import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2 import { Notifier, RedirectService, ServerService } from '@app/core'
3 import { UserService } from '@app/shared'
4 import { AuthService } from '../core'
5 import { FormReactive } from '../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
8 import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service'
9 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
10 import { Router } from '@angular/router'
14 templateUrl: './login.component.html',
15 styleUrls: [ './login.component.scss' ]
18 export class LoginComponent extends FormReactive implements OnInit {
19 @ViewChild('emailInput', { static: true }) input: ElementRef
20 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
23 forgotPasswordEmail = ''
25 private openedForgotPasswordModal: NgbModalRef
28 public router: Router,
29 protected formValidatorService: FormValidatorService,
30 private modalService: NgbModal,
31 private loginValidatorsService: LoginValidatorsService,
32 private authService: AuthService,
33 private userService: UserService,
34 private serverService: ServerService,
35 private redirectService: RedirectService,
36 private notifier: Notifier,
42 get signupAllowed () {
43 return this.serverService.getConfig().signup.allowed === true
47 return this.serverService.getConfig().email.enabled === false
52 username: this.loginValidatorsService.LOGIN_USERNAME,
53 password: this.loginValidatorsService.LOGIN_PASSWORD
56 this.input.nativeElement.focus()
62 const { username, password } = this.form.value
64 this.authService.login(username, password)
66 () => this.redirectService.redirectToPreviousRoute(),
69 if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
70 else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.')
71 else this.error = err.message
77 this.userService.askResetPassword(this.forgotPasswordEmail)
80 const message = this.i18n(
81 'An email with the reset password instructions will be sent to {{email}}.',
82 { email: this.forgotPasswordEmail }
84 this.notifier.success(message)
85 this.hideForgotPasswordModal()
88 err => this.notifier.error(err.message)
92 openForgotPasswordModal () {
93 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
96 hideForgotPasswordModal () {
97 this.openedForgotPasswordModal.close()