|
1 | 1 | import { defineNuxtModule, useLogger, addServerHandler, addImportsDir, createResolver, resolveModule, addTemplate } from '@nuxt/kit' |
2 | 2 | import defu from 'defu' |
3 | 3 | import { NextAuthOptions } from 'next-auth' |
4 | | -import { parseURL } from 'ufo' |
5 | 4 |
|
6 | 5 | export interface NextAuthConfig { |
7 | 6 | /** |
@@ -62,13 +61,23 @@ export default defineNuxtModule<ModuleOptions>({ |
62 | 61 | // 2.1. Get options |
63 | 62 | const nextAuthOptions = options.nextAuth |
64 | 63 |
|
65 | | - // 2.2. Reduce the providers to id, name, type and options: |
66 | | - // - `options` are what was used to instantiate the provider by the user in the `nuxt.config.ts`, we can use them for later re-instantiation in the auth-middleware |
67 | | - // - this allows us to serialize this correctly in step 2.3., as additional non-serializable properties (that we can get back by re-instatiating) are filtered out |
| 64 | + // 2.2. Reduce the providers to id, name, type and options. This allows us to do two things: |
| 65 | + // - the `options` are what was used to instantiate the provider by the user in the `nuxt.config.ts`, we can use them for later re-instantiation in the auth-middleware |
| 66 | + // - serialize the whole config correctly in step 2.3., as additional non-serializable properties (that we can get back by re-instatiating) are filtered out |
68 | 67 | const providerOptions = nextAuthOptions.options.providers.map(({ id, name, type, options }) => ({ id, name, type, options })) |
69 | 68 |
|
70 | 69 | // 2.3. Create virtual imports |
71 | 70 | // - 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')} |
| 74 | +
|
| 75 | + const providers = ${JSON.stringify(providerOptions.map(({ id }) => id))} |
| 76 | + ` |
| 77 | + console.log('hiiii', preamble) |
| 78 | + nuxt.options.nitro.virtual = defu(nuxt.options.nitro.virtual, { |
| 79 | + '#sidebase/providers': preamble |
| 80 | + }) |
72 | 81 | nuxt.options.nitro.virtual = defu(nuxt.options.nitro.virtual, |
73 | 82 | { |
74 | 83 | '#sidebase/auth': `export default ${JSON.stringify({ |
|
0 commit comments