Merge branch 'develop' into pr/1217
[oweals/peertube.git] / client / src / app / login / login.component.ts
1 import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2 import { Notifier, RedirectService, ServerService } from '@app/core'
3 import { UserService } from '@app/shared'
4 import { AuthService } from '../core'
5 import { FormReactive } from '../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
8 import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service'
9 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
10 import { Router } from '@angular/router'
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('emailInput') input: ElementRef
20   @ViewChild('forgotPasswordModal') forgotPasswordModal: ElementRef
21
22   error: string = null
23   forgotPasswordEmail = ''
24
25   private openedForgotPasswordModal: NgbModalRef
26
27   constructor (
28     public router: Router,
29     protected formValidatorService: FormValidatorService,
30     private modalService: NgbModal,
31     private loginValidatorsService: LoginValidatorsService,
32     private authService: AuthService,
33     private userService: UserService,
34     private serverService: ServerService,
35     private redirectService: RedirectService,
36     private notifier: Notifier,
37     private i18n: I18n
38   ) {
39     super()
40   }
41
42   get signupAllowed () {
43     return this.serverService.getConfig().signup.allowed === true
44   }
45
46   isEmailDisabled () {
47     return this.serverService.getConfig().email.enabled === false
48   }
49
50   ngOnInit () {
51     this.buildForm({
52       username: this.loginValidatorsService.LOGIN_USERNAME,
53       password: this.loginValidatorsService.LOGIN_PASSWORD
54     })
55
56     this.input.nativeElement.focus()
57   }
58
59   login () {
60     this.error = null
61
62     const { username, password } = this.form.value
63
64     this.authService.login(username, password)
65       .subscribe(
66         () => this.redirectService.redirectToPreviousRoute(),
67
68         err => {
69           if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
70           else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.')
71           else this.error = err.message
72         }
73       )
74   }
75
76   askResetPassword () {
77     this.userService.askResetPassword(this.forgotPasswordEmail)
78       .subscribe(
79         () => {
80           const message = this.i18n(
81             'An email with the reset password instructions will be sent to {{email}}.',
82             { email: this.forgotPasswordEmail }
83           )
84           this.notifier.success(message)
85           this.hideForgotPasswordModal()
86         },
87
88         err => this.notifier.error(err.message)
89       )
90   }
91
92   openForgotPasswordModal () {
93     this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
94   }
95
96   hideForgotPasswordModal () {
97     this.openedForgotPasswordModal.close()
98   }
99 }