]>
Commit | Line | Data |
---|---|---|
ecb4e35f | 1 | import { Component, ElementRef, OnInit, ViewChild } from '@angular/core' |
000eb0e4 | 2 | import { Notifier, RedirectService } 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' |
000eb0e4 RK |
10 | import { ActivatedRoute } from '@angular/router' |
11 | import { ServerConfig } from '@shared/models/server/server-config.model' | |
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 | 31 | private route: ActivatedRoute, |
63347a0f | 32 | private modalService: NgbModal, |
e309822b | 33 | private loginValidatorsService: LoginValidatorsService, |
b1d40cff C |
34 | private authService: AuthService, |
35 | private userService: UserService, | |
b1d40cff | 36 | private redirectService: RedirectService, |
f8b2c1b4 | 37 | private notifier: Notifier, |
b1d40cff C |
38 | private i18n: I18n |
39 | ) { | |
df98563e | 40 | super() |
4b2f33f3 | 41 | } |
b1794c53 | 42 | |
2b084d70 | 43 | get signupAllowed () { |
ba430d75 | 44 | return this.serverConfig.signup.allowed === true |
2b084d70 C |
45 | } |
46 | ||
3b3b1820 | 47 | isEmailDisabled () { |
ba430d75 | 48 | return this.serverConfig.email.enabled === false |
3b3b1820 C |
49 | } |
50 | ||
df98563e | 51 | ngOnInit () { |
ba430d75 C |
52 | this.serverConfig = this.route.snapshot.data.serverConfig |
53 | ||
d18d6478 | 54 | this.buildForm({ |
e309822b C |
55 | username: this.loginValidatorsService.LOGIN_USERNAME, |
56 | password: this.loginValidatorsService.LOGIN_PASSWORD | |
d18d6478 | 57 | }) |
9fe44067 RK |
58 | |
59 | this.input.nativeElement.focus() | |
0f6da32b C |
60 | } |
61 | ||
df98563e C |
62 | login () { |
63 | this.error = null | |
4b2f33f3 | 64 | |
df98563e | 65 | const { username, password } = this.form.value |
4b2f33f3 | 66 | |
2b084d70 C |
67 | this.authService.login(username, password) |
68 | .subscribe( | |
dae5ca24 | 69 | () => this.redirectService.redirectToPreviousRoute(), |
192ea60b | 70 | |
e6921918 C |
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 | } | |
2b084d70 | 76 | ) |
b1794c53 | 77 | } |
ecb4e35f C |
78 | |
79 | askResetPassword () { | |
80 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
81 | .subscribe( | |
141b177d | 82 | () => { |
b1d40cff | 83 | const message = this.i18n( |
f88ee4a9 | 84 | 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.', |
b1d40cff C |
85 | { email: this.forgotPasswordEmail } |
86 | ) | |
f8b2c1b4 | 87 | this.notifier.success(message) |
ecb4e35f C |
88 | this.hideForgotPasswordModal() |
89 | }, | |
90 | ||
f8b2c1b4 | 91 | err => this.notifier.error(err.message) |
ecb4e35f C |
92 | ) |
93 | } | |
94 | ||
ecb4e35f | 95 | openForgotPasswordModal () { |
63347a0f | 96 | this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal) |
ecb4e35f C |
97 | } |
98 | ||
99 | hideForgotPasswordModal () { | |
63347a0f | 100 | this.openedForgotPasswordModal.close() |
ecb4e35f | 101 | } |
b1794c53 | 102 | } |