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 13343ce commit 4d9760f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
16 changes: 3 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"clsx": "^2.1.1",
"cookie": "^1.0.1",
"framer-motion": "^11.9.0",
"js-cookie": "^3.0.5",
"lucide-react": "^0.446.0",
"next": "14.2.13",
"next-themes": "^0.3.0",
Expand Down
12 changes: 11 additions & 1 deletion src/app/api/logout/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cookies } from "next/headers";
import { NextResponse } from "next/server";

export async function POST() {
Expand All @@ -9,4 +10,13 @@ export async function POST() {
},
}
);
}
}

export async function GET() {
const cookieStore = cookies();
const token = cookieStore.get("access_token");

return NextResponse.json({
token: token?.value || null,
});
}
18 changes: 11 additions & 7 deletions src/lib/axios.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import axios, { AxiosInstance } from "axios";
import Cookies from "js-cookie";

const createAxiosInstance = (): AxiosInstance => {
const instance = axios.create({
Expand All @@ -11,13 +10,18 @@ const createAxiosInstance = (): AxiosInstance => {
});

instance.interceptors.request.use(
(config) => {
const token = Cookies.get("access_token");
async (config) => {
try {
const response = await fetch('/api/token')
const data = await response.json()

console.log('Access Token:', token);

if (token) {
config.headers.Authorization = `Bearer ${token}`;
console.log('Token:', data.token);

if (data.token) {
config.headers.Authorization = `Bearer ${data.token}`;
}
} catch (error) {
console.error('토큰 가져오기 실패:', error);
}

return config;
Expand Down

0 comments on commit 4d9760f

Please sign in to comment.