]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/login/login.component.ts
Add link to register in login form
[github/Chocobozzz/PeerTube.git] / client / src / app / login / login.component.ts
CommitLineData
ecb4e35f 1import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
df98563e
C
2import { FormBuilder, FormGroup, Validators } from '@angular/forms'
3import { Router } from '@angular/router'
2b084d70 4import { RedirectService, ServerService } from '@app/core'
ecb4e35f
C
5import { UserService } from '@app/shared'
6import { NotificationsService } from 'angular2-notifications'
7import { ModalDirective } from 'ngx-bootstrap/modal'
df98563e
C
8import { AuthService } from '../core'
9import { FormReactive } from '../shared'
b1794c53
C
10
11@Component({
a840d396 12 selector: 'my-login',
d235f6b0
C
13 templateUrl: './login.component.html',
14 styleUrls: [ './login.component.scss' ]
b1794c53
C
15})
16
4b2f33f3 17export class LoginComponent extends FormReactive implements OnInit {
ecb4e35f
C
18 @ViewChild('forgotPasswordModal') forgotPasswordModal: ModalDirective
19 @ViewChild('forgotPasswordEmailInput') forgotPasswordEmailInput: ElementRef
20
df98563e 21 error: string = null
4b2f33f3 22
df98563e 23 form: FormGroup
4b2f33f3
C
24 formErrors = {
25 'username': '',
26 'password': ''
df98563e 27 }
4b2f33f3
C
28 validationMessages = {
29 'username': {
df98563e 30 'required': 'Username is required.'
4b2f33f3
C
31 },
32 'password': {
33 'required': 'Password is required.'
34 }
df98563e 35 }
ecb4e35f 36 forgotPasswordEmail = ''
192ea60b 37
2b084d70
C
38 constructor (private authService: AuthService,
39 private userService: UserService,
40 private serverService: ServerService,
41 private redirectService: RedirectService,
42 private notificationsService: NotificationsService,
43 private formBuilder: FormBuilder,
44 private router: Router) {
df98563e 45 super()
4b2f33f3 46 }
b1794c53 47
2b084d70
C
48 get signupAllowed () {
49 return this.serverService.getConfig().signup.allowed === true
50 }
51
df98563e 52 buildForm () {
4b2f33f3
C
53 this.form = this.formBuilder.group({
54 username: [ '', Validators.required ],
df98563e
C
55 password: [ '', Validators.required ]
56 })
4b2f33f3 57
df98563e 58 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
4b2f33f3
C
59 }
60
df98563e
C
61 ngOnInit () {
62 this.buildForm()
0f6da32b
C
63 }
64
df98563e
C
65 login () {
66 this.error = null
4b2f33f3 67
df98563e 68 const { username, password } = this.form.value
4b2f33f3 69
2b084d70
C
70 this.authService.login(username, password)
71 .subscribe(
72 () => this.redirectService.redirectToHomepage(),
192ea60b 73
2b084d70
C
74 err => this.error = err.message
75 )
b1794c53 76 }
ecb4e35f
C
77
78 askResetPassword () {
79 this.userService.askResetPassword(this.forgotPasswordEmail)
80 .subscribe(
81 res => {
82 const message = `An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.`
83 this.notificationsService.success('Success', message)
84 this.hideForgotPasswordModal()
85 },
86
87 err => this.notificationsService.error('Error', err.message)
88 )
89 }
90
91 onForgotPasswordModalShown () {
92 this.forgotPasswordEmailInput.nativeElement.focus()
93 }
94
95 openForgotPasswordModal () {
96 this.forgotPasswordModal.show()
97 }
98
99 hideForgotPasswordModal () {
100 this.forgotPasswordModal.hide()
101 }
b1794c53 102}