]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/login/login.component.ts
Add informational message at login for visitors coming from upload button
[github/Chocobozzz/PeerTube.git] / client / src / app / login / login.component.ts
CommitLineData
ecb4e35f 1import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
000eb0e4 2import { Notifier, RedirectService } from '@app/core'
ecb4e35f 3import { UserService } from '@app/shared'
df98563e
C
4import { AuthService } from '../core'
5import { FormReactive } from '../shared'
b1d40cff 6import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478 7import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
e309822b 8import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service'
63347a0f 9import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
000eb0e4
RK
10import { ActivatedRoute } from '@angular/router'
11import { ServerConfig } from '@shared/models/server/server-config.model'
b1794c53
C
12
13@Component({
a840d396 14 selector: 'my-login',
d235f6b0
C
15 templateUrl: './login.component.html',
16 styleUrls: [ './login.component.scss' ]
b1794c53
C
17})
18
4b2f33f3 19export class LoginComponent extends FormReactive implements OnInit {
f36da21e
C
20 @ViewChild('emailInput', { static: true }) input: ElementRef
21 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
ecb4e35f 22
df98563e 23 error: string = null
ecb4e35f 24 forgotPasswordEmail = ''
000eb0e4
RK
25 from = {
26 upload: false
27 }
192ea60b 28
63347a0f 29 private openedForgotPasswordModal: NgbModalRef
ba430d75 30 private serverConfig: ServerConfig
63347a0f 31
b1d40cff 32 constructor (
d18d6478 33 protected formValidatorService: FormValidatorService,
ba430d75 34 private route: ActivatedRoute,
63347a0f 35 private modalService: NgbModal,
e309822b 36 private loginValidatorsService: LoginValidatorsService,
b1d40cff
C
37 private authService: AuthService,
38 private userService: UserService,
b1d40cff 39 private redirectService: RedirectService,
f8b2c1b4 40 private notifier: Notifier,
b1d40cff
C
41 private i18n: I18n
42 ) {
df98563e 43 super()
4b2f33f3 44 }
b1794c53 45
2b084d70 46 get signupAllowed () {
ba430d75 47 return this.serverConfig.signup.allowed === true
2b084d70
C
48 }
49
000eb0e4
RK
50 get instancesIndexUrl () {
51 return this.serverConfig.followings.instance.autoFollowIndex.indexUrl || 'https://instances.joinpeertube.org'
52 }
53
3b3b1820 54 isEmailDisabled () {
ba430d75 55 return this.serverConfig.email.enabled === false
3b3b1820
C
56 }
57
df98563e 58 ngOnInit () {
ba430d75 59 this.serverConfig = this.route.snapshot.data.serverConfig
000eb0e4 60 this.from.upload = Boolean(this.route.snapshot.paramMap.get('fromUpload'))
ba430d75 61
d18d6478 62 this.buildForm({
e309822b
C
63 username: this.loginValidatorsService.LOGIN_USERNAME,
64 password: this.loginValidatorsService.LOGIN_PASSWORD
d18d6478 65 })
9fe44067
RK
66
67 this.input.nativeElement.focus()
0f6da32b
C
68 }
69
df98563e
C
70 login () {
71 this.error = null
4b2f33f3 72
df98563e 73 const { username, password } = this.form.value
4b2f33f3 74
2b084d70
C
75 this.authService.login(username, password)
76 .subscribe(
dae5ca24 77 () => this.redirectService.redirectToPreviousRoute(),
192ea60b 78
e6921918
C
79 err => {
80 if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
81 else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.')
82 else this.error = err.message
83 }
2b084d70 84 )
b1794c53 85 }
ecb4e35f
C
86
87 askResetPassword () {
88 this.userService.askResetPassword(this.forgotPasswordEmail)
89 .subscribe(
141b177d 90 () => {
b1d40cff 91 const message = this.i18n(
f88ee4a9 92 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.',
b1d40cff
C
93 { email: this.forgotPasswordEmail }
94 )
f8b2c1b4 95 this.notifier.success(message)
ecb4e35f
C
96 this.hideForgotPasswordModal()
97 },
98
f8b2c1b4 99 err => this.notifier.error(err.message)
ecb4e35f
C
100 )
101 }
102
ecb4e35f 103 openForgotPasswordModal () {
63347a0f 104 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
ecb4e35f
C
105 }
106
107 hideForgotPasswordModal () {
63347a0f 108 this.openedForgotPasswordModal.close()
ecb4e35f 109 }
b1794c53 110}