auth.global.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { useAuthStore } from '@/stores/auth'
  2. export default defineNuxtRouteMiddleware(async (to, from) => {
  3. const { $log } = useNuxtApp()
  4. // 로그인 없이 접근 가능한 페이지 배열에 '/roulette' 추가
  5. const tokenPassPages = [
  6. '/',
  7. '/roulette',
  8. '/auth',
  9. ]
  10. //let accountValue = useAuthStore().getAccountRole.charAt(0).toUpperCase()
  11. // 1. 로그인 없이 접근 가능한 페이지 예외 허용 (이미 로그인 상태면 메인으로 이동)
  12. if (tokenPassPages.includes(to.path)) {
  13. $log.debug('로그인/비로그인 허용 페이지 이동 | ' + to.path)
  14. const accessToken = useAuthStore().getAccessToken
  15. if (accessToken && accessToken !== '' && typeof accessToken === 'string' && to.path === '/') {
  16. return navigateTo('/view/event/evtList') // 원하는 메인 페이지로
  17. }
  18. return
  19. }
  20. // 2. 토큰 체크 (모든 페이지)
  21. if (!tokenPassPages.includes(to.path) && !tokenPassPages.some(path => path !== '/' && to.path.startsWith(path + '/'))) {
  22. const accessToken = useAuthStore().getAccessToken
  23. if (!accessToken || accessToken === '' || typeof accessToken !== 'string') {
  24. $log.error('[ 페이지 접근 불가] 인증되지 않은 사용자입니다.')
  25. return navigateTo('/')
  26. }
  27. }
  28. // 3. 서비스 모드 체크
  29. // if (useAuthStore().getServiceMode === 'INACTIVE') {
  30. // if (accountValue === 'S') {
  31. // $log.debug('페이지 이동 | ' + to.path)
  32. // } else {
  33. // return navigateTo('/')
  34. // }
  35. // }else {
  36. // $log.debug('페이지 이동 | ' + to.path)
  37. // }
  38. })