Skip to content

Commit

Permalink
feat(imports): Add minimal stubs useAuth and useAuthState imports
Browse files Browse the repository at this point in the history
WHne nuxt-auth module is disabled all #auth imports were failing. For mocking and testing this presented a situation which was impossible to handle properly. This commit adds a stub for these imports on the client side when the module is disabled, opening a way to mock the modules properly.
  • Loading branch information
peterbud committed Dec 15, 2023
1 parent 3171771 commit 5aad90a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ export default defineNuxtModule<ModuleOptions>({
// 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')
Expand All @@ -154,18 +153,32 @@ export default defineNuxtModule<ModuleOptions>({
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) => {
Expand Down
47 changes: 47 additions & 0 deletions src/runtime/composables/stub/useAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ref, readonly } from 'vue'
import {
CommonUseAuthStateReturn,
SignOutFunc,
SignInFunc,
// SessionStatus,
GetSessionFunc
} from '../../types'
import { useAuthState } from './useAuthState'

export const useAuth = (): CommonUseAuthStateReturn<null> => {
const {
data,
status,
lastRefreshedAt
} = useAuthState()

const getters = {
status,
data: readonly(data),
lastRefreshedAt: readonly(lastRefreshedAt),
loading: ref(false),
refresh: () => null,
_internal: {
baseURL: ''
}
}

const getSession:GetSessionFunc<any> = async (_) => {
return await data.value
}
const signIn:SignInFunc<unknown, any> = async (_) => {}
const signOut:SignOutFunc = async () => {}

const actions = {
getSession,
signIn,
signOut,
getProviders: () => [],
getCsrfToken: () => ''
}

return {
...getters,
...actions
}
}
15 changes: 15 additions & 0 deletions src/runtime/composables/stub/useAuthState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ref, readonly } from 'vue'
import { CommonUseAuthStateReturn } from '../../types'
import { makeCommonAuthState } from '../commonAuthState'

export const useAuthState = (): CommonUseAuthStateReturn<null> & {
token: ReturnType<typeof ref>
refreshToken: ReturnType<typeof ref>
} => {
return {
...makeCommonAuthState<null>(),
token: readonly(ref('')),
refreshToken: readonly(ref(''))
}
}
export default useAuthState

0 comments on commit 5aad90a

Please sign in to comment.