From ff1b897fa3691537bd261b061d5ac1e1fb8aacd2 Mon Sep 17 00:00:00 2001 From: Devin Cowan Date: Mon, 27 Nov 2023 15:52:56 -0500 Subject: [PATCH] use fetchwrapper helper --- .../_helpers/{fetch-wrapper.js => fetchWrapper.js} | 11 ++++++----- app/frontend/src/auth.js | 2 +- app/frontend/src/components/SubmitButton.vue | 14 +++----------- app/frontend/src/stores/submissions.js | 12 ++---------- 4 files changed, 12 insertions(+), 27 deletions(-) rename app/frontend/src/_helpers/{fetch-wrapper.js => fetchWrapper.js} (83%) diff --git a/app/frontend/src/_helpers/fetch-wrapper.js b/app/frontend/src/_helpers/fetchWrapper.js similarity index 83% rename from app/frontend/src/_helpers/fetch-wrapper.js rename to app/frontend/src/_helpers/fetchWrapper.js index a5959fbc..6f725532 100644 --- a/app/frontend/src/_helpers/fetch-wrapper.js +++ b/app/frontend/src/_helpers/fetchWrapper.js @@ -17,7 +17,7 @@ function request(method) { requestOptions.headers['Content-Type'] = 'application/json'; requestOptions.body = JSON.stringify(body); } - console.log("url", url, "optoins", requestOptions) + console.log("url", url, "options", requestOptions) return fetch(url, requestOptions).then(handleResponse); } } @@ -26,13 +26,14 @@ function request(method) { function authHeader(url) { // return auth header with jwt if user is logged in and request is to the api url - const { token } = useAuthStore(); - const isLoggedIn = !!token?.token; + const authStore = useAuthStore(); + const jwt = authStore.getToken() + const isLoggedIn = !!jwt; const isApiUrl = url.startsWith(import.meta.env.VITE_APP_API_URL); - alert(`Isapi:${isApiUrl} isloggedin:${isLoggedIn}`) if (isLoggedIn && isApiUrl) { - return { Authorization: `Bearer ${token.token}` }; + return { Authorization: `Bearer ${jwt}` }; } else { + console.log(`Isapi:${isApiUrl} isloggedin:${isLoggedIn}`) return {}; } } diff --git a/app/frontend/src/auth.js b/app/frontend/src/auth.js index 89155447..12509573 100644 --- a/app/frontend/src/auth.js +++ b/app/frontend/src/auth.js @@ -1,6 +1,6 @@ import { ENDPOINTS, APP_URL } from '@/constants' import { useAuthStore } from '@/stores/auth' -// import { fetchWrapper } from '@/_helpers/fetch-wrapper'; +// import { fetchWrapper } from '@/_helpers/fetchWrapper'; // function openLogInDialog(redirectTo) { // this.logInDialog$.next(redirectTo); // } diff --git a/app/frontend/src/components/SubmitButton.vue b/app/frontend/src/components/SubmitButton.vue index 5978b899..7d1315f9 100644 --- a/app/frontend/src/components/SubmitButton.vue +++ b/app/frontend/src/components/SubmitButton.vue @@ -4,15 +4,14 @@ diff --git a/app/frontend/src/stores/submissions.js b/app/frontend/src/stores/submissions.js index 232d7e5a..99faa4a3 100644 --- a/app/frontend/src/stores/submissions.js +++ b/app/frontend/src/stores/submissions.js @@ -1,22 +1,14 @@ import { defineStore } from 'pinia' import { ENDPOINTS } from '../constants' import { ref } from 'vue' -import { useAuthStore } from '@/stores/auth' +import { fetchWrapper } from '@/_helpers/fetchWrapper'; -const authStore = useAuthStore(); -const jwt = authStore.getToken() export const useSubmissionsStore = defineStore('submissions', () => { const submissions = ref([]) async function getSubmissions() { - const submissionResp = await fetch(`${ENDPOINTS.submissions}`, { - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${jwt}`, - } - }) - const submissionsObj = await submissionResp.json() + const submissionsObj = await fetchWrapper.get(`${ENDPOINTS.submissions}`) let submissions = submissionsObj.submissions this.submissions = submissions }