129 lines
2.9 KiB
TypeScript
129 lines
2.9 KiB
TypeScript
// Token validation error codes
|
|
const TOKEN_ERROR_CODE = {
|
|
SUCCESS: 0,
|
|
LOGIN_INFO_ERROR: 40101,
|
|
LOGIN_INFO_INVALID: 40102,
|
|
TOKEN_EXPIRED: 40103,
|
|
TOKEN_INVALID: 40104,
|
|
} as const
|
|
|
|
type TokenErrorCode = (typeof TOKEN_ERROR_CODE)[keyof typeof TOKEN_ERROR_CODE]
|
|
|
|
interface TokenValidationResponse {
|
|
code: number
|
|
}
|
|
|
|
export const useTokenValidation = () => {
|
|
const config = useRuntimeConfig()
|
|
const modalStore = useModalStore()
|
|
const { tm } = useI18n()
|
|
|
|
const apiBaseUrl = config.public.stoveApiUrl
|
|
const customerServiceUrl = 'https://www.google.com' // TODO: 고객센터 링크로 변경
|
|
const isTokenValid = ref(false)
|
|
|
|
// 로그인 모달 표시
|
|
const showLoginModal = (alertKey: string) => {
|
|
modalStore.handleOpenConfirm({
|
|
contentText: tm(alertKey),
|
|
confirmButtonText: tm('Text_StoveLogin'),
|
|
confirmButtonEvent: () => {
|
|
csrGoStoveLogin()
|
|
},
|
|
})
|
|
}
|
|
|
|
// 에러 모달 표시
|
|
const showErrorModal = () => {
|
|
modalStore.handleOpenConfirm({
|
|
contentText: tm('Alert_Error'),
|
|
confirmButtonText: tm('Text_StoveLogin'),
|
|
confirmButtonEvent: () => {
|
|
window.open(customerServiceUrl, '_blank')
|
|
},
|
|
})
|
|
}
|
|
|
|
// 토큰 검증 API 호출
|
|
const validateToken = async (
|
|
token: string
|
|
): Promise<TokenErrorCode | false> => {
|
|
if (!token) {
|
|
return false
|
|
}
|
|
|
|
try {
|
|
const result = (await commonFetch(
|
|
'GET',
|
|
`${apiBaseUrl}/auth/v5/user_token/check`,
|
|
{
|
|
headers: {
|
|
Authorization: `bearer ${token}`,
|
|
},
|
|
}
|
|
)) as TokenValidationResponse
|
|
|
|
isTokenValid.value = result.code === TOKEN_ERROR_CODE.SUCCESS
|
|
return result.code as TokenErrorCode
|
|
} catch (error) {
|
|
isTokenValid.value = false
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
console.error('[useTokenValidation] API 호출 실패:', error)
|
|
}
|
|
|
|
return false
|
|
}
|
|
}
|
|
|
|
// 토큰 검증 및 에러 처리
|
|
const handleTokenValidation = async (token: string): Promise<boolean> => {
|
|
// 토큰 없음
|
|
if (!token) {
|
|
showLoginModal('Alert_StoveLogin')
|
|
return false
|
|
}
|
|
|
|
const result = await validateToken(token)
|
|
|
|
// API 호출 실패
|
|
if (result === false) {
|
|
showErrorModal()
|
|
return false
|
|
}
|
|
|
|
// 검증 성공
|
|
if (result === TOKEN_ERROR_CODE.SUCCESS) {
|
|
return true
|
|
}
|
|
|
|
// 로그인 정보 오류
|
|
if (
|
|
result === TOKEN_ERROR_CODE.LOGIN_INFO_ERROR ||
|
|
result === TOKEN_ERROR_CODE.LOGIN_INFO_INVALID
|
|
) {
|
|
showLoginModal('Alert_40101')
|
|
return false
|
|
}
|
|
|
|
// 토큰 만료
|
|
if (
|
|
result === TOKEN_ERROR_CODE.TOKEN_EXPIRED ||
|
|
result === TOKEN_ERROR_CODE.TOKEN_INVALID
|
|
) {
|
|
showLoginModal('Alert_40103')
|
|
return false
|
|
}
|
|
|
|
// 기타 에러
|
|
showErrorModal()
|
|
return false
|
|
}
|
|
|
|
return {
|
|
isTokenValid,
|
|
validateToken,
|
|
handleTokenValidation,
|
|
}
|
|
}
|