Commit ad33f6f9 authored by Christopher Fullarton's avatar Christopher Fullarton

Fixes logout if reloading the page where authentication is required

parent c61eae97
Pipeline #34480 passed with stages
in 17 minutes and 2 seconds
import { Injectable } from '@angular/core';
import { CanLoad, Route, Router, UrlSegment } from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { UserRole } from '../../lib/enums/UserRole';
import { UserService } from '../user/user.service';
......@@ -10,33 +12,40 @@ export class StaticLoginService implements CanLoad {
constructor(private router: Router, private userService: UserService) { }
public canLoad(route: Route, segments: UrlSegment[]): boolean {
if (this.isAllowedToProceed(route)) {
return true;
}
public canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> {
return new Observable<boolean>(subscriber => {
this.isAllowedToProceed(route).pipe(map(isAllowedToProceed => {
if (isAllowedToProceed) {
return true;
}
this.router.navigate(['/login'], {
queryParams: {
return: segments.map(segment => segment.path).join('/'),
},
this.router.navigate(['/login'], {
queryParams: {
return: segments.map(segment => segment.path).join('/'),
},
});
return false;
})).subscribe(isAllowedToProceed => {
subscriber.next(isAllowedToProceed);
subscriber.complete();
});
});
return false;
}
private isAllowedToProceed(route: Route): boolean {
if (!this.userService.isLoggedIn) {
return false;
}
private isAllowedToProceed(route: Route): Observable<boolean> {
return this.userService.loginNotifier.pipe(map(isLoggedIn => {
if (!isLoggedIn) {
return false;
}
switch (route.path) {
case 'i18n-manager':
return this.userService.isAuthorizedFor(UserRole.EditI18n);
case 'quiz-manager':
return this.userService.isAuthorizedFor(UserRole.CreateExpiredQuiz);
case 'admin':
return this.userService.isAuthorizedFor([UserRole.QuizAdmin, UserRole.SuperAdmin]);
default:
return true;
}
switch (route.path) {
case 'i18n-manager':
return this.userService.isAuthorizedFor(UserRole.EditI18n);
case 'admin':
return this.userService.isAuthorizedFor([UserRole.QuizAdmin, UserRole.SuperAdmin]);
default:
return true;
}
}));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment