X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2F%2Blogin%2Flogin.component.ts;h=73bd41ab4f16b2db24e21317f2278a21a2ee54c2;hb=cb28bb92daf1a0e123bc0144030c2bc65b865f4a;hp=351750453285c2927966714b61f73ea62c756cc2;hpb=7ed1edbbe4ffbef28093e4f5630751cb652814e4;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/+login/login.component.ts b/client/src/app/+login/login.component.ts index 351750453..73bd41ab4 100644 --- a/client/src/app/+login/login.component.ts +++ b/client/src/app/+login/login.component.ts @@ -1,11 +1,13 @@ -import { environment } from 'src/environments/environment' + import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core' import { ActivatedRoute } from '@angular/router' -import { AuthService, Notifier, RedirectService, UserService } from '@app/core' +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' import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' -import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' +import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance' +import { NgbAccordion, NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' +import { PluginsManager } from '@root-helpers/plugins-manager' import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models' @Component({ @@ -15,9 +17,11 @@ import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models' }) export class LoginComponent extends FormReactive implements OnInit, AfterViewInit { - @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef + private static SESSION_STORAGE_REDIRECT_URL_KEY = 'login-previous-url' + @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef + accordion: NgbAccordion error: string = null forgotPasswordEmail = '' @@ -25,6 +29,14 @@ export class LoginComponent extends FormReactive implements OnInit, AfterViewIni externalAuthError = false externalLogins: string[] = [] + instanceInformationPanels = { + terms: true, + administrators: false, + features: false, + moderation: false, + codeOfConduct: false + } + private openedForgotPasswordModal: NgbModalRef private serverConfig: ServerConfig @@ -36,8 +48,9 @@ export class LoginComponent extends FormReactive implements OnInit, AfterViewIni private userService: UserService, private redirectService: RedirectService, private notifier: Notifier, - private hooks: HooksService - ) { + private hooks: HooksService, + private storage: SessionStorageService + ) { super() } @@ -45,6 +58,15 @@ export class LoginComponent extends FormReactive implements OnInit, AfterViewIni return this.serverConfig.signup.allowed === true } + onTermsClick (event: Event, instanceInformation: HTMLElement) { + event.preventDefault() + + if (this.accordion) { + this.accordion.expand('terms') + instanceInformation.scrollIntoView({ behavior: 'smooth' }) + } + } + isEmailDisabled () { return this.serverConfig.email.enabled === false } @@ -52,6 +74,12 @@ export class LoginComponent extends FormReactive implements OnInit, AfterViewIni ngOnInit () { const snapshot = this.route.snapshot + // Avoid undefined errors when accessing form error properties + this.buildForm({ + username: LOGIN_USERNAME_VALIDATOR, + password: LOGIN_PASSWORD_VALIDATOR + }) + this.serverConfig = snapshot.data.serverConfig if (snapshot.queryParams.externalAuthToken) { @@ -64,17 +92,10 @@ export class LoginComponent extends FormReactive implements OnInit, AfterViewIni return } - this.buildForm({ - username: LOGIN_USERNAME_VALIDATOR, - password: LOGIN_PASSWORD_VALIDATOR - }) + this.storage.setItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY, this.redirectService.getPreviousUrl()) } ngAfterViewInit () { - if (this.usernameInput) { - this.usernameInput.nativeElement.focus() - } - this.hooks.runAction('action:login.init', 'login') } @@ -83,7 +104,7 @@ export class LoginComponent extends FormReactive implements OnInit, AfterViewIni } getAuthHref (auth: RegisteredExternalAuthConfig) { - return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}` + return PluginsManager.getExternalAuthHref(auth) } login () { @@ -92,17 +113,17 @@ export class LoginComponent extends FormReactive implements OnInit, AfterViewIni const { username, password } = this.form.value this.authService.login(username, password) - .subscribe( - () => this.redirectService.redirectToPreviousRoute(), + .subscribe({ + next: () => this.redirectService.redirectToPreviousRoute(), - err => this.handleError(err) - ) + error: err => this.handleError(err) + }) } askResetPassword () { this.userService.askResetPassword(this.forgotPasswordEmail) - .subscribe( - () => { + .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.` @@ -110,8 +131,8 @@ The link will expire within 1 hour.` this.hideForgotPasswordModal() }, - err => this.notifier.error(err.message) - ) + error: err => this.notifier.error(err.message) + }) } openForgotPasswordModal () { @@ -122,18 +143,28 @@ The link will expire within 1 hour.` this.openedForgotPasswordModal.close() } + onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) { + this.accordion = instanceAboutAccordion.accordion + } + + hasUsernameUppercase () { + return this.form.value['username'].match(/[A-Z]/) + } + private loadExternalAuthToken (username: string, token: string) { this.isAuthenticatedWithExternalAuth = true this.authService.login(username, null, token) - .subscribe( - () => this.redirectService.redirectToPreviousRoute(), - - err => { - this.handleError(err) - this.isAuthenticatedWithExternalAuth = false - } - ) + .subscribe({ + next: () => { + this.redirectService.redirectToPreviousRoute(this.storage.getItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY)) + }, + + error: err => { + this.handleError(err) + this.isAuthenticatedWithExternalAuth = false + } + }) } private handleError (err: any) {