PeerTube/client/src/app/+login/login.component.ts

216 lines
6.3 KiB
TypeScript
Raw Normal View History

import { environment } from 'src/environments/environment'
2020-06-23 14:10:17 +02:00
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router'
import { AuthService, Notifier, RedirectService, SessionStorageService, UserService } from '@app/core'
import { HooksService } from '@app/core/plugins/hooks.service'
import { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/form-validators/login-validators'
2022-10-07 11:06:28 +02:00
import { USER_OTP_TOKEN_VALIDATOR } from '@app/shared/form-validators/user-validators'
2022-10-07 15:26:53 +02:00
import { FormReactive, FormReactiveService, InputTextComponent } from '@app/shared/shared-forms'
2021-03-24 13:32:55 +01:00
import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance'
import { NgbAccordion, NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
import { getExternalAuthHref } from '@shared/core-utils'
2020-06-23 14:10:17 +02:00
import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models'
2016-03-22 15:51:54 +01:00
@Component({
selector: 'my-login',
2017-12-05 16:48:26 +01:00
templateUrl: './login.component.html',
styleUrls: [ './login.component.scss' ]
2016-03-22 15:51:54 +01:00
})
2020-04-29 10:42:35 +02:00
export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
private static SESSION_STORAGE_REDIRECT_URL_KEY = 'login-previous-url'
2019-07-24 16:05:59 +02:00
@ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
2022-10-07 11:06:28 +02:00
@ViewChild('otpTokenInput') otpTokenInput: InputTextComponent
2018-01-30 13:27:07 +01:00
accordion: NgbAccordion
error: string = null
2018-01-30 13:27:07 +01:00
forgotPasswordEmail = ''
2020-04-30 15:03:09 +02:00
2020-04-28 14:49:03 +02:00
isAuthenticatedWithExternalAuth = false
2020-04-30 15:03:09 +02:00
externalAuthError = false
2020-04-29 10:42:35 +02:00
externalLogins: string[] = []
2016-06-04 13:31:23 +02:00
instanceInformationPanels = {
terms: true,
administrators: false,
features: false,
moderation: false,
codeOfConduct: false
}
2022-10-07 11:06:28 +02:00
otpStep = false
private openedForgotPasswordModal: NgbModalRef
2019-12-18 15:31:54 +01:00
private serverConfig: ServerConfig
2018-06-04 16:21:17 +02:00
constructor (
2022-10-07 15:26:53 +02:00
protected formReactiveService: FormReactiveService,
2019-12-18 15:31:54 +01:00
private route: ActivatedRoute,
private modalService: NgbModal,
2018-06-04 16:21:17 +02:00
private authService: AuthService,
private userService: UserService,
private redirectService: RedirectService,
private notifier: Notifier,
private hooks: HooksService,
private storage: SessionStorageService,
private router: Router
2021-08-17 14:42:53 +02:00
) {
super()
2016-09-09 22:16:51 +02:00
}
2016-03-22 15:51:54 +01:00
2018-03-28 18:22:59 +02:00
get signupAllowed () {
2019-12-18 15:31:54 +01:00
return this.serverConfig.signup.allowed === true
2018-03-28 18:22:59 +02:00
}
2022-06-15 14:29:28 +02:00
get instanceName () {
return this.serverConfig.instance.name
}
onTermsClick (event: Event, instanceInformation: HTMLElement) {
event.preventDefault()
if (this.accordion) {
this.accordion.expand('terms')
instanceInformation.scrollIntoView({ behavior: 'smooth' })
}
}
isEmailDisabled () {
2019-12-18 15:31:54 +01:00
return this.serverConfig.email.enabled === false
}
ngOnInit () {
2020-04-28 14:49:03 +02:00
const snapshot = this.route.snapshot
2020-12-29 15:23:03 +01:00
// Avoid undefined errors when accessing form error properties
this.buildForm({
username: LOGIN_USERNAME_VALIDATOR,
2022-10-07 11:06:28 +02:00
password: LOGIN_PASSWORD_VALIDATOR,
'otp-token': {
VALIDATORS: [], // Will be set dynamically
MESSAGES: USER_OTP_TOKEN_VALIDATOR.MESSAGES
}
2020-12-29 15:23:03 +01:00
})
2020-04-28 14:49:03 +02:00
this.serverConfig = snapshot.data.serverConfig
if (snapshot.queryParams.externalAuthToken) {
this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
return
}
2019-12-18 15:31:54 +01:00
2020-04-30 15:03:09 +02:00
if (snapshot.queryParams.externalAuthError) {
this.externalAuthError = true
return
}
const previousUrl = this.redirectService.getPreviousUrl()
if (previousUrl && previousUrl !== '/') {
this.storage.setItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY, previousUrl)
}
2020-04-29 10:42:35 +02:00
}
ngAfterViewInit () {
this.hooks.runAction('action:login.init', 'login')
2020-04-29 10:42:35 +02:00
}
getExternalLogins () {
return this.serverConfig.plugin.registeredExternalAuths
}
2018-09-22 14:14:32 +02:00
2020-04-29 10:42:35 +02:00
getAuthHref (auth: RegisteredExternalAuthConfig) {
return getExternalAuthHref(environment.apiUrl, auth)
2016-08-23 14:37:49 +02:00
}
login () {
this.error = null
2016-09-09 22:16:51 +02:00
2022-10-07 11:06:28 +02:00
const options = {
username: this.form.value['username'],
password: this.form.value['password'],
otpToken: this.form.value['otp-token']
}
2016-09-09 22:16:51 +02:00
2022-10-07 11:06:28 +02:00
this.authService.login(options)
.pipe()
2021-08-17 11:27:47 +02:00
.subscribe({
next: () => this.redirectService.redirectToPreviousRoute(),
2016-06-04 13:31:23 +02:00
2022-10-07 11:06:28 +02:00
error: err => {
this.handleError(err)
}
2021-08-17 11:27:47 +02:00
})
2016-03-22 15:51:54 +01:00
}
2018-01-30 13:27:07 +01:00
askResetPassword () {
this.userService.askResetPassword(this.forgotPasswordEmail)
2021-08-17 11:27:47 +02:00
.subscribe({
next: () => {
const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.
The link will expire within 1 hour.`
this.notifier.success(message)
2018-01-30 13:27:07 +01:00
this.hideForgotPasswordModal()
},
2021-08-17 11:27:47 +02:00
error: err => this.notifier.error(err.message)
})
2018-01-30 13:27:07 +01:00
}
openForgotPasswordModal () {
this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
2018-01-30 13:27:07 +01:00
}
hideForgotPasswordModal () {
this.openedForgotPasswordModal.close()
2018-01-30 13:27:07 +01:00
}
2020-04-28 14:49:03 +02:00
onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
this.accordion = instanceAboutAccordion.accordion
}
hasUsernameUppercase () {
return this.form.value['username'].match(/[A-Z]/)
}
2020-04-28 14:49:03 +02:00
private loadExternalAuthToken (username: string, token: string) {
this.isAuthenticatedWithExternalAuth = true
2022-10-07 11:06:28 +02:00
this.authService.login({ username, password: null, token })
2021-08-17 11:27:47 +02:00
.subscribe({
next: () => {
const redirectUrl = this.storage.getItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY)
if (redirectUrl) {
this.storage.removeItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY)
return this.router.navigateByUrl(redirectUrl)
}
this.redirectService.redirectToLatestSessionRoute()
},
2021-08-17 11:27:47 +02:00
error: err => {
this.handleError(err)
this.isAuthenticatedWithExternalAuth = false
}
})
2020-04-28 14:49:03 +02:00
}
private handleError (err: any) {
2022-10-07 11:06:28 +02:00
if (this.authService.isOTPMissingError(err)) {
this.otpStep = true
setTimeout(() => {
this.form.get('otp-token').setValidators(USER_OTP_TOKEN_VALIDATOR.VALIDATORS)
this.otpTokenInput.focus()
})
return
}
if (err.message.indexOf('credentials are invalid') !== -1) this.error = $localize`Incorrect username or password.`
else if (err.message.indexOf('blocked') !== -1) this.error = $localize`Your account is blocked.`
2020-04-28 14:49:03 +02:00
else this.error = err.message
}
2016-03-22 15:51:54 +01:00
}