]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ElementRef, OnInit, ViewChild } from '@angular/core' | |
2 | import { Notifier, RedirectService } from '@app/core' | |
3 | import { UserService } from '@app/shared' | |
4 | import { AuthService } from '../core' | |
5 | import { FormReactive } from '../shared' | |
6 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
7 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
8 | import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service' | |
9 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' | |
10 | import { ActivatedRoute } from '@angular/router' | |
11 | import { ServerConfig } from '@shared/models/server/server-config.model' | |
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', { static: true }) input: ElementRef | |
21 | @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef | |
22 | ||
23 | error: string = null | |
24 | forgotPasswordEmail = '' | |
25 | ||
26 | private openedForgotPasswordModal: NgbModalRef | |
27 | private serverConfig: ServerConfig | |
28 | ||
29 | constructor ( | |
30 | protected formValidatorService: FormValidatorService, | |
31 | private route: ActivatedRoute, | |
32 | private modalService: NgbModal, | |
33 | private loginValidatorsService: LoginValidatorsService, | |
34 | private authService: AuthService, | |
35 | private userService: UserService, | |
36 | private redirectService: RedirectService, | |
37 | private notifier: Notifier, | |
38 | private i18n: I18n | |
39 | ) { | |
40 | super() | |
41 | } | |
42 | ||
43 | get signupAllowed () { | |
44 | return this.serverConfig.signup.allowed === true | |
45 | } | |
46 | ||
47 | isEmailDisabled () { | |
48 | return this.serverConfig.email.enabled === false | |
49 | } | |
50 | ||
51 | ngOnInit () { | |
52 | this.serverConfig = this.route.snapshot.data.serverConfig | |
53 | ||
54 | this.buildForm({ | |
55 | username: this.loginValidatorsService.LOGIN_USERNAME, | |
56 | password: this.loginValidatorsService.LOGIN_PASSWORD | |
57 | }) | |
58 | ||
59 | this.input.nativeElement.focus() | |
60 | } | |
61 | ||
62 | login () { | |
63 | this.error = null | |
64 | ||
65 | const { username, password } = this.form.value | |
66 | ||
67 | this.authService.login(username, password) | |
68 | .subscribe( | |
69 | () => this.redirectService.redirectToPreviousRoute(), | |
70 | ||
71 | err => { | |
72 | if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.') | |
73 | else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.') | |
74 | else this.error = err.message | |
75 | } | |
76 | ) | |
77 | } | |
78 | ||
79 | askResetPassword () { | |
80 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
81 | .subscribe( | |
82 | () => { | |
83 | const message = this.i18n( | |
84 | 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.', | |
85 | { email: this.forgotPasswordEmail } | |
86 | ) | |
87 | this.notifier.success(message) | |
88 | this.hideForgotPasswordModal() | |
89 | }, | |
90 | ||
91 | err => this.notifier.error(err.message) | |
92 | ) | |
93 | } | |
94 | ||
95 | openForgotPasswordModal () { | |
96 | this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal) | |
97 | } | |
98 | ||
99 | hideForgotPasswordModal () { | |
100 | this.openedForgotPasswordModal.close() | |
101 | } | |
102 | } |