Skip to content

Commit 5788005

Browse files
authored
feat(#797, #878): set baseURL via environment variables and improve internal url detection (#913)
1 parent 218a846 commit 5788005

File tree

27 files changed

+845
-206
lines changed

27 files changed

+845
-206
lines changed

.github/workflows/ci.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
branches: [main]
88

99
env:
10-
NODE_VER: 22.5
10+
NODE_VER: 22.11
1111
CI: true
1212

1313
jobs:
@@ -37,6 +37,9 @@ jobs:
3737
# Check linting and typing
3838
- run: pnpm lint
3939
- run: pnpm typecheck
40+
41+
# Run unit tests
42+
- run: pnpm test:unit
4043

4144
# Check building
4245
- run: pnpm build
@@ -131,5 +134,5 @@ jobs:
131134
# start prod-app and curl from it
132135
- run: "timeout 60 pnpm start & (sleep 45 && curl --fail localhost:$PORT)"
133136
env:
134-
AUTH_ORIGIN: "http://localhost:3001"
137+
AUTH_ORIGIN: "http://localhost:3001/api/auth"
135138
PORT: 3001

.github/workflows/deploy-docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
workflow_dispatch:
99

1010
env:
11-
NODE_VER: 22.5
11+
NODE_VER: 22.11
1212
CI: true
1313

1414
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages

.github/workflows/pkg.pr.new.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
pull_request:
99

1010
env:
11-
NODE_VER: 22.5
11+
NODE_VER: 22.11
1212

1313
jobs:
1414
build:

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"dev:prepare": "nuxt-module-build build --stub",
3434
"docs:dev": "vitepress dev docs",
3535
"docs:build": "vitepress build docs",
36-
"docs:preview": "vitepress preview docs"
36+
"docs:preview": "vitepress preview docs",
37+
"test:unit": "vitest"
3738
},
3839
"dependencies": {
3940
"@nuxt/kit": "^3.12.4",
@@ -61,6 +62,7 @@
6162
"ts-essentials": "^9.4.2",
6263
"typescript": "^5.5.4",
6364
"vitepress": "^1.3.1",
65+
"vitest": "^1.6.0",
6466
"vue-tsc": "^2.0.29"
6567
},
6668
"packageManager": "pnpm@9.6.0+sha512.38dc6fba8dba35b39340b9700112c2fe1e12f10b17134715a4aa98ccf7bb035e76fd981cf0bb384dfa98f8d6af5481c2bef2f4266a24bfa20c34eb7147ce0b5e"

playground-authjs/nuxt.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default defineNuxtConfig({
88
globalAppMiddleware: {
99
isEnabled: true
1010
},
11-
baseURL: `http://localhost:${process.env.PORT || 3000}`
11+
baseURL: `http://localhost:${process.env.PORT || 3000}/api/auth`
1212
},
1313
routeRules: {
1414
'/with-caching': {

pnpm-lock.yaml

Lines changed: 111 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/module.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ import {
1111
useLogger
1212
} from '@nuxt/kit'
1313
import { defu } from 'defu'
14-
import { joinURL } from 'ufo'
1514
import { genInterface } from 'knitwork'
1615
import type { DeepRequired } from 'ts-essentials'
1716
import type { NuxtModule } from 'nuxt/schema'
18-
import { getOriginAndPathnameFromURL, isProduction } from './runtime/helpers'
17+
import { isProduction } from './runtime/helpers'
1918
import type {
2019
AuthProviders,
2120
ModuleOptions,
@@ -26,6 +25,8 @@ import type {
2625

2726
const topLevelDefaults = {
2827
isEnabled: true,
28+
baseURL: '/api/auth',
29+
disableInternalRouting: false as boolean,
2930
disableServerSideAuth: false,
3031
originEnvKey: 'AUTH_ORIGIN',
3132
sessionRefresh: {
@@ -108,26 +109,16 @@ export default defineNuxtModule<ModuleOptions>({
108109
const logger = useLogger(PACKAGE_NAME)
109110

110111
// 0. Assemble all options
111-
const { origin, pathname = '/api/auth' } = getOriginAndPathnameFromURL(
112-
userOptions.baseURL ?? ''
113-
)
114112

115113
const selectedProvider = userOptions.provider?.type ?? 'authjs'
116114

117-
const options = {
118-
...defu(userOptions, topLevelDefaults, {
119-
computed: {
120-
origin,
121-
pathname,
122-
fullBaseUrl: joinURL(origin ?? '', pathname)
123-
}
124-
}),
115+
const options = defu({
125116
// We use `as` to infer backend types correctly for runtime-usage (everything is set, although for user everything was optional)
126117
provider: defu(
127118
userOptions.provider,
128119
defaultsByBackend[selectedProvider]
129120
) as DeepRequired<AuthProviders>
130-
}
121+
}, userOptions, topLevelDefaults)
131122

132123
// 1. Check if module should be enabled at all
133124
if (!options.isEnabled) {
@@ -137,15 +128,23 @@ export default defineNuxtModule<ModuleOptions>({
137128

138129
logger.info('`nuxt-auth` setup starting')
139130

140-
// 2. Set up runtime configuration
131+
// 2.1. Disable internal routing for `local` provider when not specified otherwise
132+
// https://github.com/sidebase/nuxt-auth/issues/797
133+
if (userOptions.disableInternalRouting === undefined && selectedProvider === 'local') {
134+
options.disableInternalRouting = true
135+
}
136+
137+
// 2.2. Set up runtime configuration
141138
if (!isProduction) {
142-
const authjsAddition
143-
= selectedProvider === 'authjs'
144-
? ', ensure that `NuxtAuthHandler({ ... })` is there, see https://sidebase.io/nuxt-auth/configuration/nuxt-auth-handler'
145-
: ''
146-
logger.info(
147-
`Selected provider: ${selectedProvider}. Auth API location is \`${options.computed.fullBaseUrl}\`${authjsAddition}`
148-
)
139+
const loggerMessages = [
140+
`Selected provider: ${selectedProvider}.`,
141+
`Auth API location is \`${options.baseURL}\`, if you would like to change this, see https://auth.sidebase.io/guide/application-side/configuration#baseurl.`
142+
]
143+
if (selectedProvider === 'authjs') {
144+
loggerMessages.push('Ensure that the `NuxtAuthHandler({ ... })` is there, see https://auth.sidebase.io/guide/authjs/nuxt-auth-handler')
145+
}
146+
147+
logger.info(loggerMessages.join(' '))
149148
}
150149

151150
nuxt.options.runtimeConfig = nuxt.options.runtimeConfig || { public: {} }

0 commit comments

Comments
 (0)