]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/login/login.component.ts
autofocus first field on login
[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 { AuthService } from '../core'
6 import { FormReactive } from '../shared'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
9 import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service'
10 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
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 @ViewChild('forgotPasswordEmailInput') forgotPasswordEmailInput: ElementRef
22
23 error: string = null
24 forgotPasswordEmail = ''
25
26 private openedForgotPasswordModal: NgbModalRef
27
28 constructor (
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 notificationsService: NotificationsService,
37 private i18n: I18n
38 ) {
39 super()
40 }
41
42 get signupAllowed () {
43 return this.serverService.getConfig().signup.allowed === true
44 }
45
46 ngOnInit () {
47 this.buildForm({
48 username: this.loginValidatorsService.LOGIN_USERNAME,
49 password: this.loginValidatorsService.LOGIN_PASSWORD
50 })
51
52 this.input.nativeElement.focus()
53 }
54
55 login () {
56 this.error = null
57
58 const { username, password } = this.form.value
59
60 this.authService.login(username, password)
61 .subscribe(
62 () => this.redirectService.redirectToHomepage(),
63
64 err => {
65 if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
66 else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.')
67 else this.error = err.message
68 }
69 )
70 }
71
72 askResetPassword () {
73 this.userService.askResetPassword(this.forgotPasswordEmail)
74 .subscribe(
75 () => {
76 const message = this.i18n(
77 'An email with the reset password instructions will be sent to {{email}}.',
78 { email: this.forgotPasswordEmail }
79 )
80 this.notificationsService.success(this.i18n('Success'), message)
81 this.hideForgotPasswordModal()
82 },
83
84 err => this.notificationsService.error(this.i18n('Error'), err.message)
85 )
86 }
87
88 onForgotPasswordModalShown () {
89 this.forgotPasswordEmailInput.nativeElement.focus()
90 }
91
92 openForgotPasswordModal () {
93 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
94 }
95
96 hideForgotPasswordModal () {
97 this.openedForgotPasswordModal.close()
98 }
99 }