Skip to content

Commit

Permalink
헤더 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ipcgrdn committed Nov 27, 2024
1 parent ac4d408 commit e1a5f24
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/features/main/main-playlist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const MainPlaylist = () => {
<div className="flex justify-end rounded-full drop-shadow-lg bg-[#F1DCDC] hover:bg-pink-200 transition-colors duration-300 dark:bg-[#FFFFFF0D] dark:hover:bg-[#FFFFFF1A] px-4 py-1">
<button
className="text-base font-bold text-black dark:text-white"
onClick={() => router.push("/more/playist")}
onClick={() => router.push("/more/playlist")}
>
더보기
</button>
Expand Down
8 changes: 5 additions & 3 deletions src/lib/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ const createAxiosInstance = (): AxiosInstance => {

instance.interceptors.request.use(
(config) => {
const accessToken = Cookies.get("access_token");
const token = Cookies.get("access_token");

if (accessToken) {
config.headers["Authorization"] = `Bearer ${accessToken}`;
console.log('Access Token:', token);

if (token) {
config.headers.Authorization = `Bearer ${token}`;
}

return config;
Expand Down
40 changes: 15 additions & 25 deletions src/provider/userProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import axios, { AxiosError } from "axios";
import { useRouter } from "next/navigation";
import React, { createContext, useContext, useEffect, useState, useCallback } from "react";
import React, {
createContext,
useContext,
useEffect,
useState,
useCallback,
} from "react";

import { api } from "../lib/axios";
import { useAuth } from "./authProvider";
Expand All @@ -29,7 +35,7 @@ export function UserProvider({ children }: { children: React.ReactNode }) {
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
const { isLoggedIn, setIsLoggedIn } = useAuth();

const router = useRouter();

const fetchUser = useCallback(async () => {
Expand All @@ -39,17 +45,9 @@ export function UserProvider({ children }: { children: React.ReactNode }) {
setUser(data);
} catch (err) {
if (err instanceof AxiosError) {
if (err.response?.status === 401) {
setIsLoggedIn(false);
setUser(null);
}
setError(
new Error(
err.response?.data?.message || "Failed to fetch user data"
)
new Error(err.response?.data?.message || "Failed to fetch user data")
);
} else {
setError(new Error("An unexpected error occurred"));
}
} finally {
setIsLoading(false);
Expand All @@ -62,31 +60,23 @@ export function UserProvider({ children }: { children: React.ReactNode }) {
setIsLoading(false);
return;
}

fetchUser();

const interval = setInterval(() => {
fetchUser();
}, 60 * 60 * 1000);

return () => clearInterval(interval);
}, [isLoggedIn, fetchUser]);
}, [isLoggedIn]);

const logout = useCallback(async () => {
try {
setIsLoading(true);
await axios.post("/api/logout");

setUser(null);
setError(null);
setIsLoggedIn(false);

router.push('/');
router.push("/");
} catch (err) {
if (err instanceof AxiosError) {
setError(new Error(err.response?.data?.message || 'Failed to logout'));
setError(new Error(err.response?.data?.message || "Failed to logout"));
} else {
setError(new Error('An unexpected error occurred during logout'));
setError(new Error("An unexpected error occurred during logout"));
}
} finally {
setIsLoading(false);
Expand All @@ -106,4 +96,4 @@ export function useUser() {
throw new Error("useUser must be used within a UserProvider");
}
return context;
}
}

0 comments on commit e1a5f24

Please sign in to comment.