From 98bd5e2256bfdeba6d5ab07f0421acfde1a0de26 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 14 Feb 2023 11:47:01 +0100 Subject: [PATCH] Refactor login redirection/button links Correctly handle external auth redirection in all cases --- .../app/+error-page/error-page.component.html | 5 +---- .../src/app/+error-page/error-page.module.ts | 2 ++ .../reset-password.component.ts | 1 + .../action-buttons/video-rate.component.ts | 2 +- .../comment/video-comment-add.component.html | 5 +---- .../comment/video-comment-add.component.ts | 7 ------ client/src/app/core/auth/auth.service.ts | 10 ++++----- .../app/core/routing/login-guard.service.ts | 9 ++++---- .../src/app/core/routing/redirect.service.ts | 9 ++++++++ .../core/routing/user-right-guard.service.ts | 7 +++--- client/src/app/menu/menu.component.html | 3 +-- client/src/app/menu/menu.component.scss | 3 ++- client/src/app/menu/menu.component.ts | 6 ----- .../app/shared/shared-main/angular/index.ts | 1 + .../shared-main/angular/link.component.html | 4 ++-- .../shared-main/angular/link.component.scss | 2 +- .../shared-main/angular/link.component.ts | 16 +++++++++++--- .../angular/login-link.component.html | 1 + .../angular/login-link.component.ts | 22 +++++++++++++++++++ .../shared/shared-main/shared-main.module.ts | 3 +++ .../subscribe-button.component.ts | 7 +++--- 21 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 client/src/app/shared/shared-main/angular/login-link.component.html create mode 100644 client/src/app/shared/shared-main/angular/login-link.component.ts diff --git a/client/src/app/+error-page/error-page.component.html b/client/src/app/+error-page/error-page.component.html index 070985d3c..b8b9296a3 100644 --- a/client/src/app/+error-page/error-page.component.html +++ b/client/src/app/+error-page/error-page.component.html @@ -31,10 +31,7 @@ You might need to login to see the resource. - - Login - - +
diff --git a/client/src/app/+error-page/error-page.module.ts b/client/src/app/+error-page/error-page.module.ts index 807c6f5a9..34f026ea8 100644 --- a/client/src/app/+error-page/error-page.module.ts +++ b/client/src/app/+error-page/error-page.module.ts @@ -1,11 +1,13 @@ import { CommonModule } from '@angular/common' import { NgModule } from '@angular/core' +import { SharedMainModule } from '@app/shared/shared-main' import { ErrorPageRoutingModule } from './error-page-routing.module' import { ErrorPageComponent } from './error-page.component' @NgModule({ imports: [ CommonModule, + SharedMainModule, ErrorPageRoutingModule ], diff --git a/client/src/app/+reset-password/reset-password.component.ts b/client/src/app/+reset-password/reset-password.component.ts index 44216f978..a6c05bbfb 100644 --- a/client/src/app/+reset-password/reset-password.component.ts +++ b/client/src/app/+reset-password/reset-password.component.ts @@ -45,6 +45,7 @@ export class ResetPasswordComponent extends FormReactive implements OnInit { .subscribe({ next: () => { this.notifier.success($localize`Your password has been successfully reset!`) + this.router.navigate([ '/login' ]) }, diff --git a/client/src/app/+videos/+video-watch/shared/action-buttons/video-rate.component.ts b/client/src/app/+videos/+video-watch/shared/action-buttons/video-rate.component.ts index 0fef246b3..d0c138834 100644 --- a/client/src/app/+videos/+video-watch/shared/action-buttons/video-rate.component.ts +++ b/client/src/app/+videos/+video-watch/shared/action-buttons/video-rate.component.ts @@ -82,7 +82,7 @@ export class VideoRateComponent implements OnInit, OnChanges, OnDestroy { getRatePopoverText () { if (this.isUserLoggedIn) return undefined - return $localize`You need to be logged in to rate this video.` + return $localize`You need to be logged in to rate this video.` } private checkUserRating () { diff --git a/client/src/app/+videos/+video-watch/shared/comment/video-comment-add.component.html b/client/src/app/+videos/+video-watch/shared/comment/video-comment-add.component.html index 6bc201f32..203e9ce62 100644 --- a/client/src/app/+videos/+video-watch/shared/comment/video-comment-add.component.html +++ b/client/src/app/+videos/+video-watch/shared/comment/video-comment-add.component.html @@ -74,10 +74,7 @@ (click)="hideModals()" (key.enter)="hideModals()" > - +
diff --git a/client/src/app/+videos/+video-watch/shared/comment/video-comment-add.component.ts b/client/src/app/+videos/+video-watch/shared/comment/video-comment-add.component.ts index 9a9bfe710..033097084 100644 --- a/client/src/app/+videos/+video-watch/shared/comment/video-comment-add.component.ts +++ b/client/src/app/+videos/+video-watch/shared/comment/video-comment-add.component.ts @@ -13,7 +13,6 @@ import { SimpleChanges, ViewChild } from '@angular/core' -import { Router } from '@angular/router' import { Notifier, User } from '@app/core' import { VIDEO_COMMENT_TEXT_VALIDATOR } from '@app/shared/form-validators/video-comment-validators' import { FormReactive, FormReactiveService } from '@app/shared/shared-forms' @@ -52,7 +51,6 @@ export class VideoCommentAddComponent extends FormReactive implements OnChanges, private notifier: Notifier, private videoCommentService: VideoCommentService, private modalService: NgbModal, - private router: Router, @Inject(LOCALE_ID) private localeId: string ) { super() @@ -161,11 +159,6 @@ export class VideoCommentAddComponent extends FormReactive implements OnChanges, return window.location.href } - gotoLogin () { - this.hideModals() - this.router.navigate([ '/login' ]) - } - cancelCommentReply () { this.cancel.emit(null) this.form.value['text'] = this.textareaElement.nativeElement.value = '' diff --git a/client/src/app/core/auth/auth.service.ts b/client/src/app/core/auth/auth.service.ts index ed7eabb76..6fe601d8d 100644 --- a/client/src/app/core/auth/auth.service.ts +++ b/client/src/app/core/auth/auth.service.ts @@ -5,11 +5,11 @@ import { HttpClient, HttpErrorResponse, HttpHeaders, HttpParams } from '@angular import { Injectable } from '@angular/core' import { Router } from '@angular/router' import { Notifier } from '@app/core/notification/notifier.service' -import { logger, OAuthUserTokens, objectToUrlEncoded, peertubeLocalStorage, PluginsManager } from '@root-helpers/index' +import { logger, OAuthUserTokens, objectToUrlEncoded, peertubeLocalStorage } from '@root-helpers/index' import { HttpStatusCode, MyUser as UserServerModel, OAuthClientLocal, User, UserLogin, UserRefreshToken } from '@shared/models' import { environment } from '../../../environments/environment' import { RestExtractor } from '../rest/rest-extractor.service' -import { ServerService } from '../server' +import { RedirectService } from '../routing' import { AuthStatus } from './auth-status.model' import { AuthUser } from './auth-user.model' @@ -45,7 +45,7 @@ export class AuthService { private refreshingTokenObservable: Observable constructor ( - private serverService: ServerService, + private redirectService: RedirectService, private http: HttpClient, private notifier: Notifier, private hotkeysService: HotkeysService, @@ -227,9 +227,7 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular logger.info('Cannot refresh token -> logout...') this.logout() - const externalLoginUrl = PluginsManager.getDefaultLoginHref(environment.apiUrl, this.serverService.getHTMLConfig()) - if (externalLoginUrl) window.location.href = externalLoginUrl - else this.router.navigate([ '/login' ]) + this.redirectService.redirectToLogin() return observableThrowError(() => ({ error: $localize`You need to reconnect.` diff --git a/client/src/app/core/routing/login-guard.service.ts b/client/src/app/core/routing/login-guard.service.ts index a949be14c..2f5a31e7f 100644 --- a/client/src/app/core/routing/login-guard.service.ts +++ b/client/src/app/core/routing/login-guard.service.ts @@ -1,19 +1,20 @@ import { Injectable } from '@angular/core' -import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, Router, RouterStateSnapshot } from '@angular/router' +import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, RouterStateSnapshot } from '@angular/router' import { AuthService } from '../auth/auth.service' +import { RedirectService } from './redirect.service' @Injectable() export class LoginGuard implements CanActivate, CanActivateChild { constructor ( - private router: Router, - private auth: AuthService + private auth: AuthService, + private redirectService: RedirectService ) {} canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { if (this.auth.isLoggedIn() === true) return true - this.router.navigate([ '/login' ]) + this.redirectService.redirectToLogin() return false } diff --git a/client/src/app/core/routing/redirect.service.ts b/client/src/app/core/routing/redirect.service.ts index 1344458d5..e239c6210 100644 --- a/client/src/app/core/routing/redirect.service.ts +++ b/client/src/app/core/routing/redirect.service.ts @@ -4,6 +4,8 @@ import { NavigationCancel, NavigationEnd, Router } from '@angular/router' import { logger } from '@root-helpers/logger' import { ServerService } from '../server' import { SessionStorageService } from '../wrappers/storage.service' +import { PluginsManager } from '@root-helpers/plugins-manager' +import { environment } from 'src/environments/environment' const debugLogger = debug('peertube:router:RedirectService') @@ -100,6 +102,13 @@ export class RedirectService { } + redirectToLogin () { + const externalLoginUrl = PluginsManager.getDefaultLoginHref(environment.apiUrl, this.serverService.getHTMLConfig()) + + if (externalLoginUrl) window.location.href = externalLoginUrl + else this.router.navigate([ '/login' ]) + } + private doRedirect (redirectUrl: string, fallbackRoute?: string) { debugLogger('Redirecting on %s', redirectUrl) diff --git a/client/src/app/core/routing/user-right-guard.service.ts b/client/src/app/core/routing/user-right-guard.service.ts index a2ce772db..c6bd05bb6 100644 --- a/client/src/app/core/routing/user-right-guard.service.ts +++ b/client/src/app/core/routing/user-right-guard.service.ts @@ -1,12 +1,13 @@ import { Injectable } from '@angular/core' -import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, Router, RouterStateSnapshot } from '@angular/router' +import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, RouterStateSnapshot } from '@angular/router' import { AuthService } from '../auth/auth.service' +import { RedirectService } from './redirect.service' @Injectable() export class UserRightGuard implements CanActivate, CanActivateChild { constructor ( - private router: Router, + private redirectService: RedirectService, private auth: AuthService ) {} @@ -18,7 +19,7 @@ export class UserRightGuard implements CanActivate, CanActivateChild { if (user.hasRight(neededUserRight)) return true } - this.router.navigate([ '/login' ]) + this.redirectService.redirectToLogin() return false } diff --git a/client/src/app/menu/menu.component.html b/client/src/app/menu/menu.component.html index abfc151e7..0fb9897b9 100644 --- a/client/src/app/menu/menu.component.html +++ b/client/src/app/menu/menu.component.html @@ -100,8 +100,7 @@