From 57a88c59046b7325caaa7bd8d85678a8b4f6f736 Mon Sep 17 00:00:00 2001 From: rubic0n Date: Sat, 6 Feb 2021 22:41:35 -0600 Subject: [PATCH] Avoid redundant OAuth queries when not signed in If you aren't signed in, you don't have an auth token. When you don't have an auth token, React was sending the headers "Authorization: Bearer null" This caused 5 Doorkeeper token lookups using WHERE "oauth_access_tokens"."token" = 'null' on the Explore page (the root of the app when not signed in). --- app/javascript/gabsocial/api.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/app/javascript/gabsocial/api.js b/app/javascript/gabsocial/api.js index 9d07fbaa..19a80fa1 100644 --- a/app/javascript/gabsocial/api.js +++ b/app/javascript/gabsocial/api.js @@ -25,16 +25,20 @@ function setCSRFHeader() { ready(setCSRFHeader); -export default getState => axios.create({ - headers: Object.assign(csrfHeader, getState ? { - 'Authorization': `Bearer ${getState().getIn(['meta', 'access_token'], '')}`, - } : {}), +export default getState => { + const authToken = getState ? getState().getIn(['meta', 'access_token'], '') : null; - transformResponse: [function (data) { - try { - return JSON.parse(data); - } catch (Exception) { - return data; - } - }], -}); + return axios.create({ + headers: Object.assign(csrfHeader, authToken ? { + 'Authorization': `Bearer ${authToken}}`, + } : {}), + + transformResponse: [function (data) { + try { + return JSON.parse(data); + } catch (Exception) { + return data; + } + }], + }); +};