Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* to be authenticated before, but now he has lost his authentication;
* or he has authenticated as a different user. In both cases, we must drop
* from the state all sensitive data, accessible only to specific users. */
if (prevUserV3.handle && prevUserV3.handle !== userV3.handle) {
store.dispatch(actions.direct.dropAll());
store.dispatch(actions.groups.dropGroups());
}
/* Automatic refreshment of auth tokens. */
let time = Number.MAX_VALUE;
if (tctV2) time = decodeToken(tctV2).exp;
if (userV3) time = Math.min(time, userV3.exp);
if (time < Number.MAX_VALUE) {
time = 1000 * (time - window.CONFIG.REAUTH_TIME);
time = Math.max(0, time - Date.now());
logger.log('Reauth scheduled in', time / 1000, 'seconds');
setTimeout(() => authenticate(store), time);
}
});
}
server.use('/community-app-assets/api/logger', checkAuthorizationHeader, (req, res) => {
logger.log(`${req.clientIp} > `, ...req.body.data);
res.end();
});
getFreshToken().then((tctV3) => {
const tctV2 = cookies.get('tcjwt');
logger.log('Authenticated as:', decodeToken(tctV3));
if (!tctV2) logger.error('Failed to fetch API v2 token!');
return ({ tctV2, tctV3 });
}).catch(() => {
logger.warn('Authentication failed!');
return (state, action) => {
if (!state) {
const def = getDefaultState(action);
return init ? _.defaults(init, def) : def;
}
if (!validActions.has(action.type)) return state;
const { error, payload } = action;
const newState = _.clone(state);
const spaceName = payload.spaceName || config.CONTENTFUL.DEFAULT_SPACE_NAME;
const environment = payload.environment || config.CONTENTFUL.DEFAULT_ENVIRONMENT;
const res = _.get(newState, `${spaceName}.${environment}`);
if (error || !res) {
logger.log('CMS-related error');
return state;
}
const st = state[spaceName][environment];
switch (action.type) {
case actions.contentful.getContentDone.toString():
case actions.contentful.queryContentDone.toString():
res.preview = space(st.preview, actions.contentful.cleanState());
res.published = space(st.published, actions.contentful.cleanState());
break;
default:
}
const key = payload.preview ? 'preview' : 'published';
res[key] = space(res[key], action);
return newState;
};