Skip to content

Commit 20e934e

Browse files
authored
feat: Move origin checking to runtime (#41)
1 parent a7ad9a6 commit 20e934e

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

playground/nuxt.config.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import NuxtAuth from '..'
22

33
export default defineNuxtConfig({
4-
modules: [NuxtAuth],
5-
auth: {
6-
origin: 'http://localhost:3000'
7-
}
4+
modules: [NuxtAuth]
85
})

src/module.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,10 @@ export default defineNuxtModule<ModuleOptions>({
5656

5757
// 2. Set up runtime configuration
5858
let usedOrigin = moduleOptions.origin
59-
if (typeof usedOrigin === 'undefined') {
59+
const isOriginSet = typeof usedOrigin !== 'undefined'
60+
if (!isOriginSet) {
6061
// TODO: see if we can figure out localhost + port dynamically from the nuxt instance
61-
if (process.env.NODE_ENV === 'production') {
62-
logger.error('You must provide `origin` for production. The origin is the scheme, host and port of your target deployment, e.g., `https://example.org` (port ist 80 implicitly)')
63-
throw new Error('Bad production config - please set `auth.origin`')
64-
} else {
65-
usedOrigin = 'http://localhost:3000'
66-
logger.warn(`\`origin\` not set - an origin is mandatory for production. Using "${usedOrigin}" as a fallback`)
67-
}
62+
usedOrigin = 'http://localhost:3000'
6863
}
6964

7065
const options = defu(moduleOptions, {
@@ -74,11 +69,15 @@ export default defineNuxtModule<ModuleOptions>({
7469
})
7570

7671
const url = joinURL(options.origin, options.basePath)
77-
logger.info(`Using "${url}" as the auth API location, make sure the \`[...].ts\` auth-handler is added there. Use the \`auth.orign\` and \`auth.basePath\` config keys to change the API location`)
72+
logger.info(`Using \`${url}\` as the auth API location, make sure the \`[...].ts\` file with the \`export default NuxtAuthHandler({ ... })\` is added there. Use the \`nuxt.config.ts\` \`auth.origin\` and \`auth.basePath\` config keys to change the API location`)
73+
if (process.env.NODE_ENV === 'production') {
74+
logger.info('When building for production ensure to (1) set the application origin using `auth.origin` inside your `nuxt.config.ts` and (2) set the secret inside the `NuxtAuthHandler({ secret: ... })`')
75+
}
7876

7977
nuxt.options.runtimeConfig.auth = defu(nuxt.options.runtimeConfig.auth, {
8078
...options,
81-
url
79+
url,
80+
isOriginSet
8281
})
8382
nuxt.options.runtimeConfig.public.auth = defu(nuxt.options.runtimeConfig.public.auth, {
8483
url

src/runtime/server/services/nuxtAuthHandler.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,27 @@ const parseActionAndProvider = ({ context }: H3Event): { action: NextAuthAction,
5454

5555
/** Setup the nuxt (next) auth event handler, based on the passed in options */
5656
export const NuxtAuthHandler = (nuxtAuthOptions?: NextAuthOptions) => {
57+
const isProduction = process.env.NODE_ENV === 'production'
58+
5759
usedSecret = nuxtAuthOptions?.secret
5860
if (!usedSecret) {
5961
// eslint-disable-next-line no-console
60-
console.warn('nuxt-auth runtime: No secret supplied - supplying a secret will be necessary for production')
61-
if (process.env.NODE_ENV === 'production') {
62-
throw new Error('Bad production config - please set `secret` inside the `nuxtAuthOptions`')
62+
console.warn('nuxt-auth runtime: No `secret` supplied - supplying a `secret` will be necessary for production. Set the `secret` in the `NuxtAuthHandler` like so: `NuxtAuthHandler({ secret: "your-production-secret" })`')
63+
if (isProduction) {
64+
throw new Error('Bad production config - set `secret` inside the `NuxtAuthHandler` like so: `NuxtAuthHandler({ secret: "your-production-secret" })`')
6365
} else {
6466
usedSecret = 'secret'
6567
}
6668
}
6769

70+
if (!useRuntimeConfig().auth.isOriginSet) {
71+
// eslint-disable-next-line no-console
72+
console.warn('nuxt-auth runtime: No `origin` supplied - supplying an `origin` will be necessary for production. Set the `origin` in your `nuxt.config.ts` like so: `auth: { origin: "https://your-origin.com" }`')
73+
if (isProduction) {
74+
throw new Error('Bad production config - set the application `origin` inside your `nuxt.config.ts` file like so: `auth: { origin: "https://your-cool-website.com" }` ')
75+
}
76+
}
77+
6878
const options = defu(nuxtAuthOptions, {
6979
secret: usedSecret,
7080
logger: undefined,

0 commit comments

Comments
 (0)