]>
Commit | Line | Data |
---|---|---|
ecb4e35f | 1 | import { Component, ElementRef, OnInit, ViewChild } from '@angular/core' |
2b084d70 | 2 | import { RedirectService, ServerService } from '@app/core' |
ecb4e35f C |
3 | import { UserService } from '@app/shared' |
4 | import { NotificationsService } from 'angular2-notifications' | |
df98563e C |
5 | import { AuthService } from '../core' |
6 | import { FormReactive } from '../shared' | |
b1d40cff | 7 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 8 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
e309822b | 9 | import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service' |
63347a0f | 10 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' |
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 { |
63347a0f | 19 | @ViewChild('forgotPasswordModal') forgotPasswordModal: ElementRef |
ecb4e35f C |
20 | @ViewChild('forgotPasswordEmailInput') forgotPasswordEmailInput: ElementRef |
21 | ||
df98563e | 22 | error: string = null |
ecb4e35f | 23 | forgotPasswordEmail = '' |
192ea60b | 24 | |
63347a0f C |
25 | private openedForgotPasswordModal: NgbModalRef |
26 | ||
b1d40cff | 27 | constructor ( |
d18d6478 | 28 | protected formValidatorService: FormValidatorService, |
63347a0f | 29 | private modalService: NgbModal, |
e309822b | 30 | private loginValidatorsService: LoginValidatorsService, |
b1d40cff C |
31 | private authService: AuthService, |
32 | private userService: UserService, | |
33 | private serverService: ServerService, | |
34 | private redirectService: RedirectService, | |
35 | private notificationsService: NotificationsService, | |
b1d40cff C |
36 | private i18n: I18n |
37 | ) { | |
df98563e | 38 | super() |
4b2f33f3 | 39 | } |
b1794c53 | 40 | |
2b084d70 C |
41 | get signupAllowed () { |
42 | return this.serverService.getConfig().signup.allowed === true | |
43 | } | |
44 | ||
df98563e | 45 | ngOnInit () { |
d18d6478 | 46 | this.buildForm({ |
e309822b C |
47 | username: this.loginValidatorsService.LOGIN_USERNAME, |
48 | password: this.loginValidatorsService.LOGIN_PASSWORD | |
d18d6478 | 49 | }) |
0f6da32b C |
50 | } |
51 | ||
df98563e C |
52 | login () { |
53 | this.error = null | |
4b2f33f3 | 54 | |
df98563e | 55 | const { username, password } = this.form.value |
4b2f33f3 | 56 | |
2b084d70 C |
57 | this.authService.login(username, password) |
58 | .subscribe( | |
59 | () => this.redirectService.redirectToHomepage(), | |
192ea60b | 60 | |
e6921918 C |
61 | err => { |
62 | if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.') | |
63 | else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.') | |
64 | else this.error = err.message | |
65 | } | |
2b084d70 | 66 | ) |
b1794c53 | 67 | } |
ecb4e35f C |
68 | |
69 | askResetPassword () { | |
70 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
71 | .subscribe( | |
141b177d | 72 | () => { |
b1d40cff | 73 | const message = this.i18n( |
51d4bcad | 74 | 'An email with the reset password instructions will be sent to {{email}}.', |
b1d40cff C |
75 | { email: this.forgotPasswordEmail } |
76 | ) | |
77 | this.notificationsService.success(this.i18n('Success'), message) | |
ecb4e35f C |
78 | this.hideForgotPasswordModal() |
79 | }, | |
80 | ||
b1d40cff | 81 | err => this.notificationsService.error(this.i18n('Error'), err.message) |
ecb4e35f C |
82 | ) |
83 | } | |
84 | ||
85 | onForgotPasswordModalShown () { | |
86 | this.forgotPasswordEmailInput.nativeElement.focus() | |
87 | } | |
88 | ||
89 | openForgotPasswordModal () { | |
63347a0f | 90 | this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal) |
ecb4e35f C |
91 | } |
92 | ||
93 | hideForgotPasswordModal () { | |
63347a0f | 94 | this.openedForgotPasswordModal.close() |
ecb4e35f | 95 | } |
b1794c53 | 96 | } |