]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/login/login.component.ts
Client: reactive forms
[github/Chocobozzz/PeerTube.git] / client / src / app / login / login.component.ts
CommitLineData
0f6da32b 1import { Component, OnInit } from '@angular/core';
4b2f33f3 2import { FormBuilder, FormGroup, Validators } from '@angular/forms';
00a44645 3import { Router } from '@angular/router';
b1794c53 4
4b2f33f3 5import { AuthService, FormReactive } from '../shared';
b1794c53
C
6
7@Component({
a840d396 8 selector: 'my-login',
ab32b0fc 9 template: require('./login.component.html')
b1794c53
C
10})
11
4b2f33f3 12export class LoginComponent extends FormReactive implements OnInit {
192ea60b 13 error: string = null;
4b2f33f3
C
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 };
192ea60b 28
4fd8aa32
C
29 constructor(
30 private authService: AuthService,
4b2f33f3 31 private formBuilder: FormBuilder,
4fd8aa32 32 private router: Router
4b2f33f3
C
33 ) {
34 super();
35 }
b1794c53 36
4b2f33f3
C
37 buildForm() {
38 this.form = this.formBuilder.group({
39 username: [ '', Validators.required ],
40 password: [ '', Validators.required ],
0f6da32b 41 });
4b2f33f3
C
42
43 this.form.valueChanges.subscribe(data => this.onValueChanged(data));
44 }
45
46 ngOnInit() {
47 this.buildForm();
0f6da32b
C
48 }
49
50 login() {
4b2f33f3
C
51 this.error = null;
52
53 const { username, password } = this.form.value;
54
55 this.authService.login(username, password).subscribe(
56 result => this.router.navigate(['/videos/list']),
192ea60b 57
1553e15d 58 error => {
de59c48f 59 console.error(error.json);
bd5c83a8 60
de59c48f 61 if (error.json.error === 'invalid_grant') {
192ea60b 62 this.error = 'Credentials are invalid.';
2e2bef6f 63 } else {
de59c48f 64 this.error = `${error.json.error}: ${error.json.error_description}`;
1553e15d
C
65 }
66 }
b1794c53
C
67 );
68 }
69}