]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - 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
1 import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2 import { Notifier, RedirectService } 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 { ActivatedRoute } from '@angular/router'
11 import { ServerConfig } from '@shared/models/server/server-config.model'
12
13 @Component({
14 selector: 'my-login',
15 templateUrl: './login.component.html',
16 styleUrls: [ './login.component.scss' ]
17 })
18
19 export class LoginComponent extends FormReactive implements OnInit {
20 @ViewChild('emailInput', { static: true }) input: ElementRef
21 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
22
23 error: string = null
24 forgotPasswordEmail = ''
25 from = {
26 upload: false
27 }
28
29 private openedForgotPasswordModal: NgbModalRef
30 private serverConfig: ServerConfig
31
32 constructor (
33 protected formValidatorService: FormValidatorService,
34 private route: ActivatedRoute,
35 private modalService: NgbModal,
36 private loginValidatorsService: LoginValidatorsService,
37 private authService: AuthService,
38 private userService: UserService,
39 private redirectService: RedirectService,
40 private notifier: Notifier,
41 private i18n: I18n
42 ) {
43 super()
44 }
45
46 get signupAllowed () {
47 return this.serverConfig.signup.allowed === true
48 }
49
50 get instancesIndexUrl () {
51 return this.serverConfig.followings.instance.autoFollowIndex.indexUrl || 'https://instances.joinpeertube.org'
52 }
53
54 isEmailDisabled () {
55 return this.serverConfig.email.enabled === false
56 }
57
58 ngOnInit () {
59 this.serverConfig = this.route.snapshot.data.serverConfig
60 this.from.upload = Boolean(this.route.snapshot.paramMap.get('fromUpload'))
61
62 this.buildForm({
63 username: this.loginValidatorsService.LOGIN_USERNAME,
64 password: this.loginValidatorsService.LOGIN_PASSWORD
65 })
66
67 this.input.nativeElement.focus()
68 }
69
70 login () {
71 this.error = null
72
73 const { username, password } = this.form.value
74
75 this.authService.login(username, password)
76 .subscribe(
77 () => this.redirectService.redirectToPreviousRoute(),
78
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 }
84 )
85 }
86
87 askResetPassword () {
88 this.userService.askResetPassword(this.forgotPasswordEmail)
89 .subscribe(
90 () => {
91 const message = this.i18n(
92 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.',
93 { email: this.forgotPasswordEmail }
94 )
95 this.notifier.success(message)
96 this.hideForgotPasswordModal()
97 },
98
99 err => this.notifier.error(err.message)
100 )
101 }
102
103 openForgotPasswordModal () {
104 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
105 }
106
107 hideForgotPasswordModal () {
108 this.openedForgotPasswordModal.close()
109 }
110 }