]>
Commit | Line | Data |
---|---|---|
ecb4e35f | 1 | import { Component, ElementRef, OnInit, ViewChild } from '@angular/core' |
f8b2c1b4 | 2 | import { Notifier, RedirectService, ServerService } from '@app/core' |
ecb4e35f | 3 | import { UserService } from '@app/shared' |
df98563e C |
4 | import { AuthService } from '../core' |
5 | import { FormReactive } from '../shared' | |
b1d40cff | 6 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 7 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
e309822b | 8 | import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service' |
63347a0f | 9 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' |
ec769c89 | 10 | import { Router } from '@angular/router' |
b1794c53 C |
11 | |
12 | @Component({ | |
a840d396 | 13 | selector: 'my-login', |
d235f6b0 C |
14 | templateUrl: './login.component.html', |
15 | styleUrls: [ './login.component.scss' ] | |
b1794c53 C |
16 | }) |
17 | ||
4b2f33f3 | 18 | export class LoginComponent extends FormReactive implements OnInit { |
f36da21e C |
19 | @ViewChild('emailInput', { static: true }) input: ElementRef |
20 | @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef | |
ecb4e35f | 21 | |
df98563e | 22 | error: string = null |
ecb4e35f | 23 | forgotPasswordEmail = '' |
192ea60b | 24 | |
63347a0f C |
25 | private openedForgotPasswordModal: NgbModalRef |
26 | ||
b1d40cff | 27 | constructor ( |
ec769c89 | 28 | public router: Router, |
d18d6478 | 29 | protected formValidatorService: FormValidatorService, |
63347a0f | 30 | private modalService: NgbModal, |
e309822b | 31 | private loginValidatorsService: LoginValidatorsService, |
b1d40cff C |
32 | private authService: AuthService, |
33 | private userService: UserService, | |
34 | private serverService: ServerService, | |
35 | private redirectService: RedirectService, | |
f8b2c1b4 | 36 | private notifier: Notifier, |
b1d40cff C |
37 | private i18n: I18n |
38 | ) { | |
df98563e | 39 | super() |
4b2f33f3 | 40 | } |
b1794c53 | 41 | |
2b084d70 C |
42 | get signupAllowed () { |
43 | return this.serverService.getConfig().signup.allowed === true | |
44 | } | |
45 | ||
3b3b1820 C |
46 | isEmailDisabled () { |
47 | return this.serverService.getConfig().email.enabled === false | |
48 | } | |
49 | ||
df98563e | 50 | ngOnInit () { |
d18d6478 | 51 | this.buildForm({ |
e309822b C |
52 | username: this.loginValidatorsService.LOGIN_USERNAME, |
53 | password: this.loginValidatorsService.LOGIN_PASSWORD | |
d18d6478 | 54 | }) |
9fe44067 RK |
55 | |
56 | this.input.nativeElement.focus() | |
0f6da32b C |
57 | } |
58 | ||
df98563e C |
59 | login () { |
60 | this.error = null | |
4b2f33f3 | 61 | |
df98563e | 62 | const { username, password } = this.form.value |
4b2f33f3 | 63 | |
2b084d70 C |
64 | this.authService.login(username, password) |
65 | .subscribe( | |
dae5ca24 | 66 | () => this.redirectService.redirectToPreviousRoute(), |
192ea60b | 67 | |
e6921918 C |
68 | err => { |
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 | |
72 | } | |
2b084d70 | 73 | ) |
b1794c53 | 74 | } |
ecb4e35f C |
75 | |
76 | askResetPassword () { | |
77 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
78 | .subscribe( | |
141b177d | 79 | () => { |
b1d40cff | 80 | const message = this.i18n( |
51d4bcad | 81 | 'An email with the reset password instructions will be sent to {{email}}.', |
b1d40cff C |
82 | { email: this.forgotPasswordEmail } |
83 | ) | |
f8b2c1b4 | 84 | this.notifier.success(message) |
ecb4e35f C |
85 | this.hideForgotPasswordModal() |
86 | }, | |
87 | ||
f8b2c1b4 | 88 | err => this.notifier.error(err.message) |
ecb4e35f C |
89 | ) |
90 | } | |
91 | ||
ecb4e35f | 92 | openForgotPasswordModal () { |
63347a0f | 93 | this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal) |
ecb4e35f C |
94 | } |
95 | ||
96 | hideForgotPasswordModal () { | |
63347a0f | 97 | this.openedForgotPasswordModal.close() |
ecb4e35f | 98 | } |
b1794c53 | 99 | } |