Merge branch 'w2p-88082_password-registration-link-fixes' into w2p-88082_password-registration-link-fixes-main

This commit is contained in:
Kristof De Langhe
2022-03-03 11:10:58 +01:00
3 changed files with 6 additions and 20 deletions

View File

@@ -197,27 +197,19 @@ export const getAllSucceededRemoteListPayload = <T>() =>
*
* @param router The router used to navigate to a new page
* @param authService Service to check if the user is authenticated
* @param navigateAction Optional action to take with the Promise returned by navigateByUrl
*/
// tslint:disable-next-line:no-empty
export const redirectOn4xx = <T>(router: Router, authService: AuthService, navigateAction?: (nav: boolean) => void) =>
export const redirectOn4xx = <T>(router: Router, authService: AuthService) =>
(source: Observable<RemoteData<T>>): Observable<RemoteData<T>> =>
source.pipe(
withLatestFrom(authService.isAuthenticated()),
filter(([rd, isAuthenticated]: [RemoteData<T>, boolean]) => {
if (rd.hasFailed) {
if (rd.statusCode === 404 || rd.statusCode === 422) {
const promise = router.navigateByUrl(getPageNotFoundRoute(), { skipLocationChange: true });
if (hasValue(navigateAction)) {
promise.then(navigateAction);
}
router.navigateByUrl(getPageNotFoundRoute(), { skipLocationChange: true });
return false;
} else if (rd.statusCode === 403 || rd.statusCode === 401) {
if (isAuthenticated) {
const promise = router.navigateByUrl(getForbiddenRoute(), { skipLocationChange: true });
if (hasValue(navigateAction)) {
promise.then(navigateAction);
}
router.navigateByUrl(getForbiddenRoute(), { skipLocationChange: true });
return false;
} else {
authService.setRedirectUrl(router.url);

View File

@@ -17,7 +17,6 @@ describe('RegistrationGuard', () => {
let epersonRegistrationService: EpersonRegistrationService;
let router: Router;
let authService: AuthService;
let location: Location;
let registration: Registration;
let registrationRD: RemoteData<Registration>;
@@ -61,9 +60,8 @@ describe('RegistrationGuard', () => {
isAuthenticated: observableOf(false),
setRedirectUrl: {},
});
location = jasmine.createSpyObj('location', ['replaceState']);
guard = new RegistrationGuard(epersonRegistrationService, router, authService, location);
guard = new RegistrationGuard(epersonRegistrationService, router, authService);
});
describe('canActivate', () => {

View File

@@ -18,8 +18,7 @@ import { Location } from '@angular/common';
export class RegistrationGuard implements CanActivate {
constructor(private epersonRegistrationService: EpersonRegistrationService,
private router: Router,
private authService: AuthService,
private location: Location) {
private authService: AuthService) {
}
/**
@@ -33,10 +32,7 @@ export class RegistrationGuard implements CanActivate {
const token = route.params.token;
return this.epersonRegistrationService.searchByToken(token).pipe(
getFirstCompletedRemoteData(),
// The replaceState call is an action taken by the Promise returned by navigateByUrl within redirectOn4xx
// It ensures the user stays on the correct URL when redirected to a 4xx page
// See this related issue's comment: https://github.com/angular/angular/issues/16981#issuecomment-549330207
redirectOn4xx(this.router, this.authService, () => this.location.replaceState(state.url)),
redirectOn4xx(this.router, this.authService),
map((rd) => {
route.data = { ...route.data, registration: rd };
return rd.hasSucceeded;