]>
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', |
d235f6b0 C |
10 | templateUrl: './login.component.html', |
11 | styleUrls: [ './login.component.scss' ] | |
b1794c53 C |
12 | }) |
13 | ||
4b2f33f3 | 14 | export class LoginComponent extends FormReactive implements OnInit { |
df98563e | 15 | error: string = null |
4b2f33f3 | 16 | |
df98563e | 17 | form: FormGroup |
4b2f33f3 C |
18 | formErrors = { |
19 | 'username': '', | |
20 | 'password': '' | |
df98563e | 21 | } |
4b2f33f3 C |
22 | validationMessages = { |
23 | 'username': { | |
df98563e | 24 | 'required': 'Username is required.' |
4b2f33f3 C |
25 | }, |
26 | 'password': { | |
27 | 'required': 'Password is required.' | |
28 | } | |
df98563e | 29 | } |
192ea60b | 30 | |
df98563e | 31 | constructor ( |
4fd8aa32 | 32 | private authService: AuthService, |
4b2f33f3 | 33 | private formBuilder: FormBuilder, |
4fd8aa32 | 34 | private router: Router |
4b2f33f3 | 35 | ) { |
df98563e | 36 | super() |
4b2f33f3 | 37 | } |
b1794c53 | 38 | |
df98563e | 39 | buildForm () { |
4b2f33f3 C |
40 | this.form = this.formBuilder.group({ |
41 | username: [ '', Validators.required ], | |
df98563e C |
42 | password: [ '', Validators.required ] |
43 | }) | |
4b2f33f3 | 44 | |
df98563e | 45 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
4b2f33f3 C |
46 | } |
47 | ||
df98563e C |
48 | ngOnInit () { |
49 | this.buildForm() | |
0f6da32b C |
50 | } |
51 | ||
df98563e C |
52 | login () { |
53 | this.error = null | |
4b2f33f3 | 54 | |
df98563e | 55 | const { username, password } = this.form.value |
4b2f33f3 C |
56 | |
57 | this.authService.login(username, password).subscribe( | |
58 | result => this.router.navigate(['/videos/list']), | |
192ea60b | 59 | |
c9d6d155 C |
60 | err => { |
61 | if (err.message === 'invalid_grant') { | |
df98563e | 62 | this.error = 'Credentials are invalid.' |
2e2bef6f | 63 | } else { |
c9d6d155 | 64 | this.error = `${err.body.error_description}` |
1553e15d C |
65 | } |
66 | } | |
df98563e | 67 | ) |
b1794c53 C |
68 | } |
69 | } |