handle the logout route based on app config (#6587)

This commit is contained in:
Maurizio Vitale
2021-01-28 13:09:37 +00:00
committed by GitHub
parent 3010cfb310
commit a5e41e8492
2 changed files with 49 additions and 3 deletions

View File

@@ -20,6 +20,7 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { of, throwError } from 'rxjs'; import { of, throwError } from 'rxjs';
import { AuthenticationService } from '../services'; import { AuthenticationService } from '../services';
import { AppConfigService } from '../app-config';
import { setupTestBed } from '../testing/setup-test-bed'; import { setupTestBed } from '../testing/setup-test-bed';
import { LogoutDirective } from './logout.directive'; import { LogoutDirective } from './logout.directive';
import { CoreTestingModule } from '../testing/core.testing.module'; import { CoreTestingModule } from '../testing/core.testing.module';
@@ -41,6 +42,7 @@ describe('LogoutDirective', () => {
let fixture: ComponentFixture<TestComponent>; let fixture: ComponentFixture<TestComponent>;
let router: Router; let router: Router;
let authService: AuthenticationService; let authService: AuthenticationService;
let appConfig: AppConfigService;
setupTestBed({ setupTestBed({
imports: [ imports: [
@@ -55,11 +57,13 @@ describe('LogoutDirective', () => {
beforeEach(() => { beforeEach(() => {
router = TestBed.inject(Router); router = TestBed.inject(Router);
authService = TestBed.inject(AuthenticationService); authService = TestBed.inject(AuthenticationService);
appConfig = TestBed.inject(AppConfigService);
fixture = TestBed.createComponent(TestComponent); fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges(); fixture.detectChanges();
appConfig.config['loginRoute'] = undefined;
}); });
it('should redirect to login on click', () => { it('should redirect to login route if basic auth and loginRoute NOT defined', () => {
spyOn(router, 'navigate'); spyOn(router, 'navigate');
spyOn(authService, 'logout').and.returnValue(of(true)); spyOn(authService, 'logout').and.returnValue(of(true));
@@ -70,6 +74,32 @@ describe('LogoutDirective', () => {
expect(router.navigate).toHaveBeenCalledWith(['/login']); expect(router.navigate).toHaveBeenCalledWith(['/login']);
}); });
it('should redirect to loginRoute if basic auth and loginRoute defined', () => {
spyOn(router, 'navigate');
spyOn(authService, 'isOauth').and.returnValue(false);
appConfig.config['loginRoute'] = 'fake-base-logout';
spyOn(authService, 'logout').and.returnValue(of(true));
const button = fixture.nativeElement.querySelector('button');
button.click();
expect(authService.logout).toHaveBeenCalled();
expect(router.navigate).toHaveBeenCalledWith(['fake-base-logout']);
});
it('should redirect to redirectUriLogout on click if SSO auth', () => {
spyOn(router, 'navigate');
spyOn(authService, 'isOauth').and.returnValue(true);
spyOn(authService, 'logout').and.returnValue(of(true));
appConfig.config['oauth2.redirectUriLogout'] = 'fake-logout';
const button = fixture.nativeElement.querySelector('button');
button.click();
expect(authService.logout).toHaveBeenCalled();
expect(router.navigate).toHaveBeenCalledWith(['fake-logout']);
});
it('should redirect to login even on logout error', () => { it('should redirect to login even on logout error', () => {
spyOn(router, 'navigate'); spyOn(router, 'navigate');
spyOn(authService, 'logout').and.returnValue(throwError('err')); spyOn(authService, 'logout').and.returnValue(throwError('err'));
@@ -167,5 +197,6 @@ describe('LogoutDirective', () => {
expect(authService.logout).toHaveBeenCalled(); expect(authService.logout).toHaveBeenCalled();
expect(router.navigate).not.toHaveBeenCalled(); expect(router.navigate).not.toHaveBeenCalled();
}); });
}); });
}); });

View File

@@ -17,6 +17,7 @@
import { Input, Directive, ElementRef, OnInit, Renderer2 } from '@angular/core'; import { Input, Directive, ElementRef, OnInit, Renderer2 } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { AppConfigService } from '../app-config/app-config.service';
import { AuthenticationService } from '../services/authentication.service'; import { AuthenticationService } from '../services/authentication.service';
@Directive({ @Directive({
@@ -26,7 +27,7 @@ export class LogoutDirective implements OnInit {
/** URI to redirect to after logging out. */ /** URI to redirect to after logging out. */
@Input() @Input()
redirectUri: string = '/login'; redirectUri: string;
/** Enable redirecting after logout */ /** Enable redirecting after logout */
@Input() @Input()
@@ -35,10 +36,12 @@ export class LogoutDirective implements OnInit {
constructor(private elementRef: ElementRef, constructor(private elementRef: ElementRef,
private renderer: Renderer2, private renderer: Renderer2,
private router: Router, private router: Router,
private appConfig: AppConfigService,
private auth: AuthenticationService) { private auth: AuthenticationService) {
} }
ngOnInit() { ngOnInit() {
if (this.elementRef.nativeElement) { if (this.elementRef.nativeElement) {
this.renderer.listen(this.elementRef.nativeElement, 'click', (evt) => { this.renderer.listen(this.elementRef.nativeElement, 'click', (evt) => {
evt.preventDefault(); evt.preventDefault();
@@ -47,6 +50,17 @@ export class LogoutDirective implements OnInit {
} }
} }
getRedirectUri () {
if (this.redirectUri === undefined ) {
if (this.auth.isOauth()) {
return this.appConfig.get<string>('oauth2.redirectUriLogout');
} else {
return this.appConfig.get<string>('loginRoute', '/login');
}
}
return this.redirectUri;
}
logout() { logout() {
this.auth.logout().subscribe( this.auth.logout().subscribe(
() => this.redirectToUri(), () => this.redirectToUri(),
@@ -55,8 +69,9 @@ export class LogoutDirective implements OnInit {
} }
redirectToUri() { redirectToUri() {
const redirectRoute = this.getRedirectUri();
if (this.enableRedirect) { if (this.enableRedirect) {
this.router.navigate([this.redirectUri]); this.router.navigate([redirectRoute]);
} }
} }
} }