]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/login/login.component.ts
Upgrade to bootstrap 4 first step
[github/Chocobozzz/PeerTube.git] / client / src / app / login / login.component.ts
... / ...
CommitLineData
1import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2import { RedirectService, ServerService } from '@app/core'
3import { UserService } from '@app/shared'
4import { NotificationsService } from 'angular2-notifications'
5import { ModalDirective } from 'ngx-bootstrap/modal'
6import { AuthService } from '../core'
7import { FormReactive } from '../shared'
8import { I18n } from '@ngx-translate/i18n-polyfill'
9import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
10import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service'
11
12@Component({
13 selector: 'my-login',
14 templateUrl: './login.component.html',
15 styleUrls: [ './login.component.scss' ]
16})
17
18export 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 loginValidatorsService: LoginValidatorsService,
28 private authService: AuthService,
29 private userService: UserService,
30 private serverService: ServerService,
31 private redirectService: RedirectService,
32 private notificationsService: NotificationsService,
33 private i18n: I18n
34 ) {
35 super()
36 }
37
38 get signupAllowed () {
39 return this.serverService.getConfig().signup.allowed === true
40 }
41
42 ngOnInit () {
43 this.buildForm({
44 username: this.loginValidatorsService.LOGIN_USERNAME,
45 password: this.loginValidatorsService.LOGIN_PASSWORD
46 })
47 }
48
49 login () {
50 this.error = null
51
52 const { username, password } = this.form.value
53
54 this.authService.login(username, password)
55 .subscribe(
56 () => this.redirectService.redirectToHomepage(),
57
58 err => {
59 if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
60 else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.')
61 else this.error = err.message
62 }
63 )
64 }
65
66 askResetPassword () {
67 this.userService.askResetPassword(this.forgotPasswordEmail)
68 .subscribe(
69 res => {
70 const message = this.i18n(
71 'An email with the reset password instructions will be sent to {{email}}.',
72 { email: this.forgotPasswordEmail }
73 )
74 this.notificationsService.success(this.i18n('Success'), message)
75 this.hideForgotPasswordModal()
76 },
77
78 err => this.notificationsService.error(this.i18n('Error'), err.message)
79 )
80 }
81
82 onForgotPasswordModalShown () {
83 this.forgotPasswordEmailInput.nativeElement.focus()
84 }
85
86 openForgotPasswordModal () {
87 this.forgotPasswordModal.show()
88 }
89
90 hideForgotPasswordModal () {
91 this.forgotPasswordModal.hide()
92 }
93}