Skip to content

Commit cac0023

Browse files
author
FrancescoMauto
committed
[DSC-2400] edit: ported setRedirectUrl method and setRedirectUrlAndNaviogateAction from uon
1 parent 4b9e116 commit cac0023

5 files changed

Lines changed: 41 additions & 6 deletions

File tree

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

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

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import {
7979
RetrieveAuthMethodsErrorAction,
8080
RetrieveAuthMethodsSuccessAction,
8181
RetrieveTokenAction,
82+
SetRedirectUrlAndNavigateAction,
8283
SetUserAsIdleAction,
8384
} from './auth.actions';
8485
// import services
@@ -164,6 +165,13 @@ export class AuthEffects {
164165
}),
165166
), { dispatch: false });
166167

168+
public redirectAndNavigate$: Observable<Action> = createEffect(() => this.actions$
169+
.pipe(ofType(AuthActionTypes.SET_REDIRECT_URL_AND_NAVIGATE),
170+
tap((action: SetRedirectUrlAndNavigateAction) => this.router.navigate([decodeURIComponent(action.payload.navigateUrl)])),
171+
),
172+
{ dispatch: false },
173+
);
174+
167175
// It means "reacts to this action but don't send another"
168176
public authenticatedError$: Observable<Action> = createEffect(() => this.actions$.pipe(
169177
ofType(AuthActionTypes.AUTHENTICATED_ERROR),

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
RetrieveAuthMethodsSuccessAction,
1818
SetAuthCookieStatus,
1919
SetRedirectUrlAction,
20+
SetRedirectUrlAndNavigateAction,
2021
} from './auth.actions';
2122
import { AuthMethod } from './models/auth.method';
2223
import { AuthMethodType } from './models/auth.method-type';
@@ -267,6 +268,11 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
267268
redirectUrl: (action as SetRedirectUrlAction).payload,
268269
});
269270

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

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
ResetAuthenticationMessagesAction,
6565
SetAuthCookieStatus,
6666
SetRedirectUrlAction,
67+
SetRedirectUrlAndNavigateAction,
6768
SetUserAsIdleAction,
6869
UnsetUserAsIdleAction,
6970
} from './auth.actions';
@@ -556,15 +557,19 @@ export class AuthService {
556557
/**
557558
* Set redirect url
558559
*/
559-
setRedirectUrl(url: string) {
560+
setRedirectUrl(redirectUrl: string, navigateUrl?: string) {
560561
// Add 1 hour to the current date
561562
const expireDate = Date.now() + (1000 * 60 * 60);
562563

563564
// Set the cookie expire date
564565
const expires = new Date(expireDate);
565-
const options: CookieAttributes = { expires: expires };
566-
this.storage.set(REDIRECT_COOKIE, url, options);
567-
this.store.dispatch(new SetRedirectUrlAction(isNotUndefined(url) ? url : ''));
566+
const options: Cookies.CookieAttributes = { expires: expires };
567+
this.storage.set(REDIRECT_COOKIE, redirectUrl, options);
568+
if (hasValue(navigateUrl)) {
569+
this.store.dispatch(new SetRedirectUrlAndNavigateAction(isNotUndefined(redirectUrl) ? redirectUrl : '', navigateUrl));
570+
} else {
571+
this.store.dispatch(new SetRedirectUrlAction(isNotUndefined(redirectUrl) ? redirectUrl : ''));
572+
}
568573
}
569574

570575
/**

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ describe('Core Module - RxJS Operators', () => {
277277
expectObservable(source.pipe(redirectOn4xx(router, authService))).toBe(expected, values);
278278
flush();
279279
expect(authService.setRedirectUrl).toHaveBeenCalled();
280-
expect(router.navigateByUrl).toHaveBeenCalledWith('login');
281280
});
282281
});
283282

@@ -291,7 +290,6 @@ describe('Core Module - RxJS Operators', () => {
291290
expectObservable(source.pipe(redirectOn4xx(router, authService))).toBe(expected, values);
292291
flush();
293292
expect(authService.setRedirectUrl).toHaveBeenCalled();
294-
expect(router.navigateByUrl).toHaveBeenCalledWith('login');
295293
});
296294
});
297295
});

0 commit comments

Comments
 (0)