]>
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 | import { Router } from '@angular/router' | |
12 | ||
13 | @Component({ | |
14 | selector: 'my-login', | |
15 | templateUrl: './login.component.html', | |
16 | styleUrls: [ './login.component.scss' ] | |
17 | }) | |
18 | ||
19 | export class LoginComponent extends FormReactive implements OnInit { | |
20 | @ViewChild('emailInput') input: ElementRef | |
21 | @ViewChild('forgotPasswordModal') forgotPasswordModal: ElementRef | |
22 | @ViewChild('forgotPasswordEmailInput') forgotPasswordEmailInput: ElementRef | |
23 | ||
24 | error: string = null | |
25 | forgotPasswordEmail = '' | |
26 | ||
27 | private openedForgotPasswordModal: NgbModalRef | |
28 | ||
29 | constructor ( | |
30 | public router: Router, | |
31 | protected formValidatorService: FormValidatorService, | |
32 | private modalService: NgbModal, | |
33 | private loginValidatorsService: LoginValidatorsService, | |
34 | private authService: AuthService, | |
35 | private userService: UserService, | |
36 | private serverService: ServerService, | |
37 | private redirectService: RedirectService, | |
38 | private notificationsService: NotificationsService, | |
39 | private i18n: I18n | |
40 | ) { | |
41 | super() | |
42 | } | |
43 | ||
44 | get signupAllowed () { | |
45 | return this.serverService.getConfig().signup.allowed === true | |
46 | } | |
47 | ||
48 | ngOnInit () { | |
49 | this.buildForm({ | |
50 | username: this.loginValidatorsService.LOGIN_USERNAME, | |
51 | password: this.loginValidatorsService.LOGIN_PASSWORD | |
52 | }) | |
53 | ||
54 | this.input.nativeElement.focus() | |
55 | } | |
56 | ||
57 | login () { | |
58 | this.error = null | |
59 | ||
60 | const { username, password } = this.form.value | |
61 | ||
62 | this.authService.login(username, password) | |
63 | .subscribe( | |
64 | () => this.redirect(), | |
65 | ||
66 | err => { | |
67 | if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.') | |
68 | else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.') | |
69 | else this.error = err.message | |
70 | } | |
71 | ) | |
72 | } | |
73 | ||
74 | redirect () { | |
75 | const redirect = this.authService.redirectUrl | |
76 | if (redirect) { | |
77 | this.router.navigate([ redirect ]) | |
78 | } else { | |
79 | this.redirectService.redirectToHomepage() | |
80 | } | |
81 | } | |
82 | ||
83 | askResetPassword () { | |
84 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
85 | .subscribe( | |
86 | () => { | |
87 | const message = this.i18n( | |
88 | 'An email with the reset password instructions will be sent to {{email}}.', | |
89 | { email: this.forgotPasswordEmail } | |
90 | ) | |
91 | this.notificationsService.success(this.i18n('Success'), message) | |
92 | this.hideForgotPasswordModal() | |
93 | }, | |
94 | ||
95 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
96 | ) | |
97 | } | |
98 | ||
99 | onForgotPasswordModalShown () { | |
100 | this.forgotPasswordEmailInput.nativeElement.focus() | |
101 | } | |
102 | ||
103 | openForgotPasswordModal () { | |
104 | this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal) | |
105 | } | |
106 | ||
107 | hideForgotPasswordModal () { | |
108 | this.openedForgotPasswordModal.close() | |
109 | } | |
110 | } |