1 import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2 import { Notifier, RedirectService } 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 { ActivatedRoute } from '@angular/router'
11 import { ServerConfig } from '@shared/models/server/server-config.model'
15 templateUrl: './login.component.html',
16 styleUrls: [ './login.component.scss' ]
19 export class LoginComponent extends FormReactive implements OnInit {
20 @ViewChild('emailInput', { static: true }) input: ElementRef
21 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
24 forgotPasswordEmail = ''
26 private openedForgotPasswordModal: NgbModalRef
27 private serverConfig: ServerConfig
30 protected formValidatorService: FormValidatorService,
31 private route: ActivatedRoute,
32 private modalService: NgbModal,
33 private loginValidatorsService: LoginValidatorsService,
34 private authService: AuthService,
35 private userService: UserService,
36 private redirectService: RedirectService,
37 private notifier: Notifier,
43 get signupAllowed () {
44 return this.serverConfig.signup.allowed === true
48 return this.serverConfig.email.enabled === false
52 this.serverConfig = this.route.snapshot.data.serverConfig
55 username: this.loginValidatorsService.LOGIN_USERNAME,
56 password: this.loginValidatorsService.LOGIN_PASSWORD
59 this.input.nativeElement.focus()
65 const { username, password } = this.form.value
67 this.authService.login(username, password)
69 () => this.redirectService.redirectToPreviousRoute(),
72 if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
73 else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.')
74 else this.error = err.message
80 this.userService.askResetPassword(this.forgotPasswordEmail)
83 const message = this.i18n(
84 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.',
85 { email: this.forgotPasswordEmail }
87 this.notifier.success(message)
88 this.hideForgotPasswordModal()
91 err => this.notifier.error(err.message)
95 openForgotPasswordModal () {
96 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
99 hideForgotPasswordModal () {
100 this.openedForgotPasswordModal.close()