@@ -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
0 commit comments