Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return next => action => {
const nextCheckPostponed = checkNextAction(
next,
postponedRSAAs,
rsaaMiddleware
);
if (isRSAA(action)) {
const state = getState();
const refreshToken = JwtTokenHandlerDesktop.getRefreshToken(state);
// If it is a LOGIN_REQUEST or LOGOUT_REQUEST we don't try to refresh the token
if (
action[RSAA].types.indexOf(authActions.LOGOUT_REQUEST) > -1 ||
action[RSAA].types.indexOf(authActions.LOGIN_REQUEST) > -1
) {
return rsaaMiddleware(next)(action);
}
if (refreshToken && JwtTokenHandlerDesktop.isAccessTokenExpired()) {
postponedRSAAs.push(action);
if (postponedRSAAs.length > 0) {
return rsaaMiddleware(nextCheckPostponed)(
dialBackendApi().refreshAccessToken()
);
return next => action => {
const nextCheckPostponed = checkNextAction(
next,
postponedRSAAs,
rsaaMiddleware
);
if (isRSAA(action)) {
const state = getState();
const refreshToken = JwtTokenHandlerMobile.getRefreshToken(state);
// If it is a LOGIN_REQUEST or LOGOUT_REQUEST we don't try to refresh the token
if (
action[RSAA].types.indexOf(authActions.LOGOUT_REQUEST) > -1 ||
action[RSAA].types.indexOf(authActions.LOGIN_REQUEST) > -1
) {
return rsaaMiddleware(next)(action);
}
if (refreshToken && JwtTokenHandlerMobile.isAccessTokenExpired(state)) {
postponedRSAAs.push(action);
if (postponedRSAAs.length > 0) {
return rsaaMiddleware(nextCheckPostponed)(
dialBackendApi().refreshAccessToken()
);
return next => action => {
const nextCheckPostponed = checkNextAction(
next,
postponedRSAAs,
rsaaMiddleware
);
if (isRSAA(action)) {
const refreshToken = getRefreshToken();
// If it is a LOGIN_REQUEST or LOGOUT_REQUEST we don't try to refresh the token
if (
action[RSAA].types.indexOf(LOGOUT_REQUEST) > -1 ||
action[RSAA].types.indexOf(LOGIN_REQUEST) > -1
) {
return rsaaMiddleware(next)(action);
}
if (refreshToken && isAccessTokenExpired()) {
logMessage("Access token is expired but we have refresh token");
postponedRSAAs.push(action);
logMessage("postponed RSAAs: ", postponedRSAAs);
if (postponedRSAAs.length > 0) {
return rsaaMiddleware(nextCheckPostponed)(refreshAccessToken());
} else {
return (next) => (action) => {
const nextCheckPostoned = (nextAction) => {
// Run postponed actions after token refresh
if (nextAction.type === TOKEN_RECEIVED) {
next(nextAction);
postponedRSAAs.forEach((postponed) => {
rsaaMiddleware(next)(postponed)
})
postponedRSAAs = []
} else {
next(nextAction)
}
}
if(isRSAA(action)) {
const state = getState(),
token = refreshToken(state)
if(token && isAccessTokenExpired(state)) {
postponedRSAAs.push(action)
if(postponedRSAAs.length === 1) {
return rsaaMiddleware(nextCheckPostoned)(refreshAccessToken(token))
} else {
return
}
}
return rsaaMiddleware(next)(action);
}
return next(action);
}
return next => (action) => {
if (!isRSAA(action)) {
return next(action)
}
// abort if not authenticated
if (!store.getState().auth.isAuthenticated) {
return undefined
}
const callApi = action[CALL_API]
// prepend API_ROOT to all endpoints
callApi.endpoint = process.env.API_ROOT + callApi.endpoint
// add Authorization header with token
callApi.headers = { 'Content-Type': 'application/json', Authorization: `Bearer ${AuthService.getToken()}` }
// normalize data on SUCCESS
const type = typeof callApi.types[1] === 'string' ? callApi.types[1] : callApi.types[1].type // 0 is REQUEST, 1 is SUCCESS and 2 is FAILURE
callApi.types[1] = {
type,
export default store => next => action => {
if (!isRSAA(action)) {
return next(action);
}
switch (action[CALL_API].types[0].type) {
case FETCH_ACCOUNTS_REQUEST:
case FETCH_REPOS_REQUEST:
case FETCH_BUILDS_REQUEST:
const type = action[CALL_API].types[0].meta.isPro ? 'pro' : 'os';
const token = 'token ' + store.getState().auth.token[type];
console.log('TOKEN: ', token);
action[CALL_API].headers['Authorization'] = token;
break;
}
return next(action);
};
export function getApiActionTypes(apiAction) {
const callApiAction = unwrapAction(apiAction);
invariant(isRSAA(callApiAction), 'Not a valid api action');
return callApiAction[RSAA].types.map(actionType => typeof actionType === 'string' ? actionType : actionType.type);
}
export const networkRequestMiddleware = setPriority(store => next => (action) => {
if (isRSAA(action)) {
const state = store.getState();
if (!legacyApiDomain) {
const appSettings = getExtensionSettings(state, APPLICATION_EXTENSION);
const { legacyApiEndpoint } = appSettings;
legacyApiDomain = legacyApiEndpoint && new URI(legacyApiEndpoint).domain();
}
const endpointDomain = new URI(action[RSAA].endpoint).domain();
if (legacyApiDomain === endpointDomain && !_.has(action[RSAA], AUTH_HEADERS)) {
_.set(action[RSAA], AUTH_HEADERS, getAuthHeader(state));
}
}
export function getApiActionEntity(apiAction) {
const callApiAction = unwrapAction(apiAction);
invariant(isRSAA(callApiAction), 'Not a valid api action');
const actionTypes = callApiAction[RSAA].types;
const successType = actionTypes && actionTypes[1];
return successType && successType.meta && successType.meta.entity;
}