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, CheckAuthenticationTokenCookieAction,
LogOutErrorAction, LogOutErrorAction,
LogOutSuccessAction, LogOutSuccessAction,
RefreshTokenAction,
RefreshTokenErrorAction, RefreshTokenErrorAction,
RefreshTokenSuccessAction, RetrieveAuthMethodsAction, RetrieveAuthMethodsErrorAction, RetrieveAuthMethodsSuccessAction RefreshTokenSuccessAction,
RetrieveAuthMethodsAction,
RetrieveAuthMethodsErrorAction,
RetrieveAuthMethodsSuccessAction
} from './auth.actions'; } from './auth.actions';
import { authMethodsMock, AuthServiceStub } from '../../shared/testing/auth-service-stub'; import { authMethodsMock, AuthServiceStub } from '../../shared/testing/auth-service-stub';
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
@@ -157,16 +161,15 @@ describe('AuthEffects', () => {
describe('checkTokenCookie$', () => { describe('checkTokenCookie$', () => {
describe('when check token succeeded', () => { 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( spyOn((authEffects as any).authService, 'checkAuthenticationCookie').and.returnValue(
observableOf( observableOf(
{ authenticated: true, { authenticated: true
token
}) })
); );
actions = hot('--a-', {a: {type: AuthActionTypes.CHECK_AUTHENTICATION_TOKEN_COOKIE}}); 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); expect(authEffects.checkTokenCookie$).toBeObservable(expected);
}); });
@@ -253,7 +256,6 @@ describe('AuthEffects', () => {
describe('when retrieve authentication methods succeeded', () => { describe('when retrieve authentication methods succeeded', () => {
it('should return a RETRIEVE_AUTH_METHODS_SUCCESS action in response to a RETRIEVE_AUTH_METHODS action', () => { 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}}); actions = hot('--a-', {a: {type: AuthActionTypes.RETRIEVE_AUTH_METHODS}});
const expected = cold('--b-', {b: new RetrieveAuthMethodsSuccessAction(authMethodsMock)}); const expected = cold('--b-', {b: new RetrieveAuthMethodsSuccessAction(authMethodsMock)});
@@ -264,7 +266,7 @@ describe('AuthEffects', () => {
describe('when retrieve authentication methods failed', () => { describe('when retrieve authentication methods failed', () => {
it('should return a RETRIEVE_AUTH_METHODS_ERROR action in response to a RETRIEVE_AUTH_METHODS action', () => { 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}}); 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 { catchError, debounceTime, filter, map, switchMap, take, tap } from 'rxjs/operators';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
// import @ngrx // import @ngrx
import { Actions, Effect, ofType } from '@ngrx/effects'; import { Actions, Effect, ofType } from '@ngrx/effects';
import { Action, select, Store } from '@ngrx/store'; import { Action, select, Store } from '@ngrx/store';
// import services // import services
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
// import actions // import actions
@@ -99,8 +97,7 @@ export class AuthEffects {
return this.authService.checkAuthenticationCookie().pipe( return this.authService.checkAuthenticationCookie().pipe(
map((response: AuthStatus) => { map((response: AuthStatus) => {
if (response.authenticated) { if (response.authenticated) {
this.authService.storeToken(response.token); return new RefreshTokenAction(null);
return new AuthenticatedAction(response.token);
} else { } else {
return new RetrieveAuthMethodsAction(response); return new RetrieveAuthMethodsAction(response);
} }

View File

@@ -208,7 +208,9 @@ export class AuthService {
const options: HttpOptions = Object.create({}); const options: HttpOptions = Object.create({});
let headers = new HttpHeaders(); let headers = new HttpHeaders();
headers = headers.append('Accept', 'application/json'); 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; options.headers = headers;
return this.authRequestService.postToEndpoint('login', {}, options).pipe( return this.authRequestService.postToEndpoint('login', {}, options).pipe(
map((status: AuthStatus) => { map((status: AuthStatus) => {

View File

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