]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+login/login.component.ts
Fix terms/code of conduct link toggle
[github/Chocobozzz/PeerTube.git] / client / src / app / +login / login.component.ts
CommitLineData
2570fd9c 1import { environment } from 'src/environments/environment'
67ed6552 2import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'
13e7c3b0 3import { ActivatedRoute, Router } from '@angular/router'
cb28bb92 4import { AuthService, Notifier, RedirectService, SessionStorageService, UserService } from '@app/core'
f375bb3d 5import { HooksService } from '@app/core/plugins/hooks.service'
7ed1edbb 6import { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/form-validators/login-validators'
d12b40fb 7import { USER_OTP_TOKEN_VALIDATOR } from '@app/shared/form-validators/user-validators'
5c5bcea2 8import { FormReactive, FormReactiveService, InputTextComponent } from '@app/shared/shared-forms'
a3664dfd 9import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance'
431ebbd5 10import { NgbAccordionDirective, NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
2570fd9c 11import { getExternalAuthHref } from '@shared/core-utils'
9589907c 12import { RegisteredExternalAuthConfig, ServerConfig, ServerErrorCode } from '@shared/models'
b1794c53
C
13
14@Component({
a840d396 15 selector: 'my-login',
d235f6b0
C
16 templateUrl: './login.component.html',
17 styleUrls: [ './login.component.scss' ]
b1794c53
C
18})
19
ebefc902 20export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
cb28bb92
C
21 private static SESSION_STORAGE_REDIRECT_URL_KEY = 'login-previous-url'
22
f36da21e 23 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
d12b40fb 24 @ViewChild('otpTokenInput') otpTokenInput: InputTextComponent
ed22eaab 25 @ViewChild('instanceAboutAccordion') instanceAboutAccordion: InstanceAboutAccordionComponent
ecb4e35f 26
431ebbd5 27 accordion: NgbAccordionDirective
df98563e 28 error: string = null
ecb4e35f 29 forgotPasswordEmail = ''
bc90883f 30
4a8d113b 31 isAuthenticatedWithExternalAuth = false
bc90883f 32 externalAuthError = false
ebefc902 33 externalLogins: string[] = []
192ea60b 34
40360c17
K
35 instanceInformationPanels = {
36 terms: true,
37 administrators: false,
38 features: false,
39 moderation: false,
40 codeOfConduct: false
41 }
42
d12b40fb
C
43 otpStep = false
44
63347a0f 45 private openedForgotPasswordModal: NgbModalRef
ba430d75 46 private serverConfig: ServerConfig
63347a0f 47
b1d40cff 48 constructor (
5c5bcea2 49 protected formReactiveService: FormReactiveService,
ba430d75 50 private route: ActivatedRoute,
63347a0f 51 private modalService: NgbModal,
b1d40cff
C
52 private authService: AuthService,
53 private userService: UserService,
b1d40cff 54 private redirectService: RedirectService,
f8b2c1b4 55 private notifier: Notifier,
cb28bb92 56 private hooks: HooksService,
13e7c3b0
C
57 private storage: SessionStorageService,
58 private router: Router
9df52d66 59 ) {
df98563e 60 super()
4b2f33f3 61 }
b1794c53 62
2b084d70 63 get signupAllowed () {
ba430d75 64 return this.serverConfig.signup.allowed === true
2b084d70
C
65 }
66
11056966
C
67 get instanceName () {
68 return this.serverConfig.instance.name
69 }
70
40360c17
K
71 onTermsClick (event: Event, instanceInformation: HTMLElement) {
72 event.preventDefault()
73
ed22eaab
C
74 if (this.instanceAboutAccordion) {
75 this.instanceAboutAccordion.expandTerms()
40360c17
K
76 instanceInformation.scrollIntoView({ behavior: 'smooth' })
77 }
78 }
79
3b3b1820 80 isEmailDisabled () {
ba430d75 81 return this.serverConfig.email.enabled === false
3b3b1820
C
82 }
83
df98563e 84 ngOnInit () {
4a8d113b
C
85 const snapshot = this.route.snapshot
86
2345e138
C
87 // Avoid undefined errors when accessing form error properties
88 this.buildForm({
d0fbc9fd
C
89 'username': LOGIN_USERNAME_VALIDATOR,
90 'password': LOGIN_PASSWORD_VALIDATOR,
d12b40fb
C
91 'otp-token': {
92 VALIDATORS: [], // Will be set dynamically
93 MESSAGES: USER_OTP_TOKEN_VALIDATOR.MESSAGES
94 }
2345e138
C
95 })
96
4a8d113b
C
97 this.serverConfig = snapshot.data.serverConfig
98
99 if (snapshot.queryParams.externalAuthToken) {
100 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
101 return
102 }
ba430d75 103
bc90883f
C
104 if (snapshot.queryParams.externalAuthError) {
105 this.externalAuthError = true
106 return
107 }
cb28bb92 108
13e7c3b0
C
109 const previousUrl = this.redirectService.getPreviousUrl()
110 if (previousUrl && previousUrl !== '/') {
111 this.storage.setItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY, previousUrl)
112 }
ebefc902
C
113 }
114
115 ngAfterViewInit () {
f375bb3d 116 this.hooks.runAction('action:login.init', 'login')
ebefc902
C
117 }
118
119 getExternalLogins () {
120 return this.serverConfig.plugin.registeredExternalAuths
121 }
9fe44067 122
ebefc902 123 getAuthHref (auth: RegisteredExternalAuthConfig) {
2570fd9c 124 return getExternalAuthHref(environment.apiUrl, auth)
0f6da32b
C
125 }
126
df98563e
C
127 login () {
128 this.error = null
4b2f33f3 129
d12b40fb
C
130 const options = {
131 username: this.form.value['username'],
132 password: this.form.value['password'],
133 otpToken: this.form.value['otp-token']
134 }
4b2f33f3 135
d12b40fb
C
136 this.authService.login(options)
137 .pipe()
1378c0d3
C
138 .subscribe({
139 next: () => this.redirectService.redirectToPreviousRoute(),
192ea60b 140
d12b40fb
C
141 error: err => {
142 this.handleError(err)
143 }
1378c0d3 144 })
b1794c53 145 }
ecb4e35f
C
146
147 askResetPassword () {
148 this.userService.askResetPassword(this.forgotPasswordEmail)
1378c0d3
C
149 .subscribe({
150 next: () => {
66357162
C
151 const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.
152The link will expire within 1 hour.`
153
f8b2c1b4 154 this.notifier.success(message)
ecb4e35f
C
155 this.hideForgotPasswordModal()
156 },
157
1378c0d3
C
158 error: err => this.notifier.error(err.message)
159 })
ecb4e35f
C
160 }
161
ecb4e35f 162 openForgotPasswordModal () {
63347a0f 163 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
ecb4e35f
C
164 }
165
166 hideForgotPasswordModal () {
63347a0f 167 this.openedForgotPasswordModal.close()
ecb4e35f 168 }
4a8d113b 169
40360c17
K
170 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
171 this.accordion = instanceAboutAccordion.accordion
172 }
173
7f28f2dd
C
174 hasUsernameUppercase () {
175 return this.form.value['username'].match(/[A-Z]/)
176 }
177
4a8d113b
C
178 private loadExternalAuthToken (username: string, token: string) {
179 this.isAuthenticatedWithExternalAuth = true
180
d12b40fb 181 this.authService.login({ username, password: null, token })
1378c0d3 182 .subscribe({
cb28bb92 183 next: () => {
13e7c3b0
C
184 const redirectUrl = this.storage.getItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY)
185 if (redirectUrl) {
186 this.storage.removeItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY)
187 return this.router.navigateByUrl(redirectUrl)
188 }
189
190 this.redirectService.redirectToLatestSessionRoute()
cb28bb92 191 },
1378c0d3
C
192
193 error: err => {
194 this.handleError(err)
195 this.isAuthenticatedWithExternalAuth = false
196 }
197 })
4a8d113b
C
198 }
199
200 private handleError (err: any) {
d12b40fb
C
201 if (this.authService.isOTPMissingError(err)) {
202 this.otpStep = true
203
204 setTimeout(() => {
205 this.form.get('otp-token').setValidators(USER_OTP_TOKEN_VALIDATOR.VALIDATORS)
206 this.otpTokenInput.focus()
207 })
208
209 return
210 }
211
9589907c
C
212 if (err.message.includes('credentials are invalid')) {
213 this.error = $localize`Incorrect username or password.`
214 return
215 }
216
217 if (err.message.includes('blocked')) {
218 this.error = $localize`Your account is blocked.`
219 return
220 }
221
222 if (err.body?.code === ServerErrorCode.ACCOUNT_WAITING_FOR_APPROVAL) {
223 this.error = $localize`This account is awaiting approval by moderators.`
224 return
225 }
226
227 if (err.body?.code === ServerErrorCode.ACCOUNT_APPROVAL_REJECTED) {
228 this.error = $localize`Registration approval has been rejected for this account.`
229 return
230 }
231
232 this.error = err.message
4a8d113b 233 }
b1794c53 234}