Redirect to the last url on login

This commit is contained in:
Chocobozzz 2018-12-11 15:27:46 +01:00
parent f481c4f9f3
commit dae5ca24b1
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
6 changed files with 25 additions and 22 deletions

View File

@ -14,7 +14,7 @@ import { AuthUser } from './auth-user.model'
import { objectToUrlEncoded } from '@app/shared/misc/utils' import { objectToUrlEncoded } from '@app/shared/misc/utils'
import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage' import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
import { I18n } from '@ngx-translate/i18n-polyfill' import { I18n } from '@ngx-translate/i18n-polyfill'
import { HotkeysService, Hotkey } from 'angular2-hotkeys' import { Hotkey, HotkeysService } from 'angular2-hotkeys'
interface UserLoginWithUsername extends UserLogin { interface UserLoginWithUsername extends UserLogin {
access_token: string access_token: string
@ -38,7 +38,6 @@ export class AuthService {
loginChangedSource: Observable<AuthStatus> loginChangedSource: Observable<AuthStatus>
userInformationLoaded = new ReplaySubject<boolean>(1) userInformationLoaded = new ReplaySubject<boolean>(1)
hotkeys: Hotkey[] hotkeys: Hotkey[]
redirectUrl: string
private clientId: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID) 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) 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.setStatus(AuthStatus.LoggedOut)
this.hotkeysService.remove(this.hotkeys) this.hotkeysService.remove(this.hotkeys)
this.redirectUrl = null
} }
refreshAccessToken () { refreshAccessToken () {

View File

@ -20,8 +20,6 @@ export class LoginGuard implements CanActivate, CanActivateChild {
canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.auth.isLoggedIn() === true) return true if (this.auth.isLoggedIn() === true) return true
this.auth.redirectUrl = state.url
this.router.navigate([ '/login' ]) this.router.navigate([ '/login' ])
return false return false
} }

View File

@ -1,5 +1,5 @@
import { Injectable } from '@angular/core' import { Injectable } from '@angular/core'
import { Router } from '@angular/router' import { NavigationEnd, Router } from '@angular/router'
import { ServerService } from '../server' import { ServerService } from '../server'
@Injectable() @Injectable()
@ -8,6 +8,9 @@ export class RedirectService {
static INIT_DEFAULT_ROUTE = '/videos/trending' static INIT_DEFAULT_ROUTE = '/videos/trending'
static DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE static DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE
private previousUrl: string
private currentUrl: string
constructor ( constructor (
private router: Router, private router: Router,
private serverService: ServerService private serverService: ServerService
@ -18,6 +21,7 @@ export class RedirectService {
RedirectService.DEFAULT_ROUTE = config.instance.defaultClientRoute RedirectService.DEFAULT_ROUTE = config.instance.defaultClientRoute
} }
// Load default route
this.serverService.configLoaded this.serverService.configLoaded
.subscribe(() => { .subscribe(() => {
const defaultRouteConfig = this.serverService.getConfig().instance.defaultClientRoute const defaultRouteConfig = this.serverService.getConfig().instance.defaultClientRoute
@ -26,6 +30,21 @@ export class RedirectService {
RedirectService.DEFAULT_ROUTE = 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 () {
if (this.previousUrl) return this.router.navigateByUrl(this.previousUrl)
return this.redirectToHomepage()
} }
redirectToHomepage (skipLocationChange = false) { redirectToHomepage (skipLocationChange = false) {

View File

@ -64,7 +64,7 @@ export class LoginComponent extends FormReactive implements OnInit {
this.authService.login(username, password) this.authService.login(username, password)
.subscribe( .subscribe(
() => this.redirect(), () => this.redirectService.redirectToPreviousRoute(),
err => { err => {
if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.') 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 () { askResetPassword () {
this.userService.askResetPassword(this.forgotPasswordEmail) this.userService.askResetPassword(this.forgotPasswordEmail)
.subscribe( .subscribe(

View File

@ -50,11 +50,10 @@ export class SubscribeButtonComponent implements OnInit {
subscribe () { subscribe () {
if (this.isUserLoggedIn()) { if (this.isUserLoggedIn()) {
this.localSubscribe() return this.localSubscribe()
} else {
this.authService.redirectUrl = this.router.url
this.gotoLogin()
} }
return this.gotoLogin()
} }
localSubscribe () { localSubscribe () {

View File

@ -135,7 +135,6 @@ export class VideoCommentAddComponent extends FormReactive implements OnInit {
gotoLogin () { gotoLogin () {
this.hideVisitorModal() this.hideVisitorModal()
this.authService.redirectUrl = this.router.url
this.router.navigate([ '/login' ]) this.router.navigate([ '/login' ])
} }