Files
web-temp/layers/middleware/init.route.global.ts
2025-11-10 14:59:07 +09:00

50 lines
1.6 KiB
TypeScript

import type { GameDataRequest, GameDataValue } from '#layers/types/api/gameData'
export default defineNuxtRouteMiddleware(async (to, _from) => {
// 서버 사이드에서는 스킵
if (import.meta.server) {
return
}
// 현재 경로에서 언어 코드 추출
// 예: /ko/about/story -> ko
// 예: /en/test/page -> en
const gameDataStore = useGameDataStore()
const gameData = gameDataStore.gameData as GameDataValue
const langCodes = gameData?.lang_codes
const currentLangCode = csrGetFinalLocale(to.path, langCodes)
// const languagePattern = /^\/([a-z]{2})(?:\/|$)/
// const match = to.path.match(languagePattern)
// const currentLangCode = match ? match[1] : null
console.log("🚀 777777 ~ currentLangCode:", currentLangCode)
//현재 url에서 게임 도메인만 추출
const currentDomain = window.location.hostname
const runtimeConfig = useRuntimeConfig()
const req: GameDataRequest = {
gameDomain: `${currentDomain}`,
langCode: `${currentLangCode}`,
game_alias: '',
lang_code: `${currentLangCode}`,
baseApiUrl: `${runtimeConfig.public.stoveApiUrl}`,
gameId: '',
}
const { getGameDataExternal } = useGetGameDataExternal()
await getGameDataExternal(req)
// 허용된 언어 코드 목록≈≈
const allowedLangCodes = langCodes || []
// 현재 언어가 허용된 언어 목록에 없으면 에러 페이지로 이동
if (currentLangCode && !allowedLangCodes.includes(currentLangCode)) {
throw createError({
statusCode: 404,
statusMessage: 'Language not supported',
})
}
})