Skip to content

Commit

Permalink
Merge pull request #21 from BatShu/feature/auth-api
Browse files Browse the repository at this point in the history
[FEAT] add body in readUserApi
  • Loading branch information
HYEOK9 committed Oct 23, 2023
2 parents 0ad2043 + 46a3b54 commit d77ef94
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
25 changes: 17 additions & 8 deletions src/data/util/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,39 @@ const request = async <T>(
};

const API = {
GET: async <T>(endpoint: string, config?: RequestInit) => {
headers: {},

GET: async function <T>(endpoint: string, config?: RequestInit) {
return await request<T>(`${VITE_API_BASE_URL}/${endpoint}`, {
method: "GET",
...config,
headers: { ...this.headers, ...config?.headers },
});
},

POST: async <T>(endpoint: string, config?: RequestInit) =>
await request<T>(`${VITE_API_BASE_URL}/${endpoint}`, {
POST: async function <T>(endpoint: string, config?: RequestInit) {
return await request<T>(`${VITE_API_BASE_URL}/${endpoint}`, {
method: "POST",
...config,
}),
headers: { ...this.headers, ...config?.headers },
});
},

DELETE: async <T>(endpoint: string, config?: RequestInit) =>
DELETE: async function <T>(endpoint: string, config?: RequestInit) {
await request<T>(`${VITE_API_BASE_URL}/${endpoint}`, {
method: "DELETE",
...config,
}),
headers: { ...this.headers, ...config?.headers },
});
},

PATCH: async <T>(endpoint: string, config?: RequestInit) =>
PATCH: async function <T>(endpoint: string, config?: RequestInit) {
await request<T>(`${VITE_API_BASE_URL}/${endpoint}`, {
method: "PATCH",
...config,
}),
headers: { ...this.headers, ...config?.headers },
});
},
};

export { request, API };
7 changes: 6 additions & 1 deletion src/provider/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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]);

Expand Down

0 comments on commit d77ef94

Please sign in to comment.