]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
ecb4e35f 1import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2b084d70 2import { RedirectService, ServerService } from '@app/core'
ecb4e35f
C
3import { UserService } from '@app/shared'
4import { NotificationsService } from 'angular2-notifications'
5import { ModalDirective } from 'ngx-bootstrap/modal'
df98563e
C
6import { AuthService } from '../core'
7import { FormReactive } from '../shared'
b1d40cff 8import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478 9import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
e309822b 10import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service'
b1794c53
C
11
12@Component({
a840d396 13 selector: 'my-login',
d235f6b0
C
14 templateUrl: './login.component.html',
15 styleUrls: [ './login.component.scss' ]
b1794c53
C
16})
17
4b2f33f3 18export class LoginComponent extends FormReactive implements OnInit {
ecb4e35f
C
19 @ViewChild('forgotPasswordModal') forgotPasswordModal: ModalDirective
20 @ViewChild('forgotPasswordEmailInput') forgotPasswordEmailInput: ElementRef
21
df98563e 22 error: string = null
ecb4e35f 23 forgotPasswordEmail = ''
192ea60b 24
b1d40cff 25 constructor (
d18d6478 26 protected formValidatorService: FormValidatorService,
e309822b 27 private loginValidatorsService: LoginValidatorsService,
b1d40cff
C
28 private authService: AuthService,
29 private userService: UserService,
30 private serverService: ServerService,
31 private redirectService: RedirectService,
32 private notificationsService: NotificationsService,
b1d40cff
C
33 private i18n: I18n
34 ) {
df98563e 35 super()
4b2f33f3 36 }
b1794c53 37
2b084d70
C
38 get signupAllowed () {
39 return this.serverService.getConfig().signup.allowed === true
40 }
41
df98563e 42 ngOnInit () {
d18d6478 43 this.buildForm({
e309822b
C
44 username: this.loginValidatorsService.LOGIN_USERNAME,
45 password: this.loginValidatorsService.LOGIN_PASSWORD
d18d6478 46 })
0f6da32b
C
47 }
48
df98563e
C
49 login () {
50 this.error = null
4b2f33f3 51
df98563e 52 const { username, password } = this.form.value
4b2f33f3 53
2b084d70
C
54 this.authService.login(username, password)
55 .subscribe(
56 () => this.redirectService.redirectToHomepage(),
192ea60b 57
e6921918
C
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 }
2b084d70 63 )
b1794c53 64 }
ecb4e35f
C
65
66 askResetPassword () {
67 this.userService.askResetPassword(this.forgotPasswordEmail)
68 .subscribe(
69 res => {
b1d40cff 70 const message = this.i18n(
51d4bcad 71 'An email with the reset password instructions will be sent to {{email}}.',
b1d40cff
C
72 { email: this.forgotPasswordEmail }
73 )
74 this.notificationsService.success(this.i18n('Success'), message)
ecb4e35f
C
75 this.hideForgotPasswordModal()
76 },
77
b1d40cff 78 err => this.notificationsService.error(this.i18n('Error'), err.message)
ecb4e35f
C
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 }
b1794c53 93}