Skip to content

Commit 9102628

Browse files
committed
fix: use runtimeConfig and auto-gen dev session password
1 parent a45e707 commit 9102628

6 files changed

Lines changed: 43 additions & 13 deletions

File tree

CONTRIBUTING.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@ We want to create 'a fast, modern browser for the npm registry.' This means, amo
3030
pnpm install
3131
```
3232

33-
3. set `NUXT_SESSION_PASSWORD` in `.env`
34-
35-
4. start the development server:
33+
3. start the development server:
3634

3735
```bash
3836
pnpm dev
3937
```
4038

41-
5. (optional) if you want to test the admin UI/flow, you can run the local connector:
39+
4. (optional) if you want to test the admin UI/flow, you can run the local connector:
4240

4341
```bash
4442
pnpm npmx-connector

modules/dev.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { defineNuxtModule, useNuxt } from 'nuxt/kit'
2+
import { join } from 'node:path'
3+
import { appendFileSync, existsSync, readFileSync } from 'node:fs'
4+
import { randomUUID } from 'node:crypto'
5+
6+
export default defineNuxtModule({
7+
meta: {
8+
name: 'dev',
9+
},
10+
setup() {
11+
const nuxt = useNuxt()
12+
if (nuxt.options._prepare || process.env.NUXT_SESSION_PASSWORD) {
13+
return
14+
}
15+
16+
const envPath = join(nuxt.options.rootDir, '.env')
17+
const hasPassword =
18+
existsSync(envPath) && /^NUXT_SESSION_PASSWORD=/m.test(readFileSync(envPath, 'utf-8'))
19+
20+
if (!hasPassword) {
21+
console.info('Generating NUXT_SESSION_PASSWORD for development environment.')
22+
const password = randomUUID().replace(/-/g, '')
23+
24+
nuxt.options.runtimeConfig.sessionPassword = password
25+
appendFileSync(envPath, `# generated by dev module\nNUXT_SESSION_PASSWORD=${password}\n`)
26+
}
27+
},
28+
})

nuxt.config.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ export default defineNuxtConfig({
4545

4646
css: ['~/assets/main.css', 'vue-data-ui/style.css'],
4747

48-
devtools: { enabled: true },
49-
devServer: {
50-
host: '127.0.0.1',
48+
runtimeConfig: {
49+
sessionPassword: '',
5150
},
5251

52+
devtools: { enabled: true },
53+
5354
app: {
5455
head: {
5556
htmlAttrs: { lang: 'en-US' },

server/api/auth/atproto.get.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { OAuthSessionStore, OAuthStateStore } from '#server/utils/atproto/storag
55
import { SLINGSHOT_ENDPOINT } from '#shared/utils/constants'
66

77
export default defineEventHandler(async event => {
8-
if (!process.env.NUXT_SESSION_PASSWORD) {
8+
const config = useRuntimeConfig(event)
9+
if (!config.sessionPassword) {
910
throw createError({
1011
status: 500,
1112
message: 'NUXT_SESSION_PASSWORD not set',
@@ -47,7 +48,7 @@ export default defineEventHandler(async event => {
4748
event.context.agent = agent
4849

4950
const session = await useSession(event, {
50-
password: process.env.NUXT_SESSION_PASSWORD,
51+
password: config.sessionPassword,
5152
})
5253

5354
const response = await fetch(

server/api/auth/session.delete.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
2-
if (!process.env.NUXT_SESSION_PASSWORD) {
2+
const config = useRuntimeConfig(event)
3+
if (!config.sessionPassword) {
34
throw createError({
45
status: 500,
56
message: 'NUXT_SESSION_PASSWORD not set',
67
})
78
}
89

910
const session = await useSession(event, {
10-
password: process.env.NUXT_SESSION_PASSWORD,
11+
password: config.sessionPassword,
1112
})
1213

1314
await oAuthSession?.signOut()

server/api/auth/session.get.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
export default defineEventHandler(async event => {
2-
if (!process.env.NUXT_SESSION_PASSWORD) {
2+
const config = useRuntimeConfig(event)
3+
if (!config.sessionPassword) {
34
throw createError({
45
status: 500,
56
message: 'NUXT_SESSION_PASSWORD not set',
67
})
78
}
89

910
const session = await useSession(event, {
10-
password: process.env.NUXT_SESSION_PASSWORD,
11+
password: config.sessionPassword,
1112
})
1213

1314
return session.data

0 commit comments

Comments
 (0)