]>
Commit | Line | Data |
---|---|---|
ecb4e35f | 1 | import { Component, ElementRef, OnInit, ViewChild } from '@angular/core' |
f8b2c1b4 | 2 | import { Notifier, RedirectService, ServerService } from '@app/core' |
ecb4e35f | 3 | import { UserService } from '@app/shared' |
df98563e C |
4 | import { AuthService } from '../core' |
5 | import { FormReactive } from '../shared' | |
b1d40cff | 6 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 7 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
e309822b | 8 | import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service' |
63347a0f | 9 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' |
ba430d75 C |
10 | import { ActivatedRoute, Router } from '@angular/router' |
11 | import { ServerConfig } from '@shared/models' | |
b1794c53 C |
12 | |
13 | @Component({ | |
a840d396 | 14 | selector: 'my-login', |
d235f6b0 C |
15 | templateUrl: './login.component.html', |
16 | styleUrls: [ './login.component.scss' ] | |
b1794c53 C |
17 | }) |
18 | ||
4b2f33f3 | 19 | export class LoginComponent extends FormReactive implements OnInit { |
f36da21e C |
20 | @ViewChild('emailInput', { static: true }) input: ElementRef |
21 | @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef | |
ecb4e35f | 22 | |
df98563e | 23 | error: string = null |
ecb4e35f | 24 | forgotPasswordEmail = '' |
192ea60b | 25 | |
63347a0f | 26 | private openedForgotPasswordModal: NgbModalRef |
ba430d75 | 27 | private serverConfig: ServerConfig |
63347a0f | 28 | |
b1d40cff | 29 | constructor ( |
d18d6478 | 30 | protected formValidatorService: FormValidatorService, |
ba430d75 C |
31 | private router: Router, |
32 | private route: ActivatedRoute, | |
63347a0f | 33 | private modalService: NgbModal, |
e309822b | 34 | private loginValidatorsService: LoginValidatorsService, |
b1d40cff C |
35 | private authService: AuthService, |
36 | private userService: UserService, | |
37 | private serverService: ServerService, | |
38 | private redirectService: RedirectService, | |
f8b2c1b4 | 39 | private notifier: Notifier, |
b1d40cff C |
40 | private i18n: I18n |
41 | ) { | |
df98563e | 42 | super() |
4b2f33f3 | 43 | } |
b1794c53 | 44 | |
2b084d70 | 45 | get signupAllowed () { |
ba430d75 | 46 | return this.serverConfig.signup.allowed === true |
2b084d70 C |
47 | } |
48 | ||
3b3b1820 | 49 | isEmailDisabled () { |
ba430d75 | 50 | return this.serverConfig.email.enabled === false |
3b3b1820 C |
51 | } |
52 | ||
df98563e | 53 | ngOnInit () { |
ba430d75 C |
54 | this.serverConfig = this.route.snapshot.data.serverConfig |
55 | ||
d18d6478 | 56 | this.buildForm({ |
e309822b C |
57 | username: this.loginValidatorsService.LOGIN_USERNAME, |
58 | password: this.loginValidatorsService.LOGIN_PASSWORD | |
d18d6478 | 59 | }) |
9fe44067 RK |
60 | |
61 | this.input.nativeElement.focus() | |
0f6da32b C |
62 | } |
63 | ||
df98563e C |
64 | login () { |
65 | this.error = null | |
4b2f33f3 | 66 | |
df98563e | 67 | const { username, password } = this.form.value |
4b2f33f3 | 68 | |
2b084d70 C |
69 | this.authService.login(username, password) |
70 | .subscribe( | |
dae5ca24 | 71 | () => this.redirectService.redirectToPreviousRoute(), |
192ea60b | 72 | |
e6921918 C |
73 | err => { |
74 | if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.') | |
75 | else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.') | |
76 | else this.error = err.message | |
77 | } | |
2b084d70 | 78 | ) |
b1794c53 | 79 | } |
ecb4e35f C |
80 | |
81 | askResetPassword () { | |
82 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
83 | .subscribe( | |
141b177d | 84 | () => { |
b1d40cff | 85 | const message = this.i18n( |
f88ee4a9 | 86 | 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.', |
b1d40cff C |
87 | { email: this.forgotPasswordEmail } |
88 | ) | |
f8b2c1b4 | 89 | this.notifier.success(message) |
ecb4e35f C |
90 | this.hideForgotPasswordModal() |
91 | }, | |
92 | ||
f8b2c1b4 | 93 | err => this.notifier.error(err.message) |
ecb4e35f C |
94 | ) |
95 | } | |
96 | ||
ecb4e35f | 97 | openForgotPasswordModal () { |
63347a0f | 98 | this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal) |
ecb4e35f C |
99 | } |
100 | ||
101 | hideForgotPasswordModal () { | |
63347a0f | 102 | this.openedForgotPasswordModal.close() |
ecb4e35f | 103 | } |
b1794c53 | 104 | } |