Skip to content

Commit 5ae6cce

Browse files
vins01-4scienceFrancescoMolinaro
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-1766 (pull request DSpace#1794)
[DSC-1766] Approved-by: Francesco Molinaro
2 parents b310c73 + 767f989 commit 5ae6cce

2 files changed

Lines changed: 26 additions & 16 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ import { AuthStatus } from './models/auth-status.model';
4242
import { EPersonMock } from '../../shared/testing/eperson.mock';
4343
import { AppState, storeModuleConfig } from '../../app.reducer';
4444
import { StoreActionTypes } from '../../store.actions';
45-
import { isAuthenticated, isAuthenticatedLoaded } from './selectors';
45+
import { getAuthenticatedUser, isAuthenticated, isAuthenticatedLoaded } from './selectors';
4646
import { AuthorizationDataService } from '../data/feature-authorization/authorization-data.service';
4747
import { RouterStub } from '../../shared/testing/router.stub';
48+
import { EPerson } from '../eperson/models/eperson.model';
4849

4950

5051
describe('AuthEffects', () => {
@@ -467,6 +468,8 @@ describe('AuthEffects', () => {
467468

468469
describe('when refresh state, token and redirect action', () => {
469470
it('should return a REFRESH_STATE_TOKEN_AND_REDIRECT_SUCCESS action in response to a REFRESH_STATE_TOKEN_AND_REDIRECT action', (done) => {
471+
472+
store.overrideSelector(getAuthenticatedUser, { id: EPersonMock.id } as EPerson);
470473
spyOn((authEffects as any).authService, 'retrieveAuthenticatedUserById').and.returnValue(observableOf(EPersonMock));
471474

472475
actions = hot('--a-', {
@@ -484,7 +487,9 @@ describe('AuthEffects', () => {
484487
});
485488

486489
describe('when refresh state token failed', () => {
487-
it('should return a REFRESH_STATE_TOKEN_AND_REDIRECT_SUCCESS action in response to a REFRESH_STATE_TOKEN_AND_REDIRECT action', (done) => {
490+
it('should return a REFRESH_STATE_TOKEN_AND_REDIRECT_ERROR action in response to a REFRESH_STATE_TOKEN_AND_REDIRECT action', (done) => {
491+
492+
store.overrideSelector(getAuthenticatedUser, { id: 'mock-id'} as EPerson);
488493
spyOn((authEffects as any).authService, 'retrieveAuthenticatedUserById').and.returnValue(observableThrow(''));
489494

490495
actions = hot('--a-', {

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
queueScheduler,
99
timer
1010
} from 'rxjs';
11-
import { catchError, delay, filter, map, observeOn, switchMap, take, tap } from 'rxjs/operators';
11+
import { catchError, delay, filter, map, observeOn, switchMap, take, tap, withLatestFrom } from 'rxjs/operators';
1212
// import @ngrx
1313
import { Actions, createEffect, ofType } from '@ngrx/effects';
1414
import { Action, select, Store } from '@ngrx/store';
@@ -19,7 +19,7 @@ import { EPerson } from '../eperson/models/eperson.model';
1919
import { AuthStatus } from './models/auth-status.model';
2020
import { AuthTokenInfo } from './models/auth-token-info.model';
2121
import { AppState } from '../../app.reducer';
22-
import { isAuthenticated, isAuthenticatedLoaded } from './selectors';
22+
import { getAuthenticatedUser, isAuthenticated, isAuthenticatedLoaded } from './selectors';
2323
import { StoreActionTypes } from '../../store.actions';
2424
import { AuthMethod } from './models/auth.method';
2525
// import actions
@@ -53,7 +53,7 @@ import {
5353
RetrieveTokenAction,
5454
SetUserAsIdleAction
5555
} from './auth.actions';
56-
import { hasValue } from '../../shared/empty.util';
56+
import { hasValue, isNotNull } from '../../shared/empty.util';
5757
import { Router } from '@angular/router';
5858
import { environment } from '../../../environments/environment';
5959
import { RequestActionTypes } from '../data/request.actions';
@@ -187,8 +187,9 @@ export class AuthEffects {
187187
public refreshToken$: Observable<Action> = createEffect(() => this.actions$.pipe(ofType(AuthActionTypes.REFRESH_TOKEN),
188188
switchMap((action: RefreshTokenAction) => {
189189
return this.authService.refreshAuthenticationToken(action.payload).pipe(
190+
take(1),
190191
map((token: AuthTokenInfo) => new RefreshTokenSuccessAction(token)),
191-
catchError((error) => observableOf(new RefreshTokenErrorAction()))
192+
catchError(_ => observableOf(new RefreshTokenErrorAction()))
192193
);
193194
})
194195
));
@@ -259,7 +260,7 @@ export class AuthEffects {
259260
return this.authService.retrieveAuthMethodsFromAuthStatus(action.payload)
260261
.pipe(
261262
map((authMethodModels: AuthMethod[]) => new RetrieveAuthMethodsSuccessAction(authMethodModels)),
262-
catchError((error) => observableOf(new RetrieveAuthMethodsErrorAction()))
263+
catchError(_ => observableOf(new RetrieveAuthMethodsErrorAction()))
263264
);
264265
})
265266
));
@@ -268,21 +269,25 @@ export class AuthEffects {
268269
.pipe(ofType(AuthActionTypes.REFRESH_TOKEN_AND_REDIRECT),
269270
switchMap((action: RefreshTokenAndRedirectAction) => {
270271
return this.authService.refreshAuthenticationToken(action.payload.token)
271-
.pipe(map((token: AuthTokenInfo) => new RefreshTokenAndRedirectSuccessAction(token, action.payload.redirectUrl)),
272-
catchError((error) => observableOf(new RefreshTokenAndRedirectErrorAction()))
272+
.pipe(
273+
take(1),
274+
map((token: AuthTokenInfo) => new RefreshTokenAndRedirectSuccessAction(token, action.payload.redirectUrl)),
275+
catchError(_ => observableOf(new RefreshTokenAndRedirectErrorAction()))
273276
);
274277
}))
275278
);
276279

280+
277281
public refreshStateTokenRedirect$: Observable<Action> = createEffect(() => this.actions$
278282
.pipe(ofType(AuthActionTypes.REFRESH_EPERSON_AND_TOKEN_REDIRECT),
279-
switchMap((action: RefreshEpersonAndTokenRedirectAction) =>
280-
this.authService.getAuthenticatedUserFromStore()
281-
.pipe(
282-
switchMap(user => this.authService.retrieveAuthenticatedUserById(user.id)),
283-
map(user => new RefreshEpersonAndTokenRedirectSuccessAction(user, action.payload.token, action.payload.redirectUrl)),
284-
catchError((error) => observableOf(new RefreshEpersonAndTokenRedirectErrorAction()))
285-
)
283+
map(({ payload }: RefreshEpersonAndTokenRedirectAction) => payload),
284+
withLatestFrom(this.store.pipe(select(getAuthenticatedUser), filter(isNotNull))),
285+
switchMap(([{ token, redirectUrl }, { id }]) =>
286+
this.authService.retrieveAuthenticatedUserById(id).pipe(
287+
take(1),
288+
map(user => new RefreshEpersonAndTokenRedirectSuccessAction(user, token, redirectUrl)),
289+
catchError(_ => observableOf(new RefreshEpersonAndTokenRedirectErrorAction()))
290+
)
286291
)
287292
)
288293
);

0 commit comments

Comments
 (0)