]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Redirect to the last url on login
authorChocobozzz <me@florianbigard.com>
Tue, 11 Dec 2018 14:27:46 +0000 (15:27 +0100)
committerChocobozzz <me@florianbigard.com>
Tue, 11 Dec 2018 14:27:46 +0000 (15:27 +0100)
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/login/login.component.ts
client/src/app/shared/user-subscription/subscribe-button.component.ts
client/src/app/videos/+video-watch/comment/video-comment-add.component.ts

index 443772c9e7b2d6f74813ae0711b41635912662f4..5f5730e026748cb78d463c308a8b2666d9c87585 100644 (file)
@@ -14,7 +14,7 @@ import { AuthUser } from './auth-user.model'
 import { objectToUrlEncoded } from '@app/shared/misc/utils'
 import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
 import { I18n } from '@ngx-translate/i18n-polyfill'
-import { HotkeysService, Hotkey } from 'angular2-hotkeys'
+import { Hotkey, HotkeysService } from 'angular2-hotkeys'
 
 interface UserLoginWithUsername extends UserLogin {
   access_token: string
@@ -38,7 +38,6 @@ export class AuthService {
   loginChangedSource: Observable<AuthStatus>
   userInformationLoaded = new ReplaySubject<boolean>(1)
   hotkeys: Hotkey[]
-  redirectUrl: string
 
   private clientId: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID)
   private clientSecret: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET)
@@ -178,8 +177,6 @@ export class AuthService {
     this.setStatus(AuthStatus.LoggedOut)
 
     this.hotkeysService.remove(this.hotkeys)
-
-    this.redirectUrl = null
   }
 
   refreshAccessToken () {
index 40ff8f5059491177fd9695e01ec22c675aed81cc..18bc41ca6301e9e8811f851dbd3c7cf256b33356 100644 (file)
@@ -20,8 +20,6 @@ export class LoginGuard implements CanActivate, CanActivateChild {
   canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
     if (this.auth.isLoggedIn() === true) return true
 
-    this.auth.redirectUrl = state.url
-
     this.router.navigate([ '/login' ])
     return false
   }
index 1881be117e4a9d20bc4a9e87ee25241474a26468..e1db4097b87ff35934529538a2c86487ae22e9a8 100644 (file)
@@ -1,5 +1,5 @@
 import { Injectable } from '@angular/core'
-import { Router } from '@angular/router'
+import { NavigationEnd, Router } from '@angular/router'
 import { ServerService } from '../server'
 
 @Injectable()
@@ -8,6 +8,9 @@ export class RedirectService {
   static INIT_DEFAULT_ROUTE = '/videos/trending'
   static DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE
 
+  private previousUrl: string
+  private currentUrl: string
+
   constructor (
     private router: Router,
     private serverService: ServerService
@@ -18,6 +21,7 @@ export class RedirectService {
       RedirectService.DEFAULT_ROUTE = config.instance.defaultClientRoute
     }
 
+    // Load default route
     this.serverService.configLoaded
         .subscribe(() => {
           const defaultRouteConfig = this.serverService.getConfig().instance.defaultClientRoute
@@ -26,6 +30,21 @@ export class RedirectService {
             RedirectService.DEFAULT_ROUTE = defaultRouteConfig
           }
         })
+
+    // Track previous url
+    this.currentUrl = this.router.url
+    router.events.subscribe(event => {
+      if (event instanceof NavigationEnd) {
+        this.previousUrl = this.currentUrl
+        this.currentUrl = event.url
+      }
+    })
+  }
+
+  redirectToPreviousRoute () {
+    if (this.previousUrl) return this.router.navigateByUrl(this.previousUrl)
+
+    return this.redirectToHomepage()
   }
 
   redirectToHomepage (skipLocationChange = false) {
index 212a8ff1f60698c1f62c6022cea955c77083f62b..18f8f69e57ec5f396d7a7c7aceaf8b98179e91c9 100644 (file)
@@ -64,7 +64,7 @@ export class LoginComponent extends FormReactive implements OnInit {
 
     this.authService.login(username, password)
       .subscribe(
-        () => this.redirect(),
+        () => this.redirectService.redirectToPreviousRoute(),
 
         err => {
           if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
@@ -74,15 +74,6 @@ export class LoginComponent extends FormReactive implements OnInit {
       )
   }
 
-  redirect () {
-    const redirect = this.authService.redirectUrl
-    if (redirect) {
-      this.router.navigate([ redirect ])
-    } else {
-      this.redirectService.redirectToHomepage()
-    }
-  }
-
   askResetPassword () {
     this.userService.askResetPassword(this.forgotPasswordEmail)
       .subscribe(
index 315ea5037d8d13cb3ab1329a90eecea27d0aaf6c..9c8a15023e39005198c162c5bc7df400043a39dc 100644 (file)
@@ -50,11 +50,10 @@ export class SubscribeButtonComponent implements OnInit {
 
   subscribe () {
     if (this.isUserLoggedIn()) {
-      this.localSubscribe()
-    } else {
-      this.authService.redirectUrl = this.router.url
-      this.gotoLogin()
+      return this.localSubscribe()
     }
+
+    return this.gotoLogin()
   }
 
   localSubscribe () {
index 6db0eb55ddaa5277258dd841cf2cca3dab918d47..7f582c95011b03f8dd0e1c7029c9520e193ba2dd 100644 (file)
@@ -135,7 +135,6 @@ export class VideoCommentAddComponent extends FormReactive implements OnInit {
 
   gotoLogin () {
     this.hideVisitorModal()
-    this.authService.redirectUrl = this.router.url
     this.router.navigate([ '/login' ])
   }