Skip to content

Commit

Permalink
fixed nuxt instance retrieval after async call
Browse files Browse the repository at this point in the history
  • Loading branch information
manchenkoff committed Nov 13, 2023
1 parent 27c3cd0 commit 3519963
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/runtime/composables/useSanctumAuth.ts
Original file line number Diff line number Diff line change
@@ -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<T> {
Expand All @@ -17,6 +17,8 @@ export interface SanctumAuth<T> {
* @param T Type of the user object
*/
export const useSanctumAuth = <T>(): SanctumAuth<T> => {
const nuxtApp = useNuxtApp();

const user = useSanctumUser<T>();
const client = useSanctumClient();
const options = useRuntimeConfig().public.sanctum as SanctumModuleOptions;
Expand Down Expand Up @@ -51,12 +53,14 @@ export const useSanctumAuth = <T>(): SanctumAuth<T> => {
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));
}
}

Expand All @@ -73,7 +77,9 @@ export const useSanctumAuth = <T>(): SanctumAuth<T> => {
user.value = null;

if (options.redirect.onLogout) {
await navigateTo(options.redirect.onLogout);
const redirect = options.redirect.onLogout as string;

await nuxtApp.runWithContext(() => navigateTo(redirect));
}
}

Expand Down

0 comments on commit 3519963

Please sign in to comment.