added refresh token when check on authorization cookie is successful

This commit is contained in:
Giuseppe Digilio
2020-01-17 11:09:41 +01:00
parent 49e59c44e1
commit afe70bc546
4 changed files with 14 additions and 13 deletions

View File

@@ -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}});

View File

@@ -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);
}

View File

@@ -208,7 +208,9 @@ export class AuthService {
const options: HttpOptions = Object.create({});
let headers = new HttpHeaders();
headers = headers.append('Accept', 'application/json');
if (token && token.accessToken) {
headers = headers.append('Authorization', `Bearer ${token.accessToken}`);
}
options.headers = headers;
return this.authRequestService.postToEndpoint('login', {}, options).pipe(
map((status: AuthStatus) => {

View File

@@ -113,7 +113,7 @@ export class AuthServiceStub {
return;
}
retrieveAuthMethods(status: AuthStatus) {
retrieveAuthMethodsFromAuthStatus(status: AuthStatus) {
return observableOf(authMethodsMock);
}
}