From 351996347c5fb710c56190bbb4a244cf715f3ffa Mon Sep 17 00:00:00 2001 From: manchenkoff Date: Tue, 14 Nov 2023 00:13:24 +0100 Subject: [PATCH] fixed nuxt instance retrieval after async call --- src/runtime/composables/useSanctumAuth.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/runtime/composables/useSanctumAuth.ts b/src/runtime/composables/useSanctumAuth.ts index 9d9e3de..1f32853 100644 --- a/src/runtime/composables/useSanctumAuth.ts +++ b/src/runtime/composables/useSanctumAuth.ts @@ -1,7 +1,7 @@ import { type Ref, computed } from 'vue'; import { useSanctumClient } from './useSanctumClient'; import { useSanctumUser } from './useSanctumUser'; -import { navigateTo, useRoute, useRuntimeConfig } from '#app'; +import { navigateTo, useNuxtApp, useRoute, useRuntimeConfig } from '#app'; import { SanctumModuleOptions } from '~/src/types'; export interface SanctumAuth { @@ -17,6 +17,8 @@ export interface SanctumAuth { * @param T Type of the user object */ export const useSanctumAuth = (): SanctumAuth => { + const nuxtApp = useNuxtApp(); + const user = useSanctumUser(); const client = useSanctumClient(); const options = useRuntimeConfig().public.sanctum as SanctumModuleOptions; @@ -51,12 +53,14 @@ export const useSanctumAuth = (): SanctumAuth => { const requestedRoute = route.query.redirect as string | undefined; if (requestedRoute) { - await navigateTo(requestedRoute); + await nuxtApp.runWithContext(() => navigateTo(requestedRoute)); } } if (options.redirect.onLogin) { - await navigateTo(options.redirect.onLogin); + const redirect = options.redirect.onLogin as string; + + await nuxtApp.runWithContext(() => navigateTo(redirect)); } } @@ -73,7 +77,9 @@ export const useSanctumAuth = (): SanctumAuth => { user.value = null; if (options.redirect.onLogout) { - await navigateTo(options.redirect.onLogout); + const redirect = options.redirect.onLogout as string; + + await nuxtApp.runWithContext(() => navigateTo(redirect)); } }