]>
Commit | Line | Data |
---|---|---|
ecb4e35f | 1 | import { Component, ElementRef, OnInit, ViewChild } from '@angular/core' |
2b084d70 | 2 | import { RedirectService, ServerService } from '@app/core' |
ecb4e35f C |
3 | import { UserService } from '@app/shared' |
4 | import { NotificationsService } from 'angular2-notifications' | |
df98563e C |
5 | import { AuthService } from '../core' |
6 | import { FormReactive } from '../shared' | |
b1d40cff | 7 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 8 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
e309822b | 9 | import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service' |
63347a0f | 10 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' |
ec769c89 | 11 | import { Router } from '@angular/router' |
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 { |
9fe44067 | 20 | @ViewChild('emailInput') input: ElementRef |
63347a0f | 21 | @ViewChild('forgotPasswordModal') forgotPasswordModal: ElementRef |
ecb4e35f | 22 | |
df98563e | 23 | error: string = null |
ecb4e35f | 24 | forgotPasswordEmail = '' |
192ea60b | 25 | |
63347a0f C |
26 | private openedForgotPasswordModal: NgbModalRef |
27 | ||
b1d40cff | 28 | constructor ( |
ec769c89 | 29 | public router: Router, |
d18d6478 | 30 | protected formValidatorService: FormValidatorService, |
63347a0f | 31 | private modalService: NgbModal, |
e309822b | 32 | private loginValidatorsService: LoginValidatorsService, |
b1d40cff C |
33 | private authService: AuthService, |
34 | private userService: UserService, | |
35 | private serverService: ServerService, | |
36 | private redirectService: RedirectService, | |
37 | private notificationsService: NotificationsService, | |
b1d40cff C |
38 | private i18n: I18n |
39 | ) { | |
df98563e | 40 | super() |
4b2f33f3 | 41 | } |
b1794c53 | 42 | |
2b084d70 C |
43 | get signupAllowed () { |
44 | return this.serverService.getConfig().signup.allowed === true | |
45 | } | |
46 | ||
3b3b1820 C |
47 | isEmailDisabled () { |
48 | return this.serverService.getConfig().email.enabled === false | |
49 | } | |
50 | ||
df98563e | 51 | ngOnInit () { |
d18d6478 | 52 | this.buildForm({ |
e309822b C |
53 | username: this.loginValidatorsService.LOGIN_USERNAME, |
54 | password: this.loginValidatorsService.LOGIN_PASSWORD | |
d18d6478 | 55 | }) |
9fe44067 RK |
56 | |
57 | this.input.nativeElement.focus() | |
0f6da32b C |
58 | } |
59 | ||
df98563e C |
60 | login () { |
61 | this.error = null | |
4b2f33f3 | 62 | |
df98563e | 63 | const { username, password } = this.form.value |
4b2f33f3 | 64 | |
2b084d70 C |
65 | this.authService.login(username, password) |
66 | .subscribe( | |
ec769c89 | 67 | () => this.redirect(), |
192ea60b | 68 | |
e6921918 C |
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 | } | |
2b084d70 | 74 | ) |
b1794c53 | 75 | } |
ecb4e35f | 76 | |
ec769c89 B |
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 | ||
ecb4e35f C |
86 | askResetPassword () { |
87 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
88 | .subscribe( | |
141b177d | 89 | () => { |
b1d40cff | 90 | const message = this.i18n( |
51d4bcad | 91 | 'An email with the reset password instructions will be sent to {{email}}.', |
b1d40cff C |
92 | { email: this.forgotPasswordEmail } |
93 | ) | |
94 | this.notificationsService.success(this.i18n('Success'), message) | |
ecb4e35f C |
95 | this.hideForgotPasswordModal() |
96 | }, | |
97 | ||
b1d40cff | 98 | err => this.notificationsService.error(this.i18n('Error'), err.message) |
ecb4e35f C |
99 | ) |
100 | } | |
101 | ||
ecb4e35f | 102 | openForgotPasswordModal () { |
63347a0f | 103 | this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal) |
ecb4e35f C |
104 | } |
105 | ||
106 | hideForgotPasswordModal () { | |
63347a0f | 107 | this.openedForgotPasswordModal.close() |
ecb4e35f | 108 | } |
b1794c53 | 109 | } |