You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
exportdefaultdefineNuxtConfig({
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
+
exportdefaultdefineNuxtConfig({
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)
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
+
90
208
## Database Configuration
91
209
92
210
### SQLite (Default)
@@ -452,7 +570,15 @@ nuxtUsers: {
452
570
453
571
## Environment Variables
454
572
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:
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:
0 commit comments