Skip to content

Commit

Permalink
Refactor: Modularize cookie retrieval out of 'apiRequest.js'
Browse files Browse the repository at this point in the history
- Move some of the cookie retrieval logic to a separate
helper function named 'getCookie' in 'src/helpers/getCookie.js'.
- Update 'apiRequest.js' to use the new helper function.
  • Loading branch information
ITurres committed Mar 24, 2024
1 parent a4b958e commit 64851a9
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/helpers/apiRequest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import getCookie from './getCookie';

const apiRequest = async (path, method = 'GET', body = {}) => {
const baseUrl = process.env.REACT_APP_TESLA_API_BASE_URL;
const fetchOptions = {
Expand All @@ -11,15 +13,11 @@ const apiRequest = async (path, method = 'GET', body = {}) => {

// ? If the user has signup/login, we have the token, so we can
// ? have access to user's related data.
if (document.cookie.includes('tesla-booking-user-token')) {
const tokenCookie = document.cookie
.split('; ')
.find((cookie) => cookie.startsWith('tesla-booking-user-token='));

if (tokenCookie) {
const tokenValue = tokenCookie.split('=')[1];
fetchOptions.headers.Authorization = tokenValue;
}
const cookieData = getCookie('tesla-booking-user-token');
if (cookieData) {
const tokenValue = cookieData.split('=')[1];

fetchOptions.headers.Authorization = tokenValue;
}

try {
Expand Down

0 comments on commit 64851a9

Please sign in to comment.