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