| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- 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: 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) {
- redirectToLogout()
- } else {
- throw error
- }
- }
- })
|