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 router The router used to navigate to a new page
* @param authService Service to check if the user is authenticated * @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) =>
export const redirectOn4xx = <T>(router: Router, authService: AuthService, navigateAction?: (nav: boolean) => void) =>
(source: Observable<RemoteData<T>>): Observable<RemoteData<T>> => (source: Observable<RemoteData<T>>): Observable<RemoteData<T>> =>
source.pipe( source.pipe(
withLatestFrom(authService.isAuthenticated()), withLatestFrom(authService.isAuthenticated()),
filter(([rd, isAuthenticated]: [RemoteData<T>, boolean]) => { filter(([rd, isAuthenticated]: [RemoteData<T>, boolean]) => {
if (rd.hasFailed) { if (rd.hasFailed) {
if (rd.statusCode === 404 || rd.statusCode === 422) { if (rd.statusCode === 404 || rd.statusCode === 422) {
const promise = router.navigateByUrl(getPageNotFoundRoute(), { skipLocationChange: true }); router.navigateByUrl(getPageNotFoundRoute(), { skipLocationChange: true });
if (hasValue(navigateAction)) {
promise.then(navigateAction);
}
return false; return false;
} else if (rd.statusCode === 403 || rd.statusCode === 401) { } else if (rd.statusCode === 403 || rd.statusCode === 401) {
if (isAuthenticated) { if (isAuthenticated) {
const promise = router.navigateByUrl(getForbiddenRoute(), { skipLocationChange: true }); router.navigateByUrl(getForbiddenRoute(), { skipLocationChange: true });
if (hasValue(navigateAction)) {
promise.then(navigateAction);
}
return false; return false;
} else { } else {
authService.setRedirectUrl(router.url); authService.setRedirectUrl(router.url);

View File

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

View File

@@ -18,8 +18,7 @@ import { Location } from '@angular/common';
export class RegistrationGuard implements CanActivate { export class RegistrationGuard implements CanActivate {
constructor(private epersonRegistrationService: EpersonRegistrationService, constructor(private epersonRegistrationService: EpersonRegistrationService,
private router: Router, private router: Router,
private authService: AuthService, private authService: AuthService) {
private location: Location) {
} }
/** /**
@@ -33,10 +32,7 @@ export class RegistrationGuard implements CanActivate {
const token = route.params.token; const token = route.params.token;
return this.epersonRegistrationService.searchByToken(token).pipe( return this.epersonRegistrationService.searchByToken(token).pipe(
getFirstCompletedRemoteData(), getFirstCompletedRemoteData(),
// The replaceState call is an action taken by the Promise returned by navigateByUrl within redirectOn4xx redirectOn4xx(this.router, this.authService),
// 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)),
map((rd) => { map((rd) => {
route.data = { ...route.data, registration: rd }; route.data = { ...route.data, registration: rd };
return rd.hasSucceeded; return rd.hasSucceeded;