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