diff --git a/src/app/core/auth/auth.effects.spec.ts b/src/app/core/auth/auth.effects.spec.ts index 8b18a2b5a4..ba67a95ab1 100644 --- a/src/app/core/auth/auth.effects.spec.ts +++ b/src/app/core/auth/auth.effects.spec.ts @@ -17,8 +17,12 @@ import { CheckAuthenticationTokenCookieAction, LogOutErrorAction, LogOutSuccessAction, + RefreshTokenAction, RefreshTokenErrorAction, - RefreshTokenSuccessAction, RetrieveAuthMethodsAction, RetrieveAuthMethodsErrorAction, RetrieveAuthMethodsSuccessAction + RefreshTokenSuccessAction, + RetrieveAuthMethodsAction, + RetrieveAuthMethodsErrorAction, + RetrieveAuthMethodsSuccessAction } from './auth.actions'; import { authMethodsMock, AuthServiceStub } from '../../shared/testing/auth-service-stub'; import { AuthService } from './auth.service'; @@ -157,16 +161,15 @@ describe('AuthEffects', () => { describe('checkTokenCookie$', () => { describe('when check token succeeded', () => { - it('should return a AUTHENTICATED action in response to a CHECK_AUTHENTICATION_TOKEN_COOKIE action when authenticated is true', () => { + it('should return a REFRESH_TOKEN action in response to a CHECK_AUTHENTICATION_TOKEN_COOKIE action when authenticated is true', () => { spyOn((authEffects as any).authService, 'checkAuthenticationCookie').and.returnValue( observableOf( - { authenticated: true, - token + { authenticated: true }) ); actions = hot('--a-', {a: {type: AuthActionTypes.CHECK_AUTHENTICATION_TOKEN_COOKIE}}); - const expected = cold('--b-', {b: new AuthenticatedAction(token)}); + const expected = cold('--b-', {b: new RefreshTokenAction(null)}); expect(authEffects.checkTokenCookie$).toBeObservable(expected); }); @@ -253,7 +256,6 @@ describe('AuthEffects', () => { describe('when retrieve authentication methods succeeded', () => { it('should return a RETRIEVE_AUTH_METHODS_SUCCESS action in response to a RETRIEVE_AUTH_METHODS action', () => { - actions = hot('--a-', {a: {type: AuthActionTypes.RETRIEVE_AUTH_METHODS}}); const expected = cold('--b-', {b: new RetrieveAuthMethodsSuccessAction(authMethodsMock)}); @@ -264,7 +266,7 @@ describe('AuthEffects', () => { describe('when retrieve authentication methods failed', () => { it('should return a RETRIEVE_AUTH_METHODS_ERROR action in response to a RETRIEVE_AUTH_METHODS action', () => { - spyOn((authEffects as any).authService, 'retrieveAuthMethods').and.returnValue(observableThrow('')); + spyOn((authEffects as any).authService, 'retrieveAuthMethodsFromAuthStatus').and.returnValue(observableThrow('')); actions = hot('--a-', {a: {type: AuthActionTypes.RETRIEVE_AUTH_METHODS}}); diff --git a/src/app/core/auth/auth.effects.ts b/src/app/core/auth/auth.effects.ts index 9145204193..9e860326c9 100644 --- a/src/app/core/auth/auth.effects.ts +++ b/src/app/core/auth/auth.effects.ts @@ -2,11 +2,9 @@ import { Observable, of as observableOf } from 'rxjs'; import { catchError, debounceTime, filter, map, switchMap, take, tap } from 'rxjs/operators'; import { Injectable } from '@angular/core'; - // import @ngrx import { Actions, Effect, ofType } from '@ngrx/effects'; import { Action, select, Store } from '@ngrx/store'; - // import services import { AuthService } from './auth.service'; // import actions @@ -99,8 +97,7 @@ export class AuthEffects { return this.authService.checkAuthenticationCookie().pipe( map((response: AuthStatus) => { if (response.authenticated) { - this.authService.storeToken(response.token); - return new AuthenticatedAction(response.token); + return new RefreshTokenAction(null); } else { return new RetrieveAuthMethodsAction(response); } diff --git a/src/app/core/auth/auth.service.ts b/src/app/core/auth/auth.service.ts index 0978cc3084..0ecf7a69b2 100644 --- a/src/app/core/auth/auth.service.ts +++ b/src/app/core/auth/auth.service.ts @@ -208,7 +208,9 @@ export class AuthService { const options: HttpOptions = Object.create({}); let headers = new HttpHeaders(); headers = headers.append('Accept', 'application/json'); - headers = headers.append('Authorization', `Bearer ${token.accessToken}`); + if (token && token.accessToken) { + headers = headers.append('Authorization', `Bearer ${token.accessToken}`); + } options.headers = headers; return this.authRequestService.postToEndpoint('login', {}, options).pipe( map((status: AuthStatus) => { diff --git a/src/app/shared/testing/auth-service-stub.ts b/src/app/shared/testing/auth-service-stub.ts index 5c9d1dca1e..1fcd714613 100644 --- a/src/app/shared/testing/auth-service-stub.ts +++ b/src/app/shared/testing/auth-service-stub.ts @@ -113,7 +113,7 @@ export class AuthServiceStub { return; } - retrieveAuthMethods(status: AuthStatus) { + retrieveAuthMethodsFromAuthStatus(status: AuthStatus) { return observableOf(authMethodsMock); } }