|
|
|
@@ -0,0 +1,24 @@ |
|
|
|
import i18next from "i18next"; |
|
|
|
import { attachPostRequestListener } from "../../request"; |
|
|
|
import { logoutUser } from "../actions/login/loginActions"; |
|
|
|
import { makeErrorToastMessage } from "../utils/makeToastMessage"; |
|
|
|
|
|
|
|
//Interceptor unique name |
|
|
|
export const authenticationMiddlewareInterceptorName = "AUTHENTICATION_MIDDLEWARE"; |
|
|
|
|
|
|
|
export default ({ dispatch }) => |
|
|
|
(next) => |
|
|
|
(action) => { |
|
|
|
attachPostRequestListener((error) => { |
|
|
|
if (!error.response) { |
|
|
|
return makeErrorToastMessage(i18next.t("apiErrors.somethingWentWrong")); |
|
|
|
} |
|
|
|
if (error.response.status === 401) { |
|
|
|
dispatch(logoutUser()); |
|
|
|
return makeErrorToastMessage(i18next.t("apiErrors.somethingWentWrong")); |
|
|
|
} |
|
|
|
return Promise.reject(error); |
|
|
|
}, authenticationMiddlewareInterceptorName); |
|
|
|
|
|
|
|
next(action); |
|
|
|
}; |