-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Feature - SignUp, SignIn API Connection (#16)
* feat: Sign In API Connection * feat: Sign Up API Connection
- Loading branch information
Showing
6 changed files
with
145 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,63 @@ | ||
import axios from "axios"; | ||
import Cookie from "js-cookie"; | ||
import Cookies from "js-cookie"; | ||
|
||
const { VITE_APP_SERVER_PORT } = import.meta.env; | ||
|
||
const accessToken = Cookies.get('access_token'); | ||
|
||
|
||
const instance = axios.create({ | ||
baseURL: VITE_APP_SERVER_PORT + "/api/v1/", | ||
baseURL: VITE_APP_SERVER_PORT, | ||
|
||
withCredentials: true, | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Authorization": `Bearer ${accessToken}`, | ||
}, | ||
}); | ||
|
||
instance.interceptors.request.use((config) => { | ||
const accessToken = Cookies.get('accessToken'); | ||
if (accessToken) { | ||
config.headers['Authorization'] = `Bearer ${accessToken}`; | ||
config.withCredentials = true; | ||
} | ||
|
||
return config; | ||
}, (error) => { | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
instance.interceptors.response.use( | ||
response => response, | ||
async (error) => { | ||
if (error.response && error.response.status === 401) { | ||
const originalRequest = error.config; | ||
if (!originalRequest._retry) { | ||
originalRequest._retry = true; | ||
|
||
try { | ||
// 토큰 재발급 요청 | ||
const response = await axios.post(`${import.meta.env.VITE_APP_SERVER_URL}/api/v1/auth/reissue/token`, {}, { | ||
withCredentials: true, | ||
}); | ||
|
||
if (response.status === 200) { | ||
const accessToken = Cookies.get('accessToken'); | ||
originalRequest.headers['Authorization'] = `Bearer ${accessToken}`; | ||
return instance(originalRequest); | ||
} | ||
} catch (refreshError) { | ||
return Promise.reject(refreshError); | ||
} | ||
} | ||
} | ||
|
||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
export default instance; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters