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
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)
this.setStatus(AuthStatus.LoggedOut)
this.hotkeysService.remove(this.hotkeys)
-
- this.redirectUrl = null
}
refreshAccessToken () {
canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.auth.isLoggedIn() === true) return true
- this.auth.redirectUrl = state.url
-
this.router.navigate([ '/login' ])
return false
}
import { Injectable } from '@angular/core'
-import { Router } from '@angular/router'
+import { NavigationEnd, Router } from '@angular/router'
import { ServerService } from '../server'
@Injectable()
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
RedirectService.DEFAULT_ROUTE = config.instance.defaultClientRoute
}
+ // Load default route
this.serverService.configLoaded
.subscribe(() => {
const defaultRouteConfig = this.serverService.getConfig().instance.defaultClientRoute
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) {
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.')
)
}
- redirect () {
- const redirect = this.authService.redirectUrl
- if (redirect) {
- this.router.navigate([ redirect ])
- } else {
- this.redirectService.redirectToHomepage()
- }
- }
-
askResetPassword () {
this.userService.askResetPassword(this.forgotPasswordEmail)
.subscribe(
subscribe () {
if (this.isUserLoggedIn()) {
- this.localSubscribe()
- } else {
- this.authService.redirectUrl = this.router.url
- this.gotoLogin()
+ return this.localSubscribe()
}
+
+ return this.gotoLogin()
}
localSubscribe () {
gotoLogin () {
this.hideVisitorModal()
- this.authService.redirectUrl = this.router.url
this.router.navigate([ '/login' ])
}