Skip to content

Commit a376a9d

Browse files
committed
finish poc: dynamic imports
1 parent a95e340 commit a376a9d

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

playground/nuxt.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defineNuxtConfig } from 'nuxt/config'
22
import GithubProvider from 'next-auth/providers/github'
3+
import Fusion from 'next-auth/providers/fusionauth'
34
import NuxtAuth from '..'
45

56
export default defineNuxtConfig({
@@ -13,6 +14,10 @@ export default defineNuxtConfig({
1314
GithubProvider({
1415
clientId: 'enter-your-client-id-here',
1516
clientSecret: 'enter-your-client-secret-here'
17+
}),
18+
Fusion({
19+
clientId: 'enter-your-client-id-here',
20+
clientSecret: 'enter-your-client-secret-here'
1621
})
1722
]
1823
}

src/module.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,21 @@ export default defineNuxtModule<ModuleOptions>({
6868

6969
// 2.3. Create virtual imports
7070
// - TODO: add imports for all providers
71-
const providerImports = providerOptions.map(({ id }) => `import ${id} from next-auth/providers/${id}`)
72-
const preamble = `
73-
${providerImports.concat('\n')}
7471

75-
const providers = ${JSON.stringify(providerOptions.map(({ id }) => id))}
72+
// TODO: deduplicate providers
73+
const providerImports = providerOptions.map(({ id }) => `import ${id} from "next-auth/providers/${id}"`)
74+
const providerExports = providerOptions.map(({ id }) => `"${id}": ${id}`)
75+
76+
// TODO: make `\n` OS-independent
77+
const providerModule = `
78+
${providerImports.join('\n')}
79+
80+
export default {
81+
${providerExports.join(', ')}
82+
}
7683
`
77-
console.log('hiiii', preamble)
7884
nuxt.options.nitro.virtual = defu(nuxt.options.nitro.virtual, {
79-
'#sidebase/providers': preamble
85+
'#sidebase/providers': providerModule
8086
})
8187
nuxt.options.nitro.virtual = defu(nuxt.options.nitro.virtual,
8288
{

src/runtime/server/api/auth.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { NextAuthConfig } from '../../../module'
99

1010
// @ts-ignore Virtual import declared in `module.ts` - TODO: make the import discoverable
1111
import nextConfig from '#sidebase/auth'
12+
import bundlesProviders from '#sidebase/providers'
1213

1314
// TODO: Make `NEXTAUTH_URL` configurable
1415
const NEXTAUTH_URL = nextConfig.url ? parseURL(nextConfig.url) : parseURL('http://localhost:3000/api/auth/')
@@ -20,17 +21,17 @@ let loadedNextConfig: NextAuthConfig | undefined
2021
/**
2122
* Generate the next auth config that can be used for handling requests
2223
*/
23-
const getNextConfig = async (): Promise<NextAuthConfig> => {
24+
const getNextConfig = (): NextAuthConfig => {
2425
if (loadedNextConfig) {
2526
return loadedNextConfig
2627
}
2728

28-
const providers = await Promise.all(nextConfig.options.providers.map(async (providerConfig) => {
29-
const provider = await import(`next-auth/providers/${providerConfig.id}`)
29+
const providers = nextConfig.options.providers.map((providerConfig) => {
30+
const provider = bundlesProviders[providerConfig.id]
3031

3132
// Import is exported on .default during SSR, so we need to call `.default.default` here
32-
return provider.default.default(providerConfig.options)
33-
}))
33+
return provider.default(providerConfig.options)
34+
})
3435

3536
const finalConfig = {
3637
...nextConfig,
@@ -120,7 +121,7 @@ const readBodyForNext = async (event: H3Event) => {
120121
* @param event H3Event event to transform into `RequestInternal`
121122
*/
122123
const getInternalNextAuthRequestData = async (event: H3Event): Promise<RequestInternal> => {
123-
const { url } = await getNextConfig()
124+
const { url } = getNextConfig()
124125
const nextRequest: RequestInternal = {
125126
host: url,
126127
body: undefined,
@@ -185,7 +186,7 @@ export const authHandler = async (event: H3Event) => {
185186
}
186187

187188
// 2. Assemble and perform request to the NextAuth.js auth handler
188-
const nextConfig = await getNextConfig()
189+
const nextConfig = getNextConfig()
189190
const nextRequest = await getInternalNextAuthRequestData(event)
190191

191192
const nextResult = await NextAuthHandler({

0 commit comments

Comments
 (0)