]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/login/login.component.ts
f7aad06e89075a56283dc492d46ace00f70793d7
[github/Chocobozzz/PeerTube.git] / client / src / app / login / login.component.ts
1 import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2 import { RedirectService, ServerService } from '@app/core'
3 import { UserService } from '@app/shared'
4 import { NotificationsService } from 'angular2-notifications'
5 import { ModalDirective } from 'ngx-bootstrap/modal'
6 import { AuthService } from '../core'
7 import { FormReactive } from '../shared'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
10 import { LOGIN_PASSWORD, LOGIN_USERNAME } from '@app/shared/forms/form-validators/login'
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 forgotPasswordEmail = ''
24
25 constructor (
26 protected formValidatorService: FormValidatorService,
27 private authService: AuthService,
28 private userService: UserService,
29 private serverService: ServerService,
30 private redirectService: RedirectService,
31 private notificationsService: NotificationsService,
32 private i18n: I18n
33 ) {
34 super()
35 }
36
37 get signupAllowed () {
38 return this.serverService.getConfig().signup.allowed === true
39 }
40
41 ngOnInit () {
42 this.buildForm({
43 username: LOGIN_USERNAME,
44 password: LOGIN_PASSWORD
45 })
46 }
47
48 login () {
49 this.error = null
50
51 const { username, password } = this.form.value
52
53 this.authService.login(username, password)
54 .subscribe(
55 () => this.redirectService.redirectToHomepage(),
56
57 err => this.error = err.message
58 )
59 }
60
61 askResetPassword () {
62 this.userService.askResetPassword(this.forgotPasswordEmail)
63 .subscribe(
64 res => {
65 const message = this.i18n(
66 'An email with the reset password instructions will be sent to {{ email }}.',
67 { email: this.forgotPasswordEmail }
68 )
69 this.notificationsService.success(this.i18n('Success'), message)
70 this.hideForgotPasswordModal()
71 },
72
73 err => this.notificationsService.error(this.i18n('Error'), err.message)
74 )
75 }
76
77 onForgotPasswordModalShown () {
78 this.forgotPasswordEmailInput.nativeElement.focus()
79 }
80
81 openForgotPasswordModal () {
82 this.forgotPasswordModal.show()
83 }
84
85 hideForgotPasswordModal () {
86 this.forgotPasswordModal.hide()
87 }
88 }