-
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.
Merge pull request #15 from SW-Palindrome/feature/refresh-token
토큰 자동 재발급 기능 구현
- Loading branch information
Showing
4 changed files
with
68 additions
and
42 deletions.
There are no files selected for viewing
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
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,23 +1,52 @@ | ||
const BASE_URL = process.env.REACT_APP_API_URL_DEV; | ||
|
||
export const validateToken = async (token) => { | ||
export const validateToken = async (accessToken) => { | ||
try { | ||
const response = await fetch(`${BASE_URL}/auth/token-info/${token}`); | ||
|
||
if (!accessToken) { | ||
throw Error("Error: No access token"); | ||
} | ||
const response = await fetch(`${BASE_URL}/auth/token-info/${accessToken}`); | ||
if (!response.ok) { | ||
// 상태 코드가 200 OK가 아니면 오류 발생 | ||
throw new Error("Failed to validate token"); | ||
// 상태 코드가 200 OK가 아니면 액세스 토큰이 유효하지 않은 것이므로 갱신시도 | ||
return await refreshToken(accessToken); | ||
} | ||
return true; | ||
} catch (error) { | ||
throw Error(error); | ||
} | ||
}; | ||
|
||
const data = await response.json(); | ||
|
||
if (data.sub !== null && !isNaN(data.sub)) { | ||
return true; // 토큰이 유효한 경우 | ||
} else { | ||
return false; // 토큰이 유효하지 않은 경우 | ||
const refreshToken = async (accessToken) => { | ||
try { | ||
var base64Url = accessToken.split(".")[1]; | ||
var base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/"); | ||
var jsonPayload = decodeURIComponent( | ||
atob(base64) | ||
.split("") | ||
.map(function (c) { | ||
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2); | ||
}) | ||
.join(""), | ||
); | ||
const id = JSON.parse(jsonPayload)["sub"]; | ||
const refreshToken = localStorage.getItem("studitRefreshToken"); | ||
const response = await fetch(`${BASE_URL}/auth/token-refresh`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
user_id: id, | ||
refresh_token: refreshToken, | ||
}), | ||
}); | ||
if (!response.ok) { | ||
throw new Error("Error: Failed to refresh token"); | ||
} | ||
const data = await response.text(); | ||
localStorage.setItem("studitAccessToken", data); | ||
return true; | ||
} catch (error) { | ||
console.error("Error validating token:", error); | ||
throw error; // 오류 처리 | ||
throw Error(error); | ||
} | ||
}; |