]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/core/routing/redirect.service.ts
Merge branch 'master' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / core / routing / redirect.service.ts
index 844f184b414ac6cb3baad62fcfa13c2b20d7450a..3982cf36f31a6e1eea1559f2bea65f541a599046 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,30 +8,57 @@ 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
   ) {
     // The config is first loaded from the cache so try to get the default route
-    const config = this.serverService.getConfig()
-    if (config && config.instance && config.instance.defaultClientRoute) {
-      RedirectService.DEFAULT_ROUTE = config.instance.defaultClientRoute
+    const tmpConfig = this.serverService.getTmpConfig()
+    if (tmpConfig && tmpConfig.instance && tmpConfig.instance.defaultClientRoute) {
+      RedirectService.DEFAULT_ROUTE = tmpConfig.instance.defaultClientRoute
     }
 
-    this.serverService.configLoaded
-        .subscribe(() => {
-          const defaultRouteConfig = this.serverService.getConfig().instance.defaultClientRoute
+    // Load default route
+    this.serverService.getConfig()
+        .subscribe(config => {
+          const defaultRouteConfig = config.instance.defaultClientRoute
 
           if (defaultRouteConfig) {
             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 () {
+    const exceptions = [
+      '/verify-account',
+      '/reset-password'
+    ]
+
+    if (this.previousUrl) {
+      const isException = exceptions.find(e => this.previousUrl.startsWith(e))
+      if (!isException) return this.router.navigateByUrl(this.previousUrl)
+    }
+
+    return this.redirectToHomepage()
   }
 
-  redirectToHomepage () {
+  redirectToHomepage (skipLocationChange = false) {
     console.log('Redirecting to %s...', RedirectService.DEFAULT_ROUTE)
 
-    this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { replaceUrl: true })
+    this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { skipLocationChange })
         .catch(() => {
           console.error(
             'Cannot navigate to %s, resetting default route to %s.',
@@ -40,7 +67,7 @@ export class RedirectService {
           )
 
           RedirectService.DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE
-          return this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { replaceUrl: true })
+          return this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { skipLocationChange })
         })
 
   }