| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import type { CookieRef } from '#app'
- import UnauthorizedError from '~/services/error/UnauthorizedError'
- import { useRedirect } from '~/composables/utils/useRedirect'
- import { useRefreshProfile } from '~/composables/data/useRefreshProfile'
- export default defineNuxtPlugin(async () => {
- const route = useRoute()
- if (route.path.match(/\/public\/.+/)) {
- return
- }
- const runtimeConfig = useRuntimeConfig()
- const { redirectToLogout } = useRedirect()
- const bearer: CookieRef<string | null> = useCookie('BEARER') ?? null
- const accessCookieId: CookieRef<string | null> = useCookie('AccessId') ?? null
- const switchCookieId: CookieRef<string | null> =
- useCookie('SwitchAccessId') ?? null
- if (accessCookieId.value === null || Number.isNaN(accessCookieId.value)) {
- if (runtimeConfig.public.env === 'production') {
- redirectToLogout()
- } else {
- console.error('Missing access id')
- }
- return
- }
- const accessId: number = parseInt(accessCookieId.value)
- if (isNaN(accessId)) {
- if (runtimeConfig.public.env === 'production') {
- redirectToLogout()
- } else {
- console.error('Invalid access id')
- }
- return
- }
- let switchId: number | null = parseInt(switchCookieId.value ?? '')
- if (isNaN(switchId)) {
- switchId = null
- }
- const { initiateProfile } = useRefreshProfile()
- try {
- await initiateProfile(accessId, bearer.value ?? '', switchId)
- } catch (error) {
- if (
- error instanceof UnauthorizedError &&
- runtimeConfig.public.env === 'production'
- ) {
- redirectToLogout()
- } else {
- throw error
- }
- }
- })
|