mirror of
https://github.com/empayre/fleet.git
synced 2024-11-07 09:18:59 +00:00
42 lines
1.0 KiB
JavaScript
42 lines
1.0 KiB
JavaScript
/* eslint-disable no-unused-vars */
|
|
import { get } from 'lodash';
|
|
import { push } from 'react-router-redux';
|
|
|
|
import APP_CONSTANTS from 'app_constants';
|
|
import kolide from 'kolide';
|
|
import { LOGIN_FAILURE, LOGIN_SUCCESS, LOGOUT_SUCCESS, logoutSuccess } from 'redux/nodes/auth/actions';
|
|
import local from 'utilities/local';
|
|
|
|
const { HTTP_STATUS, PATHS } = APP_CONSTANTS;
|
|
|
|
const authMiddleware = store => next => (action) => {
|
|
const { type, payload } = action;
|
|
|
|
if (type.endsWith('FAILURE')) {
|
|
if (get(payload, 'errors.http_status') === HTTP_STATUS.UNAUTHENTICATED) {
|
|
store.dispatch(logoutSuccess);
|
|
}
|
|
}
|
|
|
|
if (type === LOGIN_SUCCESS) {
|
|
const { token } = payload;
|
|
|
|
if (token) {
|
|
local.setItem('auth_token', token);
|
|
kolide.setBearerToken(token);
|
|
}
|
|
}
|
|
|
|
if (type === LOGOUT_SUCCESS || type === LOGIN_FAILURE) {
|
|
const { LOGIN } = PATHS;
|
|
|
|
local.clear();
|
|
kolide.setBearerToken(null);
|
|
store.dispatch(push(LOGIN));
|
|
}
|
|
|
|
return next(action);
|
|
};
|
|
|
|
export default authMiddleware;
|