Skip to content

Commit

Permalink
Merge pull request #1716 from glific/fix/renew-call
Browse files Browse the repository at this point in the history
Fixed renew call issues
  • Loading branch information
kurund authored Oct 25, 2021
2 parents 5dc7943 + 33241da commit 3ef07fb
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const validateMediaMethod = (URL: string, attachmentType: string) =>
new Promise((resolve) => {
axios
.get(`${FLOW_EDITOR_API}validate-media?url=${URL}&type=${attachmentType.toLowerCase()}`, {
headers: { Authorization: getAuthSession('access_token') },
headers: { authorization: getAuthSession('access_token') },
})
.then((response: any) => {
resolve(response);
Expand Down
3 changes: 2 additions & 1 deletion src/components/floweditor/FlowEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export const FlowEditor = (props: FlowEditorProps) => {

useEffect(() => {
if (flowId) {
const { fetch, xmlSend } = setAuthHeaders();
const { fetch, xmlSend, xmlOpen } = setAuthHeaders();
const files = loadfiles(() => {
getFreeFlow({ variables: { id: flowId } });
});
Expand All @@ -329,6 +329,7 @@ export const FlowEditor = (props: FlowEditorProps) => {
clearTimeout(timeoutId);
}
XMLHttpRequest.prototype.send = xmlSend;
XMLHttpRequest.prototype.open = xmlOpen;
window.fetch = fetch;
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/config/apolloclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const gqlClient = (history: any) => {
return {
headers: {
...headers,
Authorization: accessToken || '',
authorization: accessToken || '',
},
};
});
Expand Down
2 changes: 1 addition & 1 deletion src/containers/Auth/Logout/Logout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const Logout: React.SFC<LogoutProps> = (props: any) => {
// let's notify the backend when user logs out
const userLogout = () => {
// get the auth token from session
axios.defaults.headers.common.Authorization = getAuthSession('access_token');
axios.defaults.headers.common.authorization = getAuthSession('access_token');
axios.delete(USER_SESSION);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const AddVariables: React.FC<AddVariablesPropTypes> = ({
const glificBase = FLOW_EDITOR_API;
const contactFieldsprefix = '@contact.fields.';
const contactVariablesprefix = '@contact.';
const headers = { Authorization: getAuthSession('access_token') };
const headers = { authorization: getAuthSession('access_token') };

useEffect(() => {
const getVariableOptions = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const getVariableOptions = async (setContactVariables: any) => {

const contactFieldsprefix = '@contact.fields.';
const contactVariablesprefix = '@contact.';
const headers = { Authorization: getAuthSession('access_token') };
const headers = { authorization: getAuthSession('access_token') };
// get fields keys
const fieldsData = await axios.get(`${glificBase}fields`, {
headers,
Expand Down
51 changes: 39 additions & 12 deletions src/services/AuthService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const renewAuthToken = () => {
return new Error('Error');
}
// get the renewal token from session
axios.defaults.headers.common.Authorization = renewalToken;
axios.defaults.headers.common.authorization = renewalToken;

return axios
.post(RENEW_TOKEN)
Expand Down Expand Up @@ -147,7 +147,7 @@ export const getUserSession = (element?: string) => {
export const setAuthHeaders = () => {
// // add authorization header in all calls
let renewTokenCalled = false;
let tokenRenewed = false;
let renewCallInProgress = false;

const { fetch } = window;
window.fetch = (...args) =>
Expand All @@ -169,47 +169,74 @@ export const setAuthHeaders = () => {
// update localstore
setAuthSession(JSON.stringify(authToken.data.data));
renewTokenCalled = false;
tokenRenewed = false;
}
if (parametersCopy[1]) {
parametersCopy[1].headers = {
...parametersCopy[1].headers,
Authorization: getAuthSession('access_token'),
authorization: getAuthSession('access_token'),
};
}
const result = await fetch(...parametersCopy);
return result;
})(args);

const xmlSend = XMLHttpRequest.prototype.send;
const xmlOpen = XMLHttpRequest.prototype.open;

((open) => {
// @ts-ignore
XMLHttpRequest.prototype.open = function authOpen(
method: string,
url: string | URL,
async: boolean,
username?: string | null,
password?: string | null
) {
if (url.toString().endsWith('renew')) {
// @ts-ignore
this.renewGlificCall = true;
}
open.call(this, method, url, async, username, password);
};
})(XMLHttpRequest.prototype.open);

((send) => {
XMLHttpRequest.prototype.send = async function authCheck(body) {
this.addEventListener('loadend', () => {
if (this.status === 401) {
// @ts-ignore
if (this.renewGlificCall) {
renewCallInProgress = false;
} else if (this.status === 401) {
window.location.href = '/logout/user';
}
});
if (checkAuthStatusService()) {
this.setRequestHeader('Authorization', getAuthSession('access_token'));

// @ts-ignore
if (this.renewGlificCall && !renewCallInProgress) {
renewCallInProgress = true;
send.call(this, body);
} else if (renewTokenCalled && !tokenRenewed) {
}
// @ts-ignore
else if (this.renewGlificCall) {
this.abort();
}
// @ts-ignore
else if (checkAuthStatusService()) {
this.setRequestHeader('authorization', getAuthSession('access_token'));
send.call(this, body);
tokenRenewed = true;
} else if (!renewTokenCalled) {
renewTokenCalled = true;
const authToken = await renewAuthToken();
if (authToken.data) {
// update localstore
setAuthSession(JSON.stringify(authToken.data.data));
renewTokenCalled = false;
tokenRenewed = false;
}
this.setRequestHeader('Authorization', getAuthSession('access_token'));
this.setRequestHeader('authorization', getAuthSession('access_token'));
send.call(this, body);
}
};
})(XMLHttpRequest.prototype.send);

return { xmlSend, fetch };
return { xmlSend, fetch, xmlOpen };
};

0 comments on commit 3ef07fb

Please sign in to comment.