Skip to content

Commit 2cad6b5

Browse files
author
FrancescoMauto
committed
[DSC-2400] edit: ported setRedirectUrl method and setRedirectUrlAndNaviogateAction from uon
1 parent b854a9b commit 2cad6b5

5 files changed

Lines changed: 45 additions & 9 deletions

File tree

src/app/core/auth/auth.actions.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const AuthActionTypes = {
3434
LOG_OUT_ERROR: type('dspace/auth/LOG_OUT_ERROR'),
3535
LOG_OUT_SUCCESS: type('dspace/auth/LOG_OUT_SUCCESS'),
3636
SET_REDIRECT_URL: type('dspace/auth/SET_REDIRECT_URL'),
37+
SET_REDIRECT_URL_AND_NAVIGATE: type('dspace/auth/SET_REDIRECT_URL_AND_NAVIGATE'),
3738
RETRIEVE_AUTHENTICATED_EPERSON: type('dspace/auth/RETRIEVE_AUTHENTICATED_EPERSON'),
3839
RETRIEVE_AUTHENTICATED_EPERSON_SUCCESS: type('dspace/auth/RETRIEVE_AUTHENTICATED_EPERSON_SUCCESS'),
3940
RETRIEVE_AUTHENTICATED_EPERSON_ERROR: type('dspace/auth/RETRIEVE_AUTHENTICATED_EPERSON_ERROR'),
@@ -359,6 +360,23 @@ export class SetRedirectUrlAction implements Action {
359360
}
360361
}
361362

363+
/**
364+
* Change the redirect url.
365+
* @class SetRedirectUrlAction
366+
* @implements {Action}
367+
*/
368+
export class SetRedirectUrlAndNavigateAction implements Action {
369+
public type: string = AuthActionTypes.SET_REDIRECT_URL_AND_NAVIGATE;
370+
payload: {
371+
redirectUrl: string;
372+
navigateUrl: string;
373+
};
374+
375+
constructor(redirectUrl: string, navigateUrl: string) {
376+
this.payload = { redirectUrl, navigateUrl };
377+
}
378+
}
379+
362380
/**
363381
* Start loading for a hard redirect
364382
* @class StartHardRedirectLoadingAction

src/app/core/auth/auth.effects.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ import {
5151
RetrieveAuthMethodsErrorAction,
5252
RetrieveAuthMethodsSuccessAction,
5353
RetrieveTokenAction,
54-
SetUserAsIdleAction
54+
SetRedirectUrlAndNavigateAction,
55+
SetUserAsIdleAction,
5556
} from './auth.actions';
5657
import { hasValue, isNotNull } from '../../shared/empty.util';
5758
import { Router } from '@angular/router';
@@ -123,6 +124,13 @@ export class AuthEffects {
123124
})
124125
), { dispatch: false });
125126

127+
public redirectAndNavigate$: Observable<Action> = createEffect(() => this.actions$
128+
.pipe(ofType(AuthActionTypes.SET_REDIRECT_URL_AND_NAVIGATE),
129+
tap((action: SetRedirectUrlAndNavigateAction) => this.router.navigate([decodeURIComponent(action.payload.navigateUrl)])),
130+
),
131+
{ dispatch: false },
132+
);
133+
126134
// It means "reacts to this action but don't send another"
127135
public authenticatedError$: Observable<Action> = createEffect(() => this.actions$.pipe(
128136
ofType(AuthActionTypes.AUTHENTICATED_ERROR),

src/app/core/auth/auth.reducer.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import {
1212
RefreshTokenAndRedirectSuccessAction,
1313
RefreshTokenSuccessAction,
1414
RetrieveAuthenticatedEpersonSuccessAction,
15-
RetrieveAuthMethodsSuccessAction, SetAuthCookieStatus,
16-
SetRedirectUrlAction
15+
RetrieveAuthMethodsSuccessAction,
16+
SetAuthCookieStatus,
17+
SetRedirectUrlAction,
18+
SetRedirectUrlAndNavigateAction,
1719
} from './auth.actions';
1820
// import models
1921
import { AuthTokenInfo } from './models/auth-token-info.model';
@@ -266,6 +268,11 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
266268
redirectUrl: (action as SetRedirectUrlAction).payload,
267269
});
268270

271+
case AuthActionTypes.SET_REDIRECT_URL_AND_NAVIGATE:
272+
return Object.assign({}, state, {
273+
redirectUrl: (action as SetRedirectUrlAndNavigateAction).payload.redirectUrl,
274+
});
275+
269276
case AuthActionTypes.REDIRECT_AFTER_LOGIN_SUCCESS:
270277
return Object.assign({}, state, {
271278
loading: true,

src/app/core/auth/auth.service.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
RefreshTokenAction,
3232
ResetAuthenticationMessagesAction, SetAuthCookieStatus,
3333
SetRedirectUrlAction,
34+
SetRedirectUrlAndNavigateAction,
3435
SetUserAsIdleAction,
3536
UnsetUserAsIdleAction
3637
} from './auth.actions';
@@ -518,15 +519,19 @@ export class AuthService {
518519
/**
519520
* Set redirect url
520521
*/
521-
setRedirectUrl(url: string) {
522+
setRedirectUrl(redirectUrl: string, navigateUrl?: string) {
522523
// Add 1 hour to the current date
523524
const expireDate = Date.now() + (1000 * 60 * 60);
524525

525526
// Set the cookie expire date
526527
const expires = new Date(expireDate);
527-
const options: CookieAttributes = {expires: expires};
528-
this.storage.set(REDIRECT_COOKIE, url, options);
529-
this.store.dispatch(new SetRedirectUrlAction(isNotUndefined(url) ? url : ''));
528+
const options: Cookies.CookieAttributes = { expires: expires };
529+
this.storage.set(REDIRECT_COOKIE, redirectUrl, options);
530+
if (hasValue(navigateUrl)) {
531+
this.store.dispatch(new SetRedirectUrlAndNavigateAction(isNotUndefined(redirectUrl) ? redirectUrl : '', navigateUrl));
532+
} else {
533+
this.store.dispatch(new SetRedirectUrlAction(isNotUndefined(redirectUrl) ? redirectUrl : ''));
534+
}
530535
}
531536

532537
/**

src/app/core/shared/operators.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ describe('Core Module - RxJS Operators', () => {
267267
expectObservable(source.pipe(redirectOn4xx(router, authService))).toBe(expected, values);
268268
flush();
269269
expect(authService.setRedirectUrl).toHaveBeenCalled();
270-
expect(router.navigateByUrl).toHaveBeenCalledWith('login');
271270
});
272271
});
273272

@@ -281,7 +280,6 @@ describe('Core Module - RxJS Operators', () => {
281280
expectObservable(source.pipe(redirectOn4xx(router, authService))).toBe(expected, values);
282281
flush();
283282
expect(authService.setRedirectUrl).toHaveBeenCalled();
284-
expect(router.navigateByUrl).toHaveBeenCalledWith('login');
285283
});
286284
});
287285
});

0 commit comments

Comments
 (0)