Skip to content

Commit

Permalink
[#206] axios server 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
ylem76 committed Aug 17, 2024
1 parent 6eb527b commit 4eef97d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/app/login/loginAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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) {
Expand Down
37 changes: 20 additions & 17 deletions src/lib/axiosInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions src/lib/axiosServer.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4eef97d

Please sign in to comment.