auth.global.js 1.6 KB

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