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