]>
Commit | Line | Data |
---|---|---|
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 { ModalDirective } from 'ngx-bootstrap/modal' | |
6 | import { AuthService } from '../core' | |
7 | import { FormReactive } from '../shared' | |
8 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
9 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
10 | import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service' | |
11 | ||
12 | @Component({ | |
13 | selector: 'my-login', | |
14 | templateUrl: './login.component.html', | |
15 | styleUrls: [ './login.component.scss' ] | |
16 | }) | |
17 | ||
18 | export class LoginComponent extends FormReactive implements OnInit { | |
19 | @ViewChild('forgotPasswordModal') forgotPasswordModal: ModalDirective | |
20 | @ViewChild('forgotPasswordEmailInput') forgotPasswordEmailInput: ElementRef | |
21 | ||
22 | error: string = null | |
23 | forgotPasswordEmail = '' | |
24 | ||
25 | constructor ( | |
26 | protected formValidatorService: FormValidatorService, | |
27 | private loginValidatorsService: LoginValidatorsService, | |
28 | private authService: AuthService, | |
29 | private userService: UserService, | |
30 | private serverService: ServerService, | |
31 | private redirectService: RedirectService, | |
32 | private notificationsService: NotificationsService, | |
33 | private i18n: I18n | |
34 | ) { | |
35 | super() | |
36 | } | |
37 | ||
38 | get signupAllowed () { | |
39 | return this.serverService.getConfig().signup.allowed === true | |
40 | } | |
41 | ||
42 | ngOnInit () { | |
43 | this.buildForm({ | |
44 | username: this.loginValidatorsService.LOGIN_USERNAME, | |
45 | password: this.loginValidatorsService.LOGIN_PASSWORD | |
46 | }) | |
47 | } | |
48 | ||
49 | login () { | |
50 | this.error = null | |
51 | ||
52 | const { username, password } = this.form.value | |
53 | ||
54 | this.authService.login(username, password) | |
55 | .subscribe( | |
56 | () => this.redirectService.redirectToHomepage(), | |
57 | ||
58 | err => this.error = err.message | |
59 | ) | |
60 | } | |
61 | ||
62 | askResetPassword () { | |
63 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
64 | .subscribe( | |
65 | res => { | |
66 | const message = this.i18n( | |
67 | 'An email with the reset password instructions will be sent to {{ email }}.', | |
68 | { email: this.forgotPasswordEmail } | |
69 | ) | |
70 | this.notificationsService.success(this.i18n('Success'), message) | |
71 | this.hideForgotPasswordModal() | |
72 | }, | |
73 | ||
74 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
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 | } | |
89 | } |