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
1313import { Actions , createEffect , ofType } from '@ngrx/effects' ;
1414import { Action , select , Store } from '@ngrx/store' ;
@@ -19,7 +19,7 @@ import { EPerson } from '../eperson/models/eperson.model';
1919import { AuthStatus } from './models/auth-status.model' ;
2020import { AuthTokenInfo } from './models/auth-token-info.model' ;
2121import { AppState } from '../../app.reducer' ;
22- import { isAuthenticated , isAuthenticatedLoaded } from './selectors' ;
22+ import { getAuthenticatedUser , isAuthenticated , isAuthenticatedLoaded } from './selectors' ;
2323import { StoreActionTypes } from '../../store.actions' ;
2424import { 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' ;
5757import { Router } from '@angular/router' ;
5858import { environment } from '../../../environments/environment' ;
5959import { 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