Skip to content

Commit 53074df

Browse files
committed
doc: add runtimeconfig
1 parent 59271e3 commit 53074df

File tree

1 file changed

+169
-1
lines changed

1 file changed

+169
-1
lines changed

docs/user-guide/configuration.md

Lines changed: 169 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,124 @@ export default defineNuxtConfig({
8787
})
8888
```
8989

90+
## Runtime Configuration Pattern
91+
92+
As an alternative to top-level `nuxtUsers` configuration, you can use Nuxt's `runtimeConfig` pattern. This approach works for both your Nuxt app runtime and CLI commands (like `npx nuxt-users migrate`).
93+
94+
### Why Use Runtime Config?
95+
96+
- **Environment Variables**: Easier integration with environment variables
97+
- **Security**: Server-side configuration is not exposed to the client
98+
- **Deployment**: Better suited for production deployments
99+
- **CLI Compatibility**: Works seamlessly with CLI commands
100+
101+
### Basic Runtime Config Setup
102+
103+
```ts
104+
// nuxt.config.ts
105+
export default defineNuxtConfig({
106+
modules: ['nuxt-users'],
107+
108+
runtimeConfig: {
109+
nuxtUsers: {
110+
connector: {
111+
name: 'mysql',
112+
options: {
113+
host: process.env.NUXT_MYSQL_HOST,
114+
port: 3306,
115+
user: process.env.NUXT_MYSQL_USER,
116+
password: process.env.NUXT_MYSQL_PASSWORD,
117+
database: process.env.NUXT_MYSQL_DATABASE
118+
}
119+
},
120+
mailer: {
121+
host: process.env.NUXT_MAILER_HOST,
122+
port: Number(process.env.NUXT_MAILER_PORT),
123+
secure: false,
124+
auth: {
125+
user: process.env.NUXT_MAILER_USER,
126+
pass: process.env.NUXT_MAILER_PASS
127+
},
128+
defaults: {
129+
from: process.env.NUXT_MAILER_FROM
130+
}
131+
}
132+
}
133+
}
134+
})
135+
```
136+
137+
### Environment Variables (.env)
138+
139+
```bash
140+
# Database
141+
NUXT_MYSQL_HOST=localhost
142+
NUXT_MYSQL_USER=myapp_user
143+
NUXT_MYSQL_PASSWORD=secure_password
144+
NUXT_MYSQL_DATABASE=myapp_production
145+
146+
# Email
147+
NUXT_MAILER_HOST=smtp.gmail.com
148+
NUXT_MAILER_PORT=587
149+
NUXT_MAILER_USER=noreply@myapp.com
150+
NUXT_MAILER_PASS=app_specific_password
151+
NUXT_MAILER_FROM="My App <noreply@myapp.com>"
152+
```
153+
154+
### Configuration Precedence
155+
156+
When both configurations are present, the precedence is:
157+
158+
1. **Top-level `nuxtUsers`** (highest priority)
159+
2. **Runtime config `runtimeConfig.nuxtUsers`**
160+
3. **Default values** (lowest priority)
161+
162+
```ts
163+
// Both configurations can coexist
164+
export default defineNuxtConfig({
165+
modules: ['nuxt-users'],
166+
167+
// This takes precedence
168+
nuxtUsers: {
169+
auth: {
170+
tokenExpiration: 60 // This overrides runtime config
171+
}
172+
},
173+
174+
// This is merged with defaults and overridden by top-level
175+
runtimeConfig: {
176+
nuxtUsers: {
177+
connector: {
178+
name: 'mysql',
179+
options: {
180+
host: process.env.NUXT_MYSQL_HOST,
181+
// ... other options
182+
}
183+
},
184+
auth: {
185+
tokenExpiration: 120 // This gets overridden
186+
}
187+
}
188+
}
189+
})
190+
```
191+
192+
### CLI Command Support
193+
194+
**Important**: Both configuration patterns work seamlessly with CLI commands:
195+
196+
```bash
197+
# These commands read your configuration (both patterns)
198+
npx nuxt-users migrate
199+
npx nuxt-users create-user -e admin@example.com -n "Admin" -p secure123
200+
```
201+
202+
The CLI will:
203+
1. First try to load your `nuxt.config.ts` configuration
204+
2. Use top-level `nuxtUsers` if present
205+
3. Otherwise use `runtimeConfig.nuxtUsers` if present
206+
4. Fall back to environment variables if no Nuxt config is found
207+
90208
## Database Configuration
91209

92210
### SQLite (Default)
@@ -452,7 +570,15 @@ nuxtUsers: {
452570

453571
## Environment Variables
454572

455-
For production deployments, use environment variables:
573+
> **Note**: For a more modern approach, consider using the [Runtime Configuration Pattern](#runtime-configuration-pattern) which provides better integration with Nuxt's built-in environment variable handling.
574+
575+
This section covers two scenarios:
576+
1. **Direct environment variable usage** in your `nuxt.config.ts`
577+
2. **CLI fallback behavior** when no Nuxt configuration is found
578+
579+
### Direct Environment Variable Usage
580+
581+
For production deployments, you can use environment variables directly in your configuration:
456582

457583
```ts
458584
// nuxt.config.ts
@@ -529,6 +655,48 @@ PASSWORD_RESET_URL=/auth/reset-password
529655
TOKEN_EXPIRATION=1440
530656
```
531657

658+
### CLI Fallback Environment Variables
659+
660+
When CLI commands (like `npx nuxt-users migrate`) can't find a `nuxt.config.ts` file or when the configuration is invalid, they fall back to reading these specific environment variables:
661+
662+
```bash
663+
# Database Configuration
664+
DB_CONNECTOR=mysql # 'sqlite' | 'mysql' | 'postgresql'
665+
666+
# SQLite options
667+
DB_PATH=./data/users.sqlite3 # Path to SQLite database file
668+
669+
# MySQL/PostgreSQL options
670+
DB_HOST=localhost # Database host
671+
DB_PORT=3306 # Database port (3306 for MySQL, 5432 for PostgreSQL)
672+
DB_USER=myuser # Database username
673+
DB_PASSWORD=mypassword # Database password
674+
DB_NAME=mydatabase # Database name
675+
```
676+
677+
**Example usage:**
678+
679+
```bash
680+
# Run migration with environment variables (useful in Docker/CI)
681+
DB_CONNECTOR=mysql \
682+
DB_HOST=localhost \
683+
DB_USER=myapp \
684+
DB_PASSWORD=secret123 \
685+
DB_NAME=production \
686+
npx nuxt-users migrate
687+
688+
# Or with SQLite
689+
DB_CONNECTOR=sqlite \
690+
DB_PATH=./production.sqlite3 \
691+
npx nuxt-users migrate
692+
```
693+
694+
**When CLI fallback is used:**
695+
- No `nuxt.config.ts` file exists
696+
- `nuxt.config.ts` file is malformed/invalid
697+
- Neither `nuxtUsers` nor `runtimeConfig.nuxtUsers` configuration is found
698+
- CLI is run outside a Nuxt project
699+
532700
## Configuration Validation
533701

534702
The module validates your configuration and will show helpful error messages if something is wrong:

0 commit comments

Comments
 (0)