Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(imports): Add minimal stubs useAuth and useAuthState imports #611

Closed
wants to merge 9 commits into from
39 changes: 26 additions & 13 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,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 @@ -155,18 +154,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')
}
])
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you need to do a

if (!options.isEnabled) {
  return
}

before step 6?

Otherwise you end up adding runtime plugins and middleware, is this intended?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at line 157 pls.


// 5. Create virtual imports for server-side
nuxt.hook('nitro:config', (nitroConfig) => {
Expand Down
46 changes: 46 additions & 0 deletions src/runtime/composables/stub/useAuth.ts
Original file line number Diff line number Diff line change
@@ -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<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 type { 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