diff --git a/src/app/login/loginAction.tsx b/src/app/login/loginAction.tsx index 263ba1f..e576ac6 100644 --- a/src/app/login/loginAction.tsx +++ b/src/app/login/loginAction.tsx @@ -2,7 +2,7 @@ import { cookies } from 'next/headers' import { redirect } from 'next/navigation' -import api from '@/lib/axiosInstance' +import api from '@/lib/axiosServer' import { LoginFormValue } from './components/LoginForm' @@ -15,9 +15,9 @@ export default async function loginAction(data: LoginFormValue) { // 가져온 json token 쿠키 설정 하기 cookies().set('Authorization', accessToken, { secure: true, - httpOnly: true, + // httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000 * 3), - path: '/', + // path: '/', sameSite: 'strict', }) } catch (error: any) { diff --git a/src/lib/axiosInstance.ts b/src/lib/axiosInstance.ts index 09c3d87..190f422 100644 --- a/src/lib/axiosInstance.ts +++ b/src/lib/axiosInstance.ts @@ -6,26 +6,29 @@ const api = axios.create({ 'Content-Type': 'application/json', }, // withCredentials: true, - // timeout: 10000, // 요청 제한 시간 설정 (밀리초) }) -let accessToken: string = '' +api.interceptors.request.use( + config => { + function getCookie(name: string) { + const value = `; ${document.cookie}` + const parts = value.split(`; ${name}=`) -export const setAccessToken = (token: string) => { - accessToken = token -} + if (parts) { + return parts[1].split(';').shift() + } + } -// api.interceptors.request.use( -// config => { -// const token = sessionStorage.getItem('accessToken') -// if (token) { -// config.headers.Authorization = `Bearer ${token}` -// } -// return config -// }, -// error => { -// return Promise.reject(error) -// } -// ) + const token = getCookie('Authorization') + console.log(token) + if (token) { + config.headers.Authorization = `Bearer ${token}` + } + return config + }, + error => { + return Promise.reject(error) + } +) export default api diff --git a/src/lib/axiosServer.ts b/src/lib/axiosServer.ts new file mode 100644 index 0000000..0adf225 --- /dev/null +++ b/src/lib/axiosServer.ts @@ -0,0 +1,29 @@ +'use server' + +import axios from 'axios' +import { cookies } from 'next/headers' + +const api = axios.create({ + baseURL: 'https://sp-taskify-api.vercel.app/7-2', + headers: { + 'Content-Type': 'application/json', + }, + withCredentials: true, +}) + +api.interceptors.request.use( + config => { + console.log('interceptor') + const token = cookies().get('Authorization') + + if (token) { + config.headers.Authorization = `Bearer ${token}` + } + return config + }, + error => { + return Promise.reject(error) + } +) + +export default api