Add URL redirection support for external auth

This commit is contained in:
Chocobozzz 2022-05-09 10:47:46 +02:00
parent f82ea670d8
commit cb28bb92da
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 18 additions and 7 deletions

View File

@ -1,7 +1,7 @@
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core' import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'
import { ActivatedRoute } from '@angular/router' import { ActivatedRoute } from '@angular/router'
import { AuthService, Notifier, RedirectService, UserService } from '@app/core' import { AuthService, Notifier, RedirectService, SessionStorageService, UserService } from '@app/core'
import { HooksService } from '@app/core/plugins/hooks.service' import { HooksService } from '@app/core/plugins/hooks.service'
import { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/form-validators/login-validators' import { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/form-validators/login-validators'
import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
@ -17,6 +17,8 @@ import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models'
}) })
export class LoginComponent extends FormReactive implements OnInit, AfterViewInit { export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
private static SESSION_STORAGE_REDIRECT_URL_KEY = 'login-previous-url'
@ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
accordion: NgbAccordion accordion: NgbAccordion
@ -46,7 +48,8 @@ export class LoginComponent extends FormReactive implements OnInit, AfterViewIni
private userService: UserService, private userService: UserService,
private redirectService: RedirectService, private redirectService: RedirectService,
private notifier: Notifier, private notifier: Notifier,
private hooks: HooksService private hooks: HooksService,
private storage: SessionStorageService
) { ) {
super() super()
} }
@ -88,6 +91,8 @@ export class LoginComponent extends FormReactive implements OnInit, AfterViewIni
this.externalAuthError = true this.externalAuthError = true
return return
} }
this.storage.setItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY, this.redirectService.getPreviousUrl())
} }
ngAfterViewInit () { ngAfterViewInit () {
@ -151,7 +156,9 @@ The link will expire within 1 hour.`
this.authService.login(username, null, token) this.authService.login(username, null, token)
.subscribe({ .subscribe({
next: () => this.redirectService.redirectToPreviousRoute(), next: () => {
this.redirectService.redirectToPreviousRoute(this.storage.getItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY))
},
error: err => { error: err => {
this.handleError(err) this.handleError(err)

View File

@ -96,7 +96,7 @@ export class VideoChannelUpdateComponent extends VideoChannelEdit implements OnI
this.notifier.success($localize`Video channel ${videoChannelUpdate.displayName} updated.`) this.notifier.success($localize`Video channel ${videoChannelUpdate.displayName} updated.`)
this.redirectService.redirectToPreviousRoute([ '/c', this.videoChannel.name ]) this.redirectService.redirectToPreviousRoute('/c/' + this.videoChannel.name)
}, },
error: err => { error: err => {

View File

@ -46,19 +46,23 @@ export class RedirectService {
return this.defaultTrendingAlgorithm return this.defaultTrendingAlgorithm
} }
redirectToPreviousRoute (fallbackRoute: string[] = null) { getPreviousUrl () {
return this.previousUrl
}
redirectToPreviousRoute (fallbackRoute?: string) {
const exceptions = [ const exceptions = [
'/verify-account', '/verify-account',
'/reset-password' '/reset-password'
] ]
if (this.previousUrl) { if (this.previousUrl && this.previousUrl !== '/') {
const isException = exceptions.find(e => this.previousUrl.startsWith(e)) const isException = exceptions.find(e => this.previousUrl.startsWith(e))
if (!isException) return this.router.navigateByUrl(this.previousUrl) if (!isException) return this.router.navigateByUrl(this.previousUrl)
} }
if (fallbackRoute) { if (fallbackRoute) {
return this.router.navigate(fallbackRoute) return this.router.navigateByUrl(fallbackRoute)
} }
return this.redirectToHomepage() return this.redirectToHomepage()