2019-10-22 07:58:45 +00:00
|
|
|
/* eslint no-param-reassign: 0 */
|
2020-09-14 09:44:26 +00:00
|
|
|
import fromUnixTime from 'date-fns/fromUnixTime';
|
|
|
|
import differenceInDays from 'date-fns/differenceInDays';
|
2020-02-16 11:50:38 +00:00
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
import { frontendURL } from '../../helper/URLHelper';
|
2021-09-07 17:41:01 +00:00
|
|
|
import {
|
|
|
|
ANALYTICS_IDENTITY,
|
|
|
|
ANALYTICS_RESET,
|
|
|
|
CHATWOOT_RESET,
|
|
|
|
CHATWOOT_SET_USER,
|
|
|
|
} from '../../helper/scriptHelpers';
|
2019-10-22 07:58:45 +00:00
|
|
|
|
2020-08-09 12:43:17 +00:00
|
|
|
Cookies.defaults = { sameSite: 'Lax' };
|
|
|
|
|
2019-10-22 07:58:45 +00:00
|
|
|
export const getLoadingStatus = state => state.fetchAPIloadingStatus;
|
|
|
|
export const setLoadingStatus = (state, status) => {
|
|
|
|
state.fetchAPIloadingStatus = status;
|
|
|
|
};
|
2020-02-16 11:50:38 +00:00
|
|
|
|
2021-09-07 17:41:01 +00:00
|
|
|
export const setUser = (user, expiryDate, options = {}) => {
|
|
|
|
if (options && options.setUserInSDK) {
|
|
|
|
window.bus.$emit(CHATWOOT_SET_USER, { user });
|
|
|
|
window.bus.$emit(ANALYTICS_IDENTITY, { user });
|
|
|
|
}
|
|
|
|
Cookies.set('user', user, {
|
2020-09-14 09:44:26 +00:00
|
|
|
expires: differenceInDays(expiryDate, new Date()),
|
2020-02-16 11:50:38 +00:00
|
|
|
});
|
2021-09-07 17:41:01 +00:00
|
|
|
};
|
2020-02-16 11:50:38 +00:00
|
|
|
|
2020-09-14 09:44:26 +00:00
|
|
|
export const getHeaderExpiry = response =>
|
|
|
|
fromUnixTime(response.headers.expiry);
|
2020-02-16 11:50:38 +00:00
|
|
|
|
|
|
|
export const setAuthCredentials = response => {
|
|
|
|
const expiryDate = getHeaderExpiry(response);
|
|
|
|
Cookies.set('auth_data', response.headers, {
|
2020-09-14 09:44:26 +00:00
|
|
|
expires: differenceInDays(expiryDate, new Date()),
|
2020-02-16 11:50:38 +00:00
|
|
|
});
|
|
|
|
setUser(response.data.data, expiryDate);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const clearCookiesOnLogout = () => {
|
2021-09-07 17:41:01 +00:00
|
|
|
window.bus.$emit(CHATWOOT_RESET);
|
|
|
|
window.bus.$emit(ANALYTICS_RESET);
|
|
|
|
|
2020-02-16 11:50:38 +00:00
|
|
|
Cookies.remove('auth_data');
|
|
|
|
Cookies.remove('user');
|
|
|
|
window.location = frontendURL('login');
|
|
|
|
};
|