-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 로그인, 회원가입 유효성 검사 - 로그인, 회원가입 api 연동 - 로그인 토큰 관리
- Loading branch information
Showing
10 changed files
with
398 additions
and
77 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 엑세스 토큰을 세션 스토리지에 저장 | ||
export function setAccessToken(token: string): void { | ||
sessionStorage.setItem('accessToken', token); | ||
} | ||
|
||
// 리프레시 토큰을 로컬 스토리지에 저장 | ||
export function setRefreshToken(token: string): void { | ||
localStorage.setItem('refreshToken', token); | ||
} | ||
|
||
// 엑세스 토큰을 세션 스토리지에서 가져오기 | ||
export function getAccessToken(): string | null { | ||
return sessionStorage.getItem('accessToken'); | ||
} | ||
|
||
// 리프레시 토큰을 로컬 스토리지에서 가져오기 | ||
export function getRefreshToken(): string | null { | ||
return localStorage.getItem('refreshToken'); | ||
} | ||
|
||
// 세션 스토리지 및 로컬 스토리지에서 토큰 삭제 | ||
export function clearTokens(): void { | ||
sessionStorage.removeItem('accessToken'); | ||
localStorage.removeItem('refreshToken'); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// src/api/Auth.api.ts | ||
import instance from './Axios'; | ||
import { | ||
getRefreshToken, | ||
setAccessToken, | ||
setRefreshToken, | ||
clearTokens, | ||
} from '../Utils/TokenManager'; | ||
|
||
// 리프레시 토큰을 사용하여 새로운 엑세스 토큰 발급 | ||
export const refreshAccessToken = async (): Promise<void> => { | ||
const refreshToken = getRefreshToken(); | ||
if (!refreshToken) { | ||
throw new Error('No refresh token available'); | ||
} | ||
|
||
try { | ||
const response = await instance.post('/auth/refresh-token', { refreshToken }); | ||
const { accessToken, newRefreshToken } = response.data; | ||
|
||
// 새 엑세스 토큰과 리프레시 토큰 저장 | ||
setAccessToken(accessToken); | ||
if (newRefreshToken) { | ||
setRefreshToken(newRefreshToken); | ||
} | ||
} catch (error) { | ||
clearTokens(); | ||
throw error; | ||
} | ||
}; |
Oops, something went wrong.