From 46a3b54e9e956c677d3a03d509178210133c01ec Mon Sep 17 00:00:00 2001 From: HYEOK9 Date: Sun, 22 Oct 2023 00:40:08 +0900 Subject: [PATCH] [FEAT] set auth Header --- src/data/util/fetcher.ts | 25 +++++++++++++++++-------- src/provider/AuthProvider.tsx | 7 ++++++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/data/util/fetcher.ts b/src/data/util/fetcher.ts index 670cce1..88a759d 100644 --- a/src/data/util/fetcher.ts +++ b/src/data/util/fetcher.ts @@ -29,30 +29,39 @@ const request = async ( }; const API = { - GET: async (endpoint: string, config?: RequestInit) => { + headers: {}, + + GET: async function (endpoint: string, config?: RequestInit) { return await request(`${VITE_API_BASE_URL}/${endpoint}`, { method: "GET", ...config, + headers: { ...this.headers, ...config?.headers }, }); }, - POST: async (endpoint: string, config?: RequestInit) => - await request(`${VITE_API_BASE_URL}/${endpoint}`, { + POST: async function (endpoint: string, config?: RequestInit) { + return await request(`${VITE_API_BASE_URL}/${endpoint}`, { method: "POST", ...config, - }), + headers: { ...this.headers, ...config?.headers }, + }); + }, - DELETE: async (endpoint: string, config?: RequestInit) => + DELETE: async function (endpoint: string, config?: RequestInit) { await request(`${VITE_API_BASE_URL}/${endpoint}`, { method: "DELETE", ...config, - }), + headers: { ...this.headers, ...config?.headers }, + }); + }, - PATCH: async (endpoint: string, config?: RequestInit) => + PATCH: async function (endpoint: string, config?: RequestInit) { await request(`${VITE_API_BASE_URL}/${endpoint}`, { method: "PATCH", ...config, - }), + headers: { ...this.headers, ...config?.headers }, + }); + }, }; export { request, API }; diff --git a/src/provider/AuthProvider.tsx b/src/provider/AuthProvider.tsx index 6772039..cc4889b 100644 --- a/src/provider/AuthProvider.tsx +++ b/src/provider/AuthProvider.tsx @@ -1,3 +1,5 @@ +import { API } from "@/data/util/fetcher"; +import { getAuthHeader } from "@/data/util/header"; import { HOME_PATH, LOGIN_PATH, SIGNUP_PATH } from "@/domain/constants/paths"; import { useAuthStore } from "@/store/authStore"; import { Backdrop, CircularProgress, css } from "@mui/material"; @@ -16,7 +18,10 @@ export const AuthProvider = () => { navigate(HOME_PATH, { replace: true }); return; } - if (user != null) return; + if (user != null) { + (async () => (API.headers = (await getAuthHeader(user)).headers))(); + return; + } navigate(LOGIN_PATH, { replace: true }); }, [user, location, navigate, setUser, init]);