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