]>
Commit | Line | Data |
---|---|---|
ecb4e35f | 1 | import { Component, ElementRef, OnInit, ViewChild } from '@angular/core' |
df98563e C |
2 | import { FormBuilder, FormGroup, Validators } from '@angular/forms' |
3 | import { Router } from '@angular/router' | |
2b084d70 | 4 | import { RedirectService, ServerService } from '@app/core' |
ecb4e35f C |
5 | import { UserService } from '@app/shared' |
6 | import { NotificationsService } from 'angular2-notifications' | |
7 | import { ModalDirective } from 'ngx-bootstrap/modal' | |
df98563e C |
8 | import { AuthService } from '../core' |
9 | import { FormReactive } from '../shared' | |
b1d40cff | 10 | import { I18n } from '@ngx-translate/i18n-polyfill' |
b1794c53 C |
11 | |
12 | @Component({ | |
a840d396 | 13 | selector: 'my-login', |
d235f6b0 C |
14 | templateUrl: './login.component.html', |
15 | styleUrls: [ './login.component.scss' ] | |
b1794c53 C |
16 | }) |
17 | ||
4b2f33f3 | 18 | export class LoginComponent extends FormReactive implements OnInit { |
ecb4e35f C |
19 | @ViewChild('forgotPasswordModal') forgotPasswordModal: ModalDirective |
20 | @ViewChild('forgotPasswordEmailInput') forgotPasswordEmailInput: ElementRef | |
21 | ||
df98563e | 22 | error: string = null |
4b2f33f3 | 23 | |
df98563e | 24 | form: FormGroup |
4b2f33f3 C |
25 | formErrors = { |
26 | 'username': '', | |
27 | 'password': '' | |
df98563e | 28 | } |
4b2f33f3 C |
29 | validationMessages = { |
30 | 'username': { | |
df98563e | 31 | 'required': 'Username is required.' |
4b2f33f3 C |
32 | }, |
33 | 'password': { | |
34 | 'required': 'Password is required.' | |
35 | } | |
df98563e | 36 | } |
ecb4e35f | 37 | forgotPasswordEmail = '' |
192ea60b | 38 | |
b1d40cff C |
39 | constructor ( |
40 | private authService: AuthService, | |
41 | private userService: UserService, | |
42 | private serverService: ServerService, | |
43 | private redirectService: RedirectService, | |
44 | private notificationsService: NotificationsService, | |
45 | private formBuilder: FormBuilder, | |
46 | private i18n: I18n | |
47 | ) { | |
df98563e | 48 | super() |
4b2f33f3 | 49 | } |
b1794c53 | 50 | |
2b084d70 C |
51 | get signupAllowed () { |
52 | return this.serverService.getConfig().signup.allowed === true | |
53 | } | |
54 | ||
df98563e | 55 | buildForm () { |
4b2f33f3 C |
56 | this.form = this.formBuilder.group({ |
57 | username: [ '', Validators.required ], | |
df98563e C |
58 | password: [ '', Validators.required ] |
59 | }) | |
4b2f33f3 | 60 | |
df98563e | 61 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
4b2f33f3 C |
62 | } |
63 | ||
df98563e C |
64 | ngOnInit () { |
65 | this.buildForm() | |
0f6da32b C |
66 | } |
67 | ||
df98563e C |
68 | login () { |
69 | this.error = null | |
4b2f33f3 | 70 | |
df98563e | 71 | const { username, password } = this.form.value |
4b2f33f3 | 72 | |
2b084d70 C |
73 | this.authService.login(username, password) |
74 | .subscribe( | |
75 | () => this.redirectService.redirectToHomepage(), | |
192ea60b | 76 | |
2b084d70 C |
77 | err => this.error = err.message |
78 | ) | |
b1794c53 | 79 | } |
ecb4e35f C |
80 | |
81 | askResetPassword () { | |
82 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
83 | .subscribe( | |
84 | res => { | |
b1d40cff C |
85 | const message = this.i18n( |
86 | 'An email with the reset password instructions will be sent to {{ email }}.', | |
87 | { email: this.forgotPasswordEmail } | |
88 | ) | |
89 | this.notificationsService.success(this.i18n('Success'), message) | |
ecb4e35f C |
90 | this.hideForgotPasswordModal() |
91 | }, | |
92 | ||
b1d40cff | 93 | err => this.notificationsService.error(this.i18n('Error'), err.message) |
ecb4e35f C |
94 | ) |
95 | } | |
96 | ||
97 | onForgotPasswordModalShown () { | |
98 | this.forgotPasswordEmailInput.nativeElement.focus() | |
99 | } | |
100 | ||
101 | openForgotPasswordModal () { | |
102 | this.forgotPasswordModal.show() | |
103 | } | |
104 | ||
105 | hideForgotPasswordModal () { | |
106 | this.forgotPasswordModal.hide() | |
107 | } | |
b1794c53 | 108 | } |