]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Refactor login redirection/button links
authorChocobozzz <me@florianbigard.com>
Tue, 14 Feb 2023 10:47:01 +0000 (11:47 +0100)
committerChocobozzz <me@florianbigard.com>
Tue, 14 Feb 2023 10:51:44 +0000 (11:51 +0100)
Correctly handle external auth redirection in all cases

21 files changed:
client/src/app/+error-page/error-page.component.html
client/src/app/+error-page/error-page.module.ts
client/src/app/+reset-password/reset-password.component.ts
client/src/app/+videos/+video-watch/shared/action-buttons/video-rate.component.ts
client/src/app/+videos/+video-watch/shared/comment/video-comment-add.component.html
client/src/app/+videos/+video-watch/shared/comment/video-comment-add.component.ts
client/src/app/core/auth/auth.service.ts
client/src/app/core/routing/login-guard.service.ts
client/src/app/core/routing/redirect.service.ts
client/src/app/core/routing/user-right-guard.service.ts
client/src/app/menu/menu.component.html
client/src/app/menu/menu.component.scss
client/src/app/menu/menu.component.ts
client/src/app/shared/shared-main/angular/index.ts
client/src/app/shared/shared-main/angular/link.component.html
client/src/app/shared/shared-main/angular/link.component.scss
client/src/app/shared/shared-main/angular/link.component.ts
client/src/app/shared/shared-main/angular/login-link.component.html [new file with mode: 0644]
client/src/app/shared/shared-main/angular/login-link.component.ts [new file with mode: 0644]
client/src/app/shared/shared-main/shared-main.module.ts
client/src/app/shared/shared-user-subscription/subscribe-button.component.ts

index 070985d3c62874b95fac7dcaede0deca33efc073..b8b9296a3d1c8e6762dc9c527a0d54d6732ba728 100644 (file)
       <ng-container *ngIf="type !== 'video'" i18n>You might need to login to see the resource.</ng-container>
     </div>
 
-    <a class="peertube-button-big-link orange-button mt-5" i18n routerLink="/login">
-      Login
-    </a>
-
+    <my-login-link className="peertube-button-big-link orange-button mt-5"></my-login-link>
   </div>
 
   <div *ngIf="status === 403" class="box">
index 807c6f5a958ddaac17858261833f154f1be5e1f0..34f026ea8b88c24960aa86b287d5e0163f6dd52c 100644 (file)
@@ -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
   ],
index 44216f978666a72809a2f5f9a0cbdbf82365301f..a6c05bbfb0d47789aa047c687e72fa6d26e84d15 100644 (file)
@@ -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' ])
         },
 
index 0fef246b372f11fb5c9b4a2f653dd18b18d0d647..d0c138834cf6745bca20af757c1c1e2410e72298 100644 (file)
@@ -82,7 +82,7 @@ export class VideoRateComponent implements OnInit, OnChanges, OnDestroy {
   getRatePopoverText () {
     if (this.isUserLoggedIn) return undefined
 
-    return $localize`You need to be <a href="/login">logged in</a> to rate this video.`
+    return $localize`You need to be logged in to rate this video.`
   }
 
   private checkUserRating () {
index 6bc201f3221a60526564754d8107efaa966cd16a..203e9ce62d9e15c2beefb3213ffe6e54592643c2 100644 (file)
       (click)="hideModals()" (key.enter)="hideModals()"
     >
 
-    <input
-      type="submit" i18n-value value="Login to comment" class="peertube-button orange-button"
-      (click)="gotoLogin()"
-    >
+    <my-login-link i18n-label label="Login to comment" className="peertube-button-link orange-button"></my-login-link>
   </div>
 </ng-template>
 
index 9a9bfe710d56694c9795939ea6ec072881663ccb..033097084e0b9e3bcfa4e23cbc5f995d417533f8 100644 (file)
@@ -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 = ''
index ed7eabb76c5442dae30e1379b9b04d96b24603e7..6fe601d8d24634275bfb0e555e3560d5aad07f3d 100644 (file)
@@ -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<any>
 
   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.`
index a949be14c18f81af6dd79b5eced3b99ed67d5949..2f5a31e7f3011fe84e7b0f36856fbeeba1eba508 100644 (file)
@@ -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
   }
 
index 1344458d556c00d7ca0200d9ab5cadb2d1b98509..e239c62103f2e6286d25c18843e3865e0e929014 100644 (file)
@@ -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)
 
index a2ce772dbc6cf9e280834987909dc48a34d8d53b..c6bd05bb695cab301b04188c88d8b2a35a5fef7f 100644 (file)
@@ -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
   }
 
index abfc151e7aa65cd85c3ce10858c9b6e7f6dc84b3..0fb9897b9e721ec203f9bfe4700ca6a172a12ae7 100644 (file)
       </div>
 
       <div *ngIf="!isLoggedIn" class="login-buttons-block">
-        <a i18n *ngIf="!getExternalLoginHref()" routerLink="/login" class="peertube-button-link orange-button">Login</a>
-        <a i18n *ngIf="getExternalLoginHref()" [href]="getExternalLoginHref()" class="peertube-button-link orange-button">Login</a>
+        <my-login-link className="peertube-button-link orange-button w-100"></my-login-link>
 
         <a *ngIf="isRegistrationAllowed()" routerLink="/signup" class="peertube-button-link create-account-button">
           <my-signup-label [requiresApproval]="requiresApproval"></my-signup-label>
index cd57e134e7c5357bd6279fb8dc399f6508012719..9d5ad57c246c9f3c5e10b706262724568183d4bb 100644 (file)
@@ -251,7 +251,8 @@ my-actor-avatar {
 .login-buttons-block {
   margin: 30px 25px 35px;
 
-  > a {
+  > a,
+  > my-login-link {
     display: block;
     width: 100%;
 
index fc6d74cff105594aa28b0feb9259bcd0fd8e2c5e..21354e52d273e9deeb119c8f407d593efa3ef4ba 100644 (file)
@@ -1,7 +1,6 @@
 import { HotkeysService } from 'angular2-hotkeys'
 import * as debug from 'debug'
 import { switchMap } from 'rxjs/operators'
-import { environment } from 'src/environments/environment'
 import { ViewportScroller } from '@angular/common'
 import { Component, OnInit, ViewChild } from '@angular/core'
 import { Router } from '@angular/router'
@@ -22,7 +21,6 @@ import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
 import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service'
 import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
-import { PluginsManager } from '@root-helpers/plugins-manager'
 import { HTMLServerConfig, ServerConfig, UserRight, VideoConstant } from '@shared/models'
 
 const debugLogger = debug('peertube:menu:MenuComponent')
@@ -135,10 +133,6 @@ export class MenuComponent implements OnInit {
       .subscribe(() => this.openQuickSettings())
   }
 
-  getExternalLoginHref () {
-    return PluginsManager.getDefaultLoginHref(environment.apiUrl, this.serverConfig)
-  }
-
   isRegistrationAllowed () {
     if (!this.serverConfig) return false
 
index 4b87c2952121ac99874362b7bfa7c678c4e98a6b..f72d603132e5efda526faa15dd92c359c4e5ac33 100644 (file)
@@ -5,5 +5,6 @@ export * from './duration-formatter.pipe'
 export * from './from-now.pipe'
 export * from './infinite-scroller.directive'
 export * from './link.component'
+export * from './login-link.component'
 export * from './number-formatter.pipe'
 export * from './peertube-template.directive'
index e61a1e085d43930fb71a7e48dbeb8b08b5ca86c7..2ea2df2b552c146d1074ba1277a31f0e86a51076 100644 (file)
@@ -2,10 +2,10 @@
   <ng-content></ng-content>
 </ng-template>
 
-<a *ngIf="!href" [routerLink]="internalLink" [attr.title]="title" [tabindex]="tabindex">
+<a *ngIf="!href" [routerLink]="internalLink" [attr.title]="title" [tabindex]="tabindex" [ngClass]="builtClasses">
   <ng-template *ngTemplateOutlet="content"></ng-template>
 </a>
 
-<a *ngIf="href" [href]="href" [target]="target" [attr.title]="title" [tabindex]="tabindex">
+<a *ngIf="href" [href]="href" [target]="target" [attr.title]="title" [tabindex]="tabindex" [ngClass]="builtClasses">
   <ng-template *ngTemplateOutlet="content"></ng-template>
 </a>
index bb86d54889e107657928948c6252dbce22029dc2..f54240d31b9129b7269160c62bd3c44f00da2a62 100644 (file)
@@ -1,4 +1,4 @@
-a {
+.no-class {
   color: inherit;
   text-decoration: inherit;
   position: inherit;
index ecbd9151c944f80b2ff459d880f67eb5734e1666..1f5975589472880e12df07c1d41d1108b57b5577 100644 (file)
@@ -1,17 +1,27 @@
-import { Component, Input } from '@angular/core'
+import { Component, Input, OnInit } from '@angular/core'
 
 @Component({
   selector: 'my-link',
   styleUrls: [ './link.component.scss' ],
   templateUrl: './link.component.html'
 })
-export class LinkComponent {
+export class LinkComponent implements OnInit {
   @Input() internalLink?: string | any[]
 
   @Input() href?: string
-  @Input() target?: string
+  @Input() target = '_self'
 
   @Input() title?: string
 
+  @Input() className?: string
+
   @Input() tabindex: string | number
+
+  builtClasses: string
+
+  ngOnInit () {
+    this.builtClasses = this.className
+      ? this.className
+      : 'no-class'
+  }
 }
diff --git a/client/src/app/shared/shared-main/angular/login-link.component.html b/client/src/app/shared/shared-main/angular/login-link.component.html
new file mode 100644 (file)
index 0000000..f90ffba
--- /dev/null
@@ -0,0 +1 @@
+<my-link i18n internalLink="/login" [href]="getExternalLoginHref()" [className]="className">{{ label }}</my-link>
diff --git a/client/src/app/shared/shared-main/angular/login-link.component.ts b/client/src/app/shared/shared-main/angular/login-link.component.ts
new file mode 100644 (file)
index 0000000..7e3a773
--- /dev/null
@@ -0,0 +1,22 @@
+import { environment } from 'src/environments/environment'
+import { Component, Input } from '@angular/core'
+import { ServerService } from '@app/core'
+import { PluginsManager } from '@root-helpers/plugins-manager'
+
+@Component({
+  selector: 'my-login-link',
+  templateUrl: './login-link.component.html'
+})
+export class LoginLinkComponent {
+  @Input() label = $localize`Login`
+
+  @Input() className?: string
+
+  constructor (private server: ServerService) {
+
+  }
+
+  getExternalLoginHref () {
+    return PluginsManager.getDefaultLoginHref(environment.apiUrl, this.server.getHTMLConfig())
+  }
+}
index eb1642d9750f1d1da0834865567bdfb045b6da60..0bdf272634fbf2496643bb7a7c06595d7a1efc1c 100644 (file)
@@ -25,6 +25,7 @@ import {
   FromNowPipe,
   InfiniteScrollerDirective,
   LinkComponent,
+  LoginLinkComponent,
   NumberFormatterPipe,
   PeerTubeTemplateDirective
 } from './angular'
@@ -92,6 +93,7 @@ import { VideoChannelService } from './video-channel'
     InfiniteScrollerDirective,
     PeerTubeTemplateDirective,
     LinkComponent,
+    LoginLinkComponent,
 
     ActionDropdownComponent,
     ButtonComponent,
@@ -152,6 +154,7 @@ import { VideoChannelService } from './video-channel'
     InfiniteScrollerDirective,
     PeerTubeTemplateDirective,
     LinkComponent,
+    LoginLinkComponent,
 
     ActionDropdownComponent,
     ButtonComponent,
index 7a55a6ffbed3b8189da41f203fdf0ee8d807cd90..a002bf4e7d749919297e20df7e1c02f1ab6b365c 100644 (file)
@@ -1,7 +1,6 @@
 import { concat, forkJoin, merge } from 'rxjs'
 import { Component, Input, OnChanges, OnInit } from '@angular/core'
-import { Router } from '@angular/router'
-import { AuthService, Notifier } from '@app/core'
+import { AuthService, Notifier, RedirectService } from '@app/core'
 import { Account, VideoChannel, VideoService } from '@app/shared/shared-main'
 import { FeedFormat } from '@shared/models'
 import { UserSubscriptionService } from './user-subscription.service'
@@ -27,7 +26,7 @@ export class SubscribeButtonComponent implements OnInit, OnChanges {
 
   constructor (
     private authService: AuthService,
-    private router: Router,
+    private redirectService: RedirectService,
     private notifier: Notifier,
     private userSubscriptionService: UserSubscriptionService,
     private videoService: VideoService
@@ -152,7 +151,7 @@ export class SubscribeButtonComponent implements OnInit, OnChanges {
   }
 
   gotoLogin () {
-    this.router.navigate([ '/login' ])
+    this.redirectService.redirectToLogin()
   }
 
   subscribeStatus (subscribed: boolean) {