Skip to content

Commit 518a9e8

Browse files
author
FrancescoMauto
committed
[DURACOM-496] edit: ported setRedirectUrl method and setRedirectUrlAndNaviogateAction from uon
1 parent 4e7d07b commit 518a9e8

5 files changed

Lines changed: 40 additions & 5 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'),
@@ -361,6 +362,23 @@ export class SetRedirectUrlAction implements Action {
361362
}
362363
}
363364

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import {
7070
RetrieveAuthMethodsErrorAction,
7171
RetrieveAuthMethodsSuccessAction,
7272
RetrieveTokenAction,
73+
SetRedirectUrlAndNavigateAction,
7374
SetUserAsIdleAction,
7475
} from './auth.actions';
7576
// import services
@@ -160,6 +161,13 @@ export class AuthEffects {
160161
}),
161162
), { dispatch: false });
162163

164+
public redirectAndNavigate$: Observable<Action> = createEffect(() => this.actions$
165+
.pipe(ofType(AuthActionTypes.SET_REDIRECT_URL_AND_NAVIGATE),
166+
tap((action: SetRedirectUrlAndNavigateAction) => this.router.navigate([decodeURIComponent(action.payload.navigateUrl)])),
167+
),
168+
{ dispatch: false },
169+
);
170+
163171
// It means "reacts to this action but don't send another"
164172
public authenticatedError$: Observable<Action> = createEffect(() => this.actions$.pipe(
165173
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
@@ -14,6 +14,7 @@ import {
1414
RetrieveAuthMethodsSuccessAction,
1515
SetAuthCookieStatus,
1616
SetRedirectUrlAction,
17+
SetRedirectUrlAndNavigateAction,
1718
} from './auth.actions';
1819
import { AuthMethod } from './models/auth.method';
1920
import { AuthMethodType } from './models/auth.method-type';
@@ -245,6 +246,11 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
245246
redirectUrl: (action as SetRedirectUrlAction).payload,
246247
});
247248

249+
case AuthActionTypes.SET_REDIRECT_URL_AND_NAVIGATE:
250+
return Object.assign({}, state, {
251+
redirectUrl: (action as SetRedirectUrlAndNavigateAction).payload.redirectUrl,
252+
});
253+
248254
case AuthActionTypes.REDIRECT_AFTER_LOGIN_SUCCESS:
249255
return Object.assign({}, state, {
250256
loading: true,

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import {
6969
ResetAuthenticationMessagesAction,
7070
SetAuthCookieStatus,
7171
SetRedirectUrlAction,
72+
SetRedirectUrlAndNavigateAction,
7273
SetUserAsIdleAction,
7374
UnsetUserAsIdleAction,
7475
} from './auth.actions';
@@ -579,15 +580,19 @@ export class AuthService {
579580
/**
580581
* Set redirect url
581582
*/
582-
setRedirectUrl(url: string) {
583+
setRedirectUrl(redirectUrl: string, navigateUrl?: string) {
583584
// Add 1 hour to the current date
584585
const expireDate = Date.now() + (1000 * 60 * 60);
585586

586587
// Set the cookie expire date
587588
const expires = new Date(expireDate);
588589
const options: Cookies.CookieAttributes = { expires: expires };
589-
this.storage.set(REDIRECT_COOKIE, url, options);
590-
this.store.dispatch(new SetRedirectUrlAction(isNotUndefined(url) ? url : ''));
590+
this.storage.set(REDIRECT_COOKIE, redirectUrl, options);
591+
if (hasValue(navigateUrl)) {
592+
this.store.dispatch(new SetRedirectUrlAndNavigateAction(isNotUndefined(redirectUrl) ? redirectUrl : '', navigateUrl));
593+
} else {
594+
this.store.dispatch(new SetRedirectUrlAction(isNotUndefined(redirectUrl) ? redirectUrl : ''));
595+
}
591596
}
592597

593598
/**

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)