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