]>
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 { |
9fe44067 | 19 | @ViewChild('emailInput') input: ElementRef |
63347a0f | 20 | @ViewChild('forgotPasswordModal') forgotPasswordModal: ElementRef |
ecb4e35f C |
21 | @ViewChild('forgotPasswordEmailInput') forgotPasswordEmailInput: ElementRef |
22 | ||
df98563e | 23 | error: string = null |
ecb4e35f | 24 | forgotPasswordEmail = '' |
192ea60b | 25 | |
63347a0f C |
26 | private openedForgotPasswordModal: NgbModalRef |
27 | ||
b1d40cff | 28 | constructor ( |
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, | |
36 | private notificationsService: NotificationsService, | |
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 | ||
df98563e | 46 | ngOnInit () { |
d18d6478 | 47 | this.buildForm({ |
e309822b C |
48 | username: this.loginValidatorsService.LOGIN_USERNAME, |
49 | password: this.loginValidatorsService.LOGIN_PASSWORD | |
d18d6478 | 50 | }) |
9fe44067 RK |
51 | |
52 | this.input.nativeElement.focus() | |
0f6da32b C |
53 | } |
54 | ||
df98563e C |
55 | login () { |
56 | this.error = null | |
4b2f33f3 | 57 | |
df98563e | 58 | const { username, password } = this.form.value |
4b2f33f3 | 59 | |
2b084d70 C |
60 | this.authService.login(username, password) |
61 | .subscribe( | |
62 | () => this.redirectService.redirectToHomepage(), | |
192ea60b | 63 | |
e6921918 C |
64 | err => { |
65 | if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.') | |
66 | else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.') | |
67 | else this.error = err.message | |
68 | } | |
2b084d70 | 69 | ) |
b1794c53 | 70 | } |
ecb4e35f C |
71 | |
72 | askResetPassword () { | |
73 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
74 | .subscribe( | |
141b177d | 75 | () => { |
b1d40cff | 76 | const message = this.i18n( |
51d4bcad | 77 | 'An email with the reset password instructions will be sent to {{email}}.', |
b1d40cff C |
78 | { email: this.forgotPasswordEmail } |
79 | ) | |
80 | this.notificationsService.success(this.i18n('Success'), message) | |
ecb4e35f C |
81 | this.hideForgotPasswordModal() |
82 | }, | |
83 | ||
b1d40cff | 84 | err => this.notificationsService.error(this.i18n('Error'), err.message) |
ecb4e35f C |
85 | ) |
86 | } | |
87 | ||
88 | onForgotPasswordModalShown () { | |
89 | this.forgotPasswordEmailInput.nativeElement.focus() | |
90 | } | |
91 | ||
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 | } |