]>
Commit | Line | Data |
---|---|---|
df98563e C |
1 | import { Component, OnInit } from '@angular/core' |
2 | import { FormBuilder, FormGroup, Validators } from '@angular/forms' | |
3 | import { Router } from '@angular/router' | |
b1794c53 | 4 | |
df98563e C |
5 | import { AuthService } from '../core' |
6 | import { FormReactive } from '../shared' | |
b1794c53 C |
7 | |
8 | @Component({ | |
a840d396 | 9 | selector: 'my-login', |
ec8d8440 | 10 | templateUrl: './login.component.html' |
b1794c53 C |
11 | }) |
12 | ||
4b2f33f3 | 13 | export class LoginComponent extends FormReactive implements OnInit { |
df98563e | 14 | error: string = null |
4b2f33f3 | 15 | |
df98563e | 16 | form: FormGroup |
4b2f33f3 C |
17 | formErrors = { |
18 | 'username': '', | |
19 | 'password': '' | |
df98563e | 20 | } |
4b2f33f3 C |
21 | validationMessages = { |
22 | 'username': { | |
df98563e | 23 | 'required': 'Username is required.' |
4b2f33f3 C |
24 | }, |
25 | 'password': { | |
26 | 'required': 'Password is required.' | |
27 | } | |
df98563e | 28 | } |
192ea60b | 29 | |
df98563e | 30 | constructor ( |
4fd8aa32 | 31 | private authService: AuthService, |
4b2f33f3 | 32 | private formBuilder: FormBuilder, |
4fd8aa32 | 33 | private router: Router |
4b2f33f3 | 34 | ) { |
df98563e | 35 | super() |
4b2f33f3 | 36 | } |
b1794c53 | 37 | |
df98563e | 38 | buildForm () { |
4b2f33f3 C |
39 | this.form = this.formBuilder.group({ |
40 | username: [ '', Validators.required ], | |
df98563e C |
41 | password: [ '', Validators.required ] |
42 | }) | |
4b2f33f3 | 43 | |
df98563e | 44 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
4b2f33f3 C |
45 | } |
46 | ||
df98563e C |
47 | ngOnInit () { |
48 | this.buildForm() | |
0f6da32b C |
49 | } |
50 | ||
df98563e C |
51 | login () { |
52 | this.error = null | |
4b2f33f3 | 53 | |
df98563e | 54 | const { username, password } = this.form.value |
4b2f33f3 C |
55 | |
56 | this.authService.login(username, password).subscribe( | |
57 | result => this.router.navigate(['/videos/list']), | |
192ea60b | 58 | |
1553e15d | 59 | error => { |
df98563e | 60 | console.error(error.json) |
bd5c83a8 | 61 | |
de59c48f | 62 | if (error.json.error === 'invalid_grant') { |
df98563e | 63 | this.error = 'Credentials are invalid.' |
2e2bef6f | 64 | } else { |
df98563e | 65 | this.error = `${error.json.error}: ${error.json.error_description}` |
1553e15d C |
66 | } |
67 | } | |
df98563e | 68 | ) |
b1794c53 C |
69 | } |
70 | } |