]>
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' | |
b1794c53 C |
10 | |
11 | @Component({ | |
a840d396 | 12 | selector: 'my-login', |
d235f6b0 C |
13 | templateUrl: './login.component.html', |
14 | styleUrls: [ './login.component.scss' ] | |
b1794c53 C |
15 | }) |
16 | ||
4b2f33f3 | 17 | export class LoginComponent extends FormReactive implements OnInit { |
ecb4e35f C |
18 | @ViewChild('forgotPasswordModal') forgotPasswordModal: ModalDirective |
19 | @ViewChild('forgotPasswordEmailInput') forgotPasswordEmailInput: ElementRef | |
20 | ||
df98563e | 21 | error: string = null |
4b2f33f3 | 22 | |
df98563e | 23 | form: FormGroup |
4b2f33f3 C |
24 | formErrors = { |
25 | 'username': '', | |
26 | 'password': '' | |
df98563e | 27 | } |
4b2f33f3 C |
28 | validationMessages = { |
29 | 'username': { | |
df98563e | 30 | 'required': 'Username is required.' |
4b2f33f3 C |
31 | }, |
32 | 'password': { | |
33 | 'required': 'Password is required.' | |
34 | } | |
df98563e | 35 | } |
ecb4e35f | 36 | forgotPasswordEmail = '' |
192ea60b | 37 | |
2b084d70 C |
38 | constructor (private authService: AuthService, |
39 | private userService: UserService, | |
40 | private serverService: ServerService, | |
41 | private redirectService: RedirectService, | |
42 | private notificationsService: NotificationsService, | |
13359203 | 43 | private formBuilder: FormBuilder) { |
df98563e | 44 | super() |
4b2f33f3 | 45 | } |
b1794c53 | 46 | |
2b084d70 C |
47 | get signupAllowed () { |
48 | return this.serverService.getConfig().signup.allowed === true | |
49 | } | |
50 | ||
df98563e | 51 | buildForm () { |
4b2f33f3 C |
52 | this.form = this.formBuilder.group({ |
53 | username: [ '', Validators.required ], | |
df98563e C |
54 | password: [ '', Validators.required ] |
55 | }) | |
4b2f33f3 | 56 | |
df98563e | 57 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
4b2f33f3 C |
58 | } |
59 | ||
df98563e C |
60 | ngOnInit () { |
61 | this.buildForm() | |
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( | |
71 | () => this.redirectService.redirectToHomepage(), | |
192ea60b | 72 | |
2b084d70 C |
73 | err => this.error = err.message |
74 | ) | |
b1794c53 | 75 | } |
ecb4e35f C |
76 | |
77 | askResetPassword () { | |
78 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
79 | .subscribe( | |
80 | res => { | |
81 | const message = `An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.` | |
82 | this.notificationsService.success('Success', message) | |
83 | this.hideForgotPasswordModal() | |
84 | }, | |
85 | ||
86 | err => this.notificationsService.error('Error', err.message) | |
87 | ) | |
88 | } | |
89 | ||
90 | onForgotPasswordModalShown () { | |
91 | this.forgotPasswordEmailInput.nativeElement.focus() | |
92 | } | |
93 | ||
94 | openForgotPasswordModal () { | |
95 | this.forgotPasswordModal.show() | |
96 | } | |
97 | ||
98 | hideForgotPasswordModal () { | |
99 | this.forgotPasswordModal.hide() | |
100 | } | |
b1794c53 | 101 | } |