Skip to content

Commit 7c23543

Browse files
committed
fix: leverage runtimeConfig to check password
1 parent de890ed commit 7c23543

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ export default defineNuxtConfig({
5353
NUXT_SESSION_PASSWORD=password-with-at-least-32-characters
5454
```
5555

56-
Nuxt Auth Utils can generate one for you when running Nuxt in development the first time when no `NUXT_SESSION_PASSWORD` is set.
56+
Nuxt Auth Utils generates one for you when running Nuxt in development the first time if no `NUXT_SESSION_PASSWORD` is set.
57+
58+
3. That's it! You can now add authentication to your Nuxt app ✨
5759

58-
4. That's it! You can now add authentication to your Nuxt app ✨
5960

6061
## Vue Composables
6162

src/module.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,6 @@ export default defineNuxtModule<ModuleOptions>({
1717
async setup (options, nuxt) {
1818
const resolver = createResolver(import.meta.url)
1919

20-
// Generate the session password
21-
if (nuxt.options.dev && !process.env.NUXT_SESSION_PASSWORD) {
22-
process.env.NUXT_SESSION_PASSWORD = randomUUID().replace(/-/g, '')
23-
// Add it to .env
24-
const envPath = join(nuxt.options.rootDir, '.env')
25-
const envContent = await readFile(envPath, 'utf-8').catch(() => '')
26-
if (!envContent.includes('NUXT_SESSION_PASSWORD')) {
27-
await writeFile(envPath, `${envContent ? envContent + '\n' : envContent}NUXT_SESSION_PASSWORD=${process.env.NUXT_SESSION_PASSWORD}`, 'utf-8')
28-
}
29-
} else if (!nuxt.options._prepare && !process.env.NUXT_SESSION_PASSWORD) {
30-
throw new Error('NUXT_SESSION_PASSWORD environment variable is not set')
31-
}
32-
3320
nuxt.options.alias['#auth-utils'] = resolver.resolve('./runtime/types/index')
3421

3522
// App
@@ -75,11 +62,25 @@ export default defineNuxtModule<ModuleOptions>({
7562
const runtimeConfig = nuxt.options.runtimeConfig
7663
runtimeConfig.session = defu(runtimeConfig.session, {
7764
name: 'nuxt-session',
78-
password: '',
65+
password: process.env.NUXT_SESSION_PASSWORD || '',
7966
cookie: {
8067
sameSite: 'lax'
8168
}
8269
})
70+
71+
// Generate the session password
72+
if (nuxt.options.dev && !runtimeConfig.session.password) {
73+
runtimeConfig.session.password = randomUUID().replace(/-/g, '')
74+
// Add it to .env
75+
const envPath = join(nuxt.options.rootDir, '.env')
76+
const envContent = await readFile(envPath, 'utf-8').catch(() => '')
77+
if (!envContent.includes('NUXT_SESSION_PASSWORD')) {
78+
await writeFile(envPath, `${envContent ? envContent + '\n' : envContent}NUXT_SESSION_PASSWORD=${runtimeConfig.session.password}`, 'utf-8')
79+
}
80+
} else if (!nuxt.options._prepare && !runtimeConfig.session.password) {
81+
throw new Error('NUXT_SESSION_PASSWORD environment variable or runtimeConfig.session.password not set')
82+
}
83+
8384
// OAuth settings
8485
runtimeConfig.oauth = defu(runtimeConfig.oauth, {})
8586
// GitHub OAuth

src/runtime/server/utils/session.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,8 @@ export async function requireUserSession(event: H3Event): Promise<UserSession &
7272
return userSession as UserSession & { user: User }
7373
}
7474

75-
let sessionConfig: SessionConfig
75+
const sessionConfig: SessionConfig = useRuntimeConfig().session
7676

7777
function _useSession (event: H3Event) {
78-
if (!sessionConfig) {
79-
// @ts-ignore
80-
sessionConfig = defu({ password: process.env.NUXT_SESSION_PASSWORD }, useRuntimeConfig(event).session)
81-
}
8278
return useSession<UserSession>(event, sessionConfig)
8379
}

test/basic.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import { describe, it, expect } from 'vitest'
22
import { fileURLToPath } from 'node:url'
33
import { setup, $fetch } from '@nuxt/test-utils'
4+
import { randomUUID } from 'uncrypto'
45

56
describe('ssr', async () => {
67
await setup({
78
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
9+
nuxtConfig: {
10+
runtimeConfig: {
11+
session: {
12+
password: randomUUID()
13+
}
14+
}
15+
}
816
})
917

1018
it('renders the index page', async () => {

0 commit comments

Comments
 (0)