]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/login/login.component.ts
Add delete button to my videos
[github/Chocobozzz/PeerTube.git] / client / src / app / login / login.component.ts
CommitLineData
df98563e
C
1import { Component, OnInit } from '@angular/core'
2import { FormBuilder, FormGroup, Validators } from '@angular/forms'
3import { Router } from '@angular/router'
b1794c53 4
df98563e
C
5import { AuthService } from '../core'
6import { 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 14export 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}