diff --git a/src/module.ts b/src/module.ts index f10a35ee..71e9bf18 100644 --- a/src/module.ts +++ b/src/module.ts @@ -132,7 +132,6 @@ export default defineNuxtModule({ // 1. Check if module should be enabled at all if (!options.isEnabled) { logger.info(`Skipping ${PACKAGE_NAME} setup, as module is disabled`) - return } logger.info('`nuxt-auth` setup starting') @@ -155,18 +154,32 @@ export default defineNuxtModule({ const { resolve } = createResolver(import.meta.url) // 4. Add the correct nuxt-auth app composable, for the desired backend - addImports([ - { - name: 'useAuth', - from: resolve(`./runtime/composables/${options.provider.type}/useAuth`) - }, - { - name: 'useAuthState', - from: resolve( - `./runtime/composables/${options.provider.type}/useAuthState` - ) - } - ]) + if (options.isEnabled) { + addImports([ + { + name: 'useAuth', + from: resolve(`./runtime/composables/${options.provider.type}/useAuth`) + }, + { + name: 'useAuthState', + from: resolve( + `./runtime/composables/${options.provider.type}/useAuthState` + ) + } + ]) + } else { + // In case nuxt-auth is disabled, we add stubs for the imports + addImports([ + { + name: 'useAuth', + from: resolve('./runtime/composables/stub/useAuth') + }, + { + name: 'useAuthState', + from: resolve('./runtime/composables/stub/useAuthState') + } + ]) + } // 5. Create virtual imports for server-side nuxt.hook('nitro:config', (nitroConfig) => { diff --git a/src/runtime/composables/stub/useAuth.ts b/src/runtime/composables/stub/useAuth.ts new file mode 100644 index 00000000..97532055 --- /dev/null +++ b/src/runtime/composables/stub/useAuth.ts @@ -0,0 +1,46 @@ +import { ref, readonly } from 'vue' +import type { + CommonUseAuthStateReturn, + SignOutFunc, + SignInFunc, + GetSessionFunc +} from '../../types' +import { useAuthState } from './useAuthState' + +export const useAuth = (): CommonUseAuthStateReturn => { + const { + data, + status, + lastRefreshedAt + } = useAuthState() + + const getters = { + status, + data: readonly(data), + lastRefreshedAt: readonly(lastRefreshedAt), + loading: ref(false), + refresh: () => null, + _internal: { + baseURL: '' + } + } + + const getSession:GetSessionFunc = async (_) => { + return await data.value + } + const signIn:SignInFunc = async (_) => {} + const signOut:SignOutFunc = async () => {} + + const actions = { + getSession, + signIn, + signOut, + getProviders: () => [], + getCsrfToken: () => '' + } + + return { + ...getters, + ...actions + } +} diff --git a/src/runtime/composables/stub/useAuthState.ts b/src/runtime/composables/stub/useAuthState.ts new file mode 100644 index 00000000..2dae8d46 --- /dev/null +++ b/src/runtime/composables/stub/useAuthState.ts @@ -0,0 +1,15 @@ +import { ref, readonly } from 'vue' +import type { CommonUseAuthStateReturn } from '../../types' +import { makeCommonAuthState } from '../commonAuthState' + +export const useAuthState = (): CommonUseAuthStateReturn & { + token: ReturnType + refreshToken: ReturnType +} => { + return { + ...makeCommonAuthState(), + token: readonly(ref('')), + refreshToken: readonly(ref('')) + } +} +export default useAuthState