Skip to content

Commit 2bf427f

Browse files
fix(env): support runtime nuxt env variables (#589)
Co-authored-by: Baptiste Leproux <leproux.baptiste@gmail.com>
1 parent 97b4a56 commit 2bf427f

7 files changed

Lines changed: 42 additions & 34 deletions

File tree

demo/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ CREATE TABLE tasks (
4343
Go to your supabase project settings, API section and get the project API key and url and fill the `.env` with them:
4444

4545
```
46-
SUPABASE_URL="https://example.supabase.co"
47-
SUPABASE_KEY="<your_key>"
46+
NUXT_PUBLIC_SUPABASE_URL="https://example.supabase.co"
47+
NUXT_PUBLIC_SUPABASE_KEY="<your_key>"
4848
```
4949

5050
### GitHub Oauth Setup

docs/content/1.getting-started/1.introduction.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ export default defineNuxtConfig({
2727
})
2828
```
2929

30-
Add `SUPABASE_URL` and `SUPABASE_KEY` to the `.env`:
30+
Add `NUXT_PUBLIC_SUPABASE_URL` and `NUXT_PUBLIC_SUPABASE_KEY` to the `.env`:
3131

3232
```bash [env]
33-
SUPABASE_URL="https://example.supabase.co"
34-
SUPABASE_KEY="<your_publishable_key>"
33+
NUXT_PUBLIC_SUPABASE_URL="https://example.supabase.co"
34+
NUXT_PUBLIC_SUPABASE_KEY="<your_publishable_key>"
3535
```
3636

3737
::tip
38-
Alternatively, you can prefix the env variables with `NUXT_PUBLIC_` in order to use runtimeConfig.
38+
The legacy `SUPABASE_URL` and `SUPABASE_KEY` env variables are still supported as fallbacks. Using the `NUXT_` prefix is recommended as it follows Nuxt's [runtimeConfig](https://nuxt.com/docs/guide/going-further/runtime-config) convention and allows overriding values at runtime without rebuilding.
3939
::
4040

4141
## Options
@@ -53,13 +53,13 @@ export default defineNuxtConfig({
5353
5454
### `url`
5555
56-
Default: `process.env.SUPABASE_URL` (ex: <https://example.supabase.co>)
56+
Default: `NUXT_PUBLIC_SUPABASE_URL` env variable (falls back to `SUPABASE_URL`) (ex: <https://example.supabase.co>)
5757
5858
The unique Supabase URL which is supplied when you create a new project in your project dashboard.
5959
6060
### `key`
6161
62-
Default: `process.env.SUPABASE_KEY`
62+
Default: `NUXT_PUBLIC_SUPABASE_KEY` env variable (falls back to `SUPABASE_KEY`)
6363
6464
Supabase `publishable key`, used to verify and decode the JWT. Can bypass the Supabase API gateway and interact with your Supabase database applying RLS Policies.
6565
@@ -69,17 +69,17 @@ In `v1.x.x` and earlier, this was referring to the `anon key`. With the introduc
6969
7070
### `secretKey`
7171
72-
Default: `process.env.SUPABASE_SECRET_KEY`
72+
Default: `NUXT_SUPABASE_SECRET_KEY` env variable (falls back to `SUPABASE_SECRET_KEY`)
7373
7474
Supabase `secret key`, has super admin rights and can bypass your Row Level Security.
7575
7676
::warning
77-
This key should be kept secret and never exposed to the client. Keep it in environment variables.
77+
This key is server-only and never exposed to the client. Keep it in environment variables.
7878
::
7979
8080
### `serviceKey` :u-badge{label="Deprecated" color="warning"}
8181
82-
Default: `process.env.SUPABASE_SERVICE_KEY`
82+
Default: `NUXT_SUPABASE_SERVICE_KEY` env variable (falls back to `SUPABASE_SERVICE_KEY`)
8383
8484
::warning{to="https://supabase.com/blog/jwt-signing-keys"}
8585
*Legacy API key* used before signing JWT keys were introduced. Use `secretKey` instead. This option will be removed in a future version.

docs/content/1.getting-started/4.migration.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ Before migrating your environment variables, you need to enable JWT signing keys
133133
#### 2.b. Use your new keys in your `.env` file
134134

135135
```bash [.env]
136-
SUPABASE_KEY=<your_publishable_key>
137-
SUPABASE_SECRET_KEY=<your_secret_key>
136+
NUXT_PUBLIC_SUPABASE_KEY=<your_publishable_key>
137+
NUXT_SUPABASE_SECRET_KEY=<your_secret_key>
138138
```
139+
140+
::tip
141+
The legacy `SUPABASE_KEY` and `SUPABASE_SECRET_KEY` env variables are still supported as fallbacks. See the [introduction](/getting-started/introduction) for details.
142+
::

docs/content/2.composables/useSupabaseClient.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: Make requests to the Supabase API with the useSupabaseClient compos
77

88
This composable is using [supabase-js](https://github.com/supabase/supabase-js/) under the hood, it gives access to the [Supabase client](https://supabase.com/docs/reference/javascript/initializing).
99

10-
> The client is initialized with the `SUPABASE_KEY` you must have in your `.env` file. It establishes the connection with the database and make use of user JWT to apply [RLS Policies](https://supabase.com/docs/learn/auth-deep-dive/auth-row-level-security) implemented in Supabase. If you want to bypass policies, you can use the [serverSupabaseServiceRole](/services/serversupabaseservicerole).
10+
> The client is initialized with the `NUXT_PUBLIC_SUPABASE_KEY` you must have in your `.env` file. It establishes the connection with the database and make use of user JWT to apply [RLS Policies](https://supabase.com/docs/learn/auth-deep-dive/auth-row-level-security) implemented in Supabase. If you want to bypass policies, you can use the [serverSupabaseServiceRole](/services/serversupabaseservicerole).
1111
1212
## Authentication
1313

docs/content/3.services/2.serverSupabaseServiceRole.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This function is designed to work only in [server routes](https://nuxt.com/docs/
1414

1515
It works similary as the [serverSupabaseClient](/services/serversupabaseclient) but it provides a client with super admin rights that can bypass your [Row Level Security](https://supabase.com/docs/guides/auth/row-level-security).
1616

17-
> The client is initialized with the `SUPABASE_SECRET_KEY` (recommended) or `SUPABASE_SERVICE_KEY` (deprecated) you must have in your `.env` file. We recommend using the new JWT signing keys (`SUPABASE_SECRET_KEY`) as described in the [Supabase blog post](https://supabase.com/blog/jwt-signing-keys).
17+
> The client is initialized with the `NUXT_SUPABASE_SECRET_KEY` you must have in your `.env` file. See the [Supabase blog post](https://supabase.com/blog/jwt-signing-keys) for more on JWT signing keys.
1818
1919
Define your server route and just import the `serverSupabaseServiceRole` from `#supabase/server`.
2020

src/module.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export * from './types'
1313
export interface ModuleOptions {
1414
/**
1515
* Supabase API URL
16-
* @default process.env.SUPABASE_URL
16+
* @default process.env.NUXT_PUBLIC_SUPABASE_URL || process.env.SUPABASE_URL
1717
* @example 'https://*.supabase.co'
1818
* @type string
1919
* @docs https://supabase.com/docs/reference/javascript/initializing#parameters
@@ -22,7 +22,7 @@ export interface ModuleOptions {
2222

2323
/**
2424
* Supabase Client publishable API Key (previously known as 'anon key')
25-
* @default process.env.SUPABASE_KEY
25+
* @default process.env.NUXT_PUBLIC_SUPABASE_KEY || process.env.SUPABASE_KEY
2626
* @example '123456789'
2727
* @type string
2828
* @docs https://supabase.com/docs/reference/javascript/initializing#parameters
@@ -31,7 +31,7 @@ export interface ModuleOptions {
3131

3232
/**
3333
* Supabase Legacy 'service_role' key (deprecated)
34-
* @default process.env.SUPABASE_SERVICE_KEY
34+
* @default process.env.NUXT_SUPABASE_SERVICE_KEY || process.env.SUPABASE_SERVICE_KEY
3535
* @example '123456789'
3636
* @type string
3737
* @docs https://supabase.com/docs/reference/javascript/initializing#parameters
@@ -41,7 +41,7 @@ export interface ModuleOptions {
4141

4242
/**
4343
* Supabase Secret key
44-
* @default process.env.SUPABASE_SECRET_KEY
44+
* @default process.env.NUXT_SUPABASE_SECRET_KEY || process.env.SUPABASE_SECRET_KEY
4545
* @example '123456789'
4646
* @type string
4747
* @docs https://supabase.com/blog/jwt-signing-keys
@@ -129,16 +129,20 @@ export default defineNuxtModule<ModuleOptions>({
129129
},
130130
},
131131
defaults: {
132-
url: (process.env.SUPABASE_URL
133-
?? process.env.NUXT_PUBLIC_SUPABASE_URL) as string,
134-
key: (process.env.SUPABASE_KEY
135-
?? process.env.SUPABASE_PUBLISHABLE_KEY
136-
?? process.env.NUXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY
137-
?? process.env.SUPABASE_ANON_KEY
138-
?? process.env.NUXT_PUBLIC_SUPABASE_ANON_KEY) as string,
139-
serviceKey: process.env.SUPABASE_SERVICE_KEY as string,
140-
secretKey: (process.env.SUPABASE_SECRET_KEY
141-
?? process.env.SUPABASE_SERVICE_ROLE_KEY) as string,
132+
// Env var resolution order: NUXT_PUBLIC_*/NUXT_* (recommended) → SUPABASE_* (legacy fallback) → undefined
133+
url: process.env.NUXT_PUBLIC_SUPABASE_URL
134+
|| process.env.SUPABASE_URL || undefined,
135+
key: process.env.NUXT_PUBLIC_SUPABASE_KEY
136+
|| process.env.SUPABASE_KEY
137+
|| process.env.SUPABASE_PUBLISHABLE_KEY
138+
|| process.env.SUPABASE_ANON_KEY
139+
|| process.env.NUXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY
140+
|| process.env.NUXT_PUBLIC_SUPABASE_ANON_KEY || undefined,
141+
serviceKey: process.env.NUXT_SUPABASE_SERVICE_KEY
142+
|| process.env.SUPABASE_SERVICE_KEY || undefined,
143+
secretKey: process.env.NUXT_SUPABASE_SECRET_KEY
144+
|| process.env.SUPABASE_SECRET_KEY
145+
|| process.env.SUPABASE_SERVICE_ROLE_KEY || undefined,
142146
redirect: true,
143147
redirectOptions: {
144148
login: '/login',
@@ -185,7 +189,7 @@ export default defineNuxtModule<ModuleOptions>({
185189

186190
// Warn if the url isn't set.
187191
if (!finalUrl) {
188-
logger.warn('Missing supabase url, set it either in `nuxt.config.ts` or via env variable')
192+
logger.warn('Missing `NUXT_PUBLIC_SUPABASE_URL`, set it in `.env` or via `runtimeConfig.public.supabase.url` in `nuxt.config.ts`.')
189193
}
190194
else {
191195
try {
@@ -206,14 +210,14 @@ export default defineNuxtModule<ModuleOptions>({
206210

207211
// Fail build in production
208212
if (!nuxt.options.dev) {
209-
throw new Error('Invalid Supabase URL configuration')
213+
throw new Error(`Invalid Supabase URL: "${finalUrl}". Provide a valid URL or leave it unset for runtime configuration.`)
210214
}
211215
}
212216
}
213217

214218
// Warn if the key isn't set.
215219
if (!nuxt.options.runtimeConfig.public.supabase.key) {
216-
logger.warn('Missing supabase publishable key, set it either in `nuxt.config.ts` or via env variable')
220+
logger.warn('Missing `NUXT_PUBLIC_SUPABASE_KEY`, set it in `.env` or via `runtimeConfig.public.supabase.key` in `nuxt.config.ts`.')
217221
}
218222

219223
// Warn for deprecated features.
@@ -230,7 +234,7 @@ export default defineNuxtModule<ModuleOptions>({
230234
const hasSecretKey = !!supabaseConfig?.secretKey
231235

232236
if (hasServiceKey && !hasSecretKey) {
233-
logger.warn('`SUPABASE_SERVICE_KEY` is deprecated and will be removed in a future version. Please migrate to `SUPABASE_SECRET_KEY` (JWT signing key). See: https://supabase.com/blog/jwt-signing-keys')
237+
logger.warn('`SUPABASE_SERVICE_KEY` is deprecated. Migrate to `NUXT_SUPABASE_SECRET_KEY`. See: https://supabase.com/blog/jwt-signing-keys')
234238
}
235239

236240
// ensure callback URL is not using SSR

src/runtime/server/services/serverSupabaseServiceRole.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const serverSupabaseServiceRole: <T = Database>(event: H3Event) => Supaba
1717

1818
// Make sure a server key is set
1919
if (!serverKey) {
20-
throw new Error('Missing server key. Set either `SUPABASE_SECRET_KEY` (recommended) or `SUPABASE_SERVICE_KEY` (deprecated) in your environment variables.')
20+
throw new Error('Missing server key. Set `NUXT_SUPABASE_SECRET_KEY` in your environment variables.')
2121
}
2222

2323
// No need to recreate client if exists in request context

0 commit comments

Comments
 (0)