| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- 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 { 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)) {
- redirectToLogout()
- return
- }
- const accessId: number = parseInt(accessCookieId.value)
- if (isNaN(accessId)) {
- redirectToLogout()
- return
- }
- let switchId = null
- if (switchCookieId.value !== null && typeof switchCookieId.value !== 'undefined') {
- if (!isNaN(switchId)) {
- switchId = parseInt(switchCookieId.value)
- } else {
- console.error('Invalid switch id : ' + switchCookieId.value)
- }
- }
- const { initiateProfile } = useRefreshProfile()
- try {
- await initiateProfile(
- accessId,
- bearer.value ?? '',
- switchId,
- )
- } catch (error) {
- if (error instanceof UnauthorizedError) {
- redirectToLogout()
- } else {
- throw error
- }
- }
- })
|