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 { AuthService } from '../core'
6 import { FormReactive } from '../shared'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
9 import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service'
10 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
11 import { Router } from '@angular/router'
15 templateUrl: './login.component.html',
16 styleUrls: [ './login.component.scss' ]
19 export class LoginComponent extends FormReactive implements OnInit {
20 @ViewChild('emailInput') input: ElementRef
21 @ViewChild('forgotPasswordModal') forgotPasswordModal: ElementRef
22 @ViewChild('forgotPasswordEmailInput') forgotPasswordEmailInput: ElementRef
25 forgotPasswordEmail = ''
27 private openedForgotPasswordModal: NgbModalRef
30 public router: Router,
31 protected formValidatorService: FormValidatorService,
32 private modalService: NgbModal,
33 private loginValidatorsService: LoginValidatorsService,
34 private authService: AuthService,
35 private userService: UserService,
36 private serverService: ServerService,
37 private redirectService: RedirectService,
38 private notificationsService: NotificationsService,
44 get signupAllowed () {
45 return this.serverService.getConfig().signup.allowed === true
50 username: this.loginValidatorsService.LOGIN_USERNAME,
51 password: this.loginValidatorsService.LOGIN_PASSWORD
54 this.input.nativeElement.focus()
60 const { username, password } = this.form.value
62 this.authService.login(username, password)
64 () => this.redirect(),
67 if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
68 else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.')
69 else this.error = err.message
75 const redirect = this.authService.redirectUrl
77 this.router.navigate([ redirect ])
79 this.redirectService.redirectToHomepage()
84 this.userService.askResetPassword(this.forgotPasswordEmail)
87 const message = this.i18n(
88 'An email with the reset password instructions will be sent to {{email}}.',
89 { email: this.forgotPasswordEmail }
91 this.notificationsService.success(this.i18n('Success'), message)
92 this.hideForgotPasswordModal()
95 err => this.notificationsService.error(this.i18n('Error'), err.message)
99 onForgotPasswordModalShown () {
100 this.forgotPasswordEmailInput.nativeElement.focus()
103 openForgotPasswordModal () {
104 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
107 hideForgotPasswordModal () {
108 this.openedForgotPasswordModal.close()