]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+login/login.component.ts
Add control bar option for peertube player
[github/Chocobozzz/PeerTube.git] / client / src / app / +login / login.component.ts
1
2 import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'
3 import { ActivatedRoute } from '@angular/router'
4 import { AuthService, Notifier, RedirectService, SessionStorageService, UserService } from '@app/core'
5 import { HooksService } from '@app/core/plugins/hooks.service'
6 import { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/form-validators/login-validators'
7 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
8 import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance'
9 import { NgbAccordion, NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
10 import { PluginsManager } from '@root-helpers/plugins-manager'
11 import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models'
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, AfterViewInit {
20 private static SESSION_STORAGE_REDIRECT_URL_KEY = 'login-previous-url'
21
22 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
23
24 accordion: NgbAccordion
25 error: string = null
26 forgotPasswordEmail = ''
27
28 isAuthenticatedWithExternalAuth = false
29 externalAuthError = false
30 externalLogins: string[] = []
31
32 instanceInformationPanels = {
33 terms: true,
34 administrators: false,
35 features: false,
36 moderation: false,
37 codeOfConduct: false
38 }
39
40 private openedForgotPasswordModal: NgbModalRef
41 private serverConfig: ServerConfig
42
43 constructor (
44 protected formValidatorService: FormValidatorService,
45 private route: ActivatedRoute,
46 private modalService: NgbModal,
47 private authService: AuthService,
48 private userService: UserService,
49 private redirectService: RedirectService,
50 private notifier: Notifier,
51 private hooks: HooksService,
52 private storage: SessionStorageService
53 ) {
54 super()
55 }
56
57 get signupAllowed () {
58 return this.serverConfig.signup.allowed === true
59 }
60
61 onTermsClick (event: Event, instanceInformation: HTMLElement) {
62 event.preventDefault()
63
64 if (this.accordion) {
65 this.accordion.expand('terms')
66 instanceInformation.scrollIntoView({ behavior: 'smooth' })
67 }
68 }
69
70 isEmailDisabled () {
71 return this.serverConfig.email.enabled === false
72 }
73
74 ngOnInit () {
75 const snapshot = this.route.snapshot
76
77 // Avoid undefined errors when accessing form error properties
78 this.buildForm({
79 username: LOGIN_USERNAME_VALIDATOR,
80 password: LOGIN_PASSWORD_VALIDATOR
81 })
82
83 this.serverConfig = snapshot.data.serverConfig
84
85 if (snapshot.queryParams.externalAuthToken) {
86 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
87 return
88 }
89
90 if (snapshot.queryParams.externalAuthError) {
91 this.externalAuthError = true
92 return
93 }
94
95 this.storage.setItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY, this.redirectService.getPreviousUrl())
96 }
97
98 ngAfterViewInit () {
99 this.hooks.runAction('action:login.init', 'login')
100 }
101
102 getExternalLogins () {
103 return this.serverConfig.plugin.registeredExternalAuths
104 }
105
106 getAuthHref (auth: RegisteredExternalAuthConfig) {
107 return PluginsManager.getExternalAuthHref(auth)
108 }
109
110 login () {
111 this.error = null
112
113 const { username, password } = this.form.value
114
115 this.authService.login(username, password)
116 .subscribe({
117 next: () => this.redirectService.redirectToPreviousRoute(),
118
119 error: err => this.handleError(err)
120 })
121 }
122
123 askResetPassword () {
124 this.userService.askResetPassword(this.forgotPasswordEmail)
125 .subscribe({
126 next: () => {
127 const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.
128 The link will expire within 1 hour.`
129
130 this.notifier.success(message)
131 this.hideForgotPasswordModal()
132 },
133
134 error: err => this.notifier.error(err.message)
135 })
136 }
137
138 openForgotPasswordModal () {
139 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
140 }
141
142 hideForgotPasswordModal () {
143 this.openedForgotPasswordModal.close()
144 }
145
146 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
147 this.accordion = instanceAboutAccordion.accordion
148 }
149
150 hasUsernameUppercase () {
151 return this.form.value['username'].match(/[A-Z]/)
152 }
153
154 private loadExternalAuthToken (username: string, token: string) {
155 this.isAuthenticatedWithExternalAuth = true
156
157 this.authService.login(username, null, token)
158 .subscribe({
159 next: () => {
160 this.redirectService.redirectToPreviousRoute(this.storage.getItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY))
161 },
162
163 error: err => {
164 this.handleError(err)
165 this.isAuthenticatedWithExternalAuth = false
166 }
167 })
168 }
169
170 private handleError (err: any) {
171 if (err.message.indexOf('credentials are invalid') !== -1) this.error = $localize`Incorrect username or password.`
172 else if (err.message.indexOf('blocked') !== -1) this.error = $localize`Your account is blocked.`
173 else this.error = err.message
174 }
175 }