Skip to content

Commit

Permalink
Merge pull request #233 from jpdillingham/develop
Browse files Browse the repository at this point in the history
Refactor handling of session refresh
  • Loading branch information
jpdillingham authored Aug 18, 2018
2 parents ff09d01 + 7053d05 commit a565b3e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
26 changes: 24 additions & 2 deletions web/src/api.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import axios from 'axios';
import { store } from './index';
import { ensureSession } from './components/security/SecurityActions';
import { refreshSession, logout } from './components/security/SecurityActions';

const api = axios.create();

api.interceptors.request.use(config => {
return store.dispatch(ensureSession()).then(() => {
return store.dispatch(() => {
config.headers.Authorization = store.getState().security.session.idToken.jwtToken;

return Promise.resolve(config);
Expand All @@ -17,4 +17,26 @@ api.interceptors.request.use(config => {
}
);

api.interceptors.response.use(config => {
return config;
}, error => {
let request = error.config;
let status = error.response.status;
let refreshToken = store.getState().security.session.refreshToken.token;

if (status === 401 || status === 403) {
if (refreshToken) {
return store.dispatch(refreshSession())
.then(response => {
request.headers.Authorization = store.getState().security.session.idToken.jwtToken;
return axios(request);
}
);
}
else {
store.dispatch(logout());
}
}
});

export default api;
6 changes: 3 additions & 3 deletions web/src/components/exercises/ExercisesActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const addExercise = (exercise) => (dispatch, getState) => {
reject('Unknown POST response code (expected 201, received ' + response.status + ').');
}
}, error => {
reject(error.response.message);
reject(error);
});
});
};
Expand All @@ -68,7 +68,7 @@ export const updateExercise = (exercise) => (dispatch, getState) => {
reject('Unknown PUT response code (expected 200, received ' + response.status + ').');
}
}, error => {
reject(error.response.message);
reject(error);
});
});
};
Expand All @@ -85,7 +85,7 @@ export const deleteExercise = (id) => (dispatch, getState) => {
reject('Unknown DELETE response code (expected 204, received ' + response.status + ').');
}
}, error => {
reject(error.response.message);
reject(error);
});
});
};
8 changes: 4 additions & 4 deletions web/src/components/routines/RoutinesActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const fetchRoutines = () => (dispatch, getState) => {
dispatch(routinesGet(response.data));
resolve(response);
}, error => {
reject(error.response.message);
reject(error);
});
});
};
Expand All @@ -47,7 +47,7 @@ export const addRoutine = (routine) => (dispatch, getState) => {
reject('Unknown POST response code (expected 201, received ' + response.status + ').');
}
}, error => {
reject(error.response.message);
reject(error);
});
});
};
Expand All @@ -64,7 +64,7 @@ export const updateRoutine = (routine) => (dispatch, getState) => {
reject('Unknown PUT response code (expected 200, received ' + response.status + ').');
}
}, error => {
reject(error.response.message);
reject(error);
});
});
};
Expand All @@ -81,7 +81,7 @@ export const deleteRoutine = (id) => (dispatch, getState) => {
reject('Unknown DELETE response code (expected 204, received ' + response.status + ').');
}
}, error => {
reject(error.response.message);
reject(error);
});
});
};
5 changes: 4 additions & 1 deletion web/src/components/security/SecurityActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ export const logout = () => (dispatch, getState) => {
let cognitoUser = getCognitoUser(getState().security.user);

return new Promise((resolve, reject) => {
cognitoUser.signOut();
if (cognitoUser) {
cognitoUser.signOut();
}

dispatch(logoutAction());
resolve();
});
Expand Down
8 changes: 4 additions & 4 deletions web/src/components/workouts/WorkoutsActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const fetchWorkouts = () => (dispatch, getState) => {
dispatch(workoutsGet(response.data));
resolve(response);
}, error => {
reject(error.response.message);
reject(error);
});
});
};
Expand All @@ -47,7 +47,7 @@ export const addWorkout = (workout) => (dispatch, getState) => {
reject("Unknown POST response code (expected 201, received " + response.status + ").");
}
}, error => {
reject(error.response.message);
reject(error);
});
});
};
Expand All @@ -67,7 +67,7 @@ export const updateWorkout = (workout) => (dispatch, getState) => {
reject("API error: Unknown PUT response code (expected 200, received " + response.status + ").");
}
}, error => {
reject(error.response.message);
reject(error);
});
});
};
Expand All @@ -84,7 +84,7 @@ export const deleteWorkout = (id) => (dispatch, getState) => {
reject("Unknown DELETE response code (expected 204, received " + response.status + ").");
}
}, error => {
reject(error.response.message);
reject(error);
});
});
};
9 changes: 4 additions & 5 deletions web/src/components/workouts/history/WorkoutsHistoryActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const fetchWorkoutsHistory = (filters) => (dispatch, getState) => {
dispatch(workoutsHistoryGet(response.data, totalCount));
resolve(response);
}, error => {
reject(error.response.message);
reject(error);
});
});
};
Expand All @@ -70,8 +70,7 @@ export const fetchWorkoutHistory = (id) => (dispatch, getState) => {
dispatch(workoutHistoryGet(response.data));
resolve(response);
}, error => {
console.log(error.response);
reject(error.response.data || error.response.status);
reject(error);
});
});
};
Expand All @@ -90,7 +89,7 @@ export const updateWorkoutHistory = (workout) => (dispatch, getState) => {
dispatch(workoutHistoryPut(response.data));
resolve(response);
}, error => {
reject(error.response.data || error.response.status);
reject(error);
});
});
};
Expand All @@ -107,7 +106,7 @@ export const deleteWorkoutHistory = (id) => (dispatch, getState) => {
reject("Unknown DELETE response code (expected 204, received " + response.status + ").");
}
}, error => {
reject(error.response.message);
reject(error);
});
});
};

0 comments on commit a565b3e

Please sign in to comment.