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
24 forgotPasswordEmail = ''
26 private openedForgotPasswordModal: NgbModalRef
29 public router: Router,
30 protected formValidatorService: FormValidatorService,
31 private modalService: NgbModal,
32 private loginValidatorsService: LoginValidatorsService,
33 private authService: AuthService,
34 private userService: UserService,
35 private serverService: ServerService,
36 private redirectService: RedirectService,
37 private notificationsService: NotificationsService,
43 get signupAllowed () {
44 return this.serverService.getConfig().signup.allowed === true
48 return this.serverService.getConfig().email.enabled === false
53 username: this.loginValidatorsService.LOGIN_USERNAME,
54 password: this.loginValidatorsService.LOGIN_PASSWORD
57 this.input.nativeElement.focus()
63 const { username, password } = this.form.value
65 this.authService.login(username, password)
67 () => this.redirect(),
70 if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
71 else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.')
72 else this.error = err.message
78 const redirect = this.authService.redirectUrl
80 this.router.navigate([ redirect ])
82 this.redirectService.redirectToHomepage()
87 this.userService.askResetPassword(this.forgotPasswordEmail)
90 const message = this.i18n(
91 'An email with the reset password instructions will be sent to {{email}}.',
92 { email: this.forgotPasswordEmail }
94 this.notificationsService.success(this.i18n('Success'), message)
95 this.hideForgotPasswordModal()
98 err => this.notificationsService.error(this.i18n('Error'), err.message)
102 openForgotPasswordModal () {
103 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
106 hideForgotPasswordModal () {
107 this.openedForgotPasswordModal.close()