Skip to content

Commit

Permalink
Merge pull request #31 from JNU-econovation/Book-Detail-Page
Browse files Browse the repository at this point in the history
feat: 리프레쉬 토큰을 통해 벡인드에 새로운 엑세스 토큰 요청
  • Loading branch information
seunghyeon77 authored Aug 20, 2024
2 parents 7561188 + 464b699 commit cc6732d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
34 changes: 31 additions & 3 deletions src/api/axiosInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import axios from "axios";
axios.defaults.withCredentials = true;

export const axiosInstance = axios.create({
baseURL: "https://b6dc-168-131-35-101.ngrok-free.app",
baseURL: import.meta.env.VITE_BASE_URL,
headers: {
// "withCredentials": true,
"withCredentials": true,
"Access-Control-Allow-Credentials": true,
"ngrok-skip-browser-warning": true,
// "Accept": "application/json, text/plain",
},
mode: "cors"
});
Expand All @@ -21,3 +20,32 @@ axiosInstance.interceptors.request.use((config) => {
return config;
});

axiosInstance.interceptors.response.use(
(response) => response,
async (error) => {
const originalRequest = error.config;

if (error.response?.status === 401 && !originalRequest._retry) {
originalRequest._retry = true;

try {
const refreshToken = localStorage.getItem("refreshToken");
const response = await axiosInstance.post("/members/refresh", { refreshToken });

if (response.status === 200) {
const reAccessToken = response.data.accessToken;
localStorage.setItem("accessToken", reAccessToken);

originalRequest.headers.Authorization = `Bearer ${reAccessToken}`;
return axiosInstance(originalRequest);
}
} catch (err) {
console.error(err);
localStorage.removeItem("accessToken");
localStorage.removeItem("refreshToken");
alert('로그아웃 되었습니다.');
window.location.href = "/login";
}
}
}
);
1 change: 1 addition & 0 deletions src/components/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default function Header() {
const [headerId, setHeaderId] = useState("");
const navigate = useNavigate();
const accessToken = localStorage.getItem("accessToken");

const onClick = () => {
localStorage.removeItem("accessToken");
window.location.reload();
Expand Down
14 changes: 9 additions & 5 deletions src/components/LogIn/LoginForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ export default function LoginForms() {
id,
password,
};
console.log(data)

try {
const response = await axiosInstance.post("/members/login", data);
console.log('백엔드에 로그인 정보 보내기 성공', response.data.response.accessToken)
if (response.data.response.accessToken) {
localStorage.setItem("accessToken", response.data.response.accessToken);
console.log("accessToken 저장 성공");
const access = response.data.response.accessToken;
const refresh = response.data.response.refreshToken;

if (access && refresh) {
localStorage.setItem("accessToken", access);
localStorage.setItem('refreshToken', refresh)
console.log("accessToken 저장 성공", access);
console.log("refreshToken 저장 성공", refresh)
navigator("/");
}
} catch (error) {
Expand Down

0 comments on commit cc6732d

Please sign in to comment.