aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/login/login.component.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/login/login.component.ts')
-rw-r--r--client/src/app/login/login.component.ts58
1 files changed, 45 insertions, 13 deletions
diff --git a/client/src/app/login/login.component.ts b/client/src/app/login/login.component.ts
index ddd62462e..c4ff7050b 100644
--- a/client/src/app/login/login.component.ts
+++ b/client/src/app/login/login.component.ts
@@ -1,35 +1,67 @@
1import { Component } from '@angular/core'; 1import { Component, OnInit } from '@angular/core';
2import { FormBuilder, FormGroup, Validators } from '@angular/forms';
2import { Router } from '@angular/router'; 3import { Router } from '@angular/router';
3 4
4import { AuthService } from '../shared'; 5import { AuthService, FormReactive } from '../shared';
5 6
6@Component({ 7@Component({
7 selector: 'my-login', 8 selector: 'my-login',
8 template: require('./login.component.html') 9 templateUrl: './login.component.html'
9}) 10})
10 11
11export class LoginComponent { 12export class LoginComponent extends FormReactive implements OnInit {
12 error: string = null; 13 error: string = null;
13 14
15 form: FormGroup;
16 formErrors = {
17 'username': '',
18 'password': ''
19 };
20 validationMessages = {
21 'username': {
22 'required': 'Username is required.',
23 },
24 'password': {
25 'required': 'Password is required.'
26 }
27 };
28
14 constructor( 29 constructor(
15 private authService: AuthService, 30 private authService: AuthService,
31 private formBuilder: FormBuilder,
16 private router: Router 32 private router: Router
17 ) {} 33 ) {
34 super();
35 }
36
37 buildForm() {
38 this.form = this.formBuilder.group({
39 username: [ '', Validators.required ],
40 password: [ '', Validators.required ],
41 });
42
43 this.form.valueChanges.subscribe(data => this.onValueChanged(data));
44 }
45
46 ngOnInit() {
47 this.buildForm();
48 }
49
50 login() {
51 this.error = null;
52
53 const { username, password } = this.form.value;
18 54
19 login(username: string, password: string) {
20 this.authService.login(username, password).subscribe( 55 this.authService.login(username, password).subscribe(
21 result => { 56 result => this.router.navigate(['/videos/list']),
22 this.error = null;
23 57
24 this.router.navigate(['/videos/list']);
25 },
26 error => { 58 error => {
27 console.error(error); 59 console.error(error.json);
28 60
29 if (error.error === 'invalid_grant') { 61 if (error.json.error === 'invalid_grant') {
30 this.error = 'Credentials are invalid.'; 62 this.error = 'Credentials are invalid.';
31 } else { 63 } else {
32 this.error = `${error.error}: ${error.error_description}`; 64 this.error = `${error.json.error}: ${error.json.error_description}`;
33 } 65 }
34 } 66 }
35 ); 67 );