Skip to content

Commit 7f1b1a7

Browse files
feat: exposing keyName to SupabaseContext (#22)
* fix: correctly passing down the keyName based on authType - Only passing keyName for during 'createContextClient' if the authType is public * feat: exposing 'keyName' to SupabaseContext * docs: adding 'authKeyName' reference
1 parent 7c67416 commit 7f1b1a7

4 files changed

Lines changed: 52 additions & 1 deletion

File tree

docs/getting-started.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Every handler receives a `SupabaseContext` with these fields:
8080
| `userClaims` | `UserClaims \| null` | JWT-derived identity (`id`, `email`, `role`, `appMetadata`, `userMetadata`). `null` for non-user auth. |
8181
| `claims` | `JWTClaims \| null` | Raw JWT payload (snake_case). `null` for non-user auth. |
8282
| `authType` | `Allow` | Which auth mode matched: `'user'`, `'public'`, `'secret'`, or `'always'`. |
83+
| `authKeyName` | `string \| null` | Which auth key name of the API key that was used. |
8384
8485
The `supabase` client respects Row-Level Security. When `authType` is `'user'`, the client is scoped to that user's permissions. For other auth modes, it's initialized as anonymous.
8586

src/create-supabase-context.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('createSupabaseContext', () => {
2626
expect(result.data!.supabase).toBeDefined()
2727
expect(result.data!.supabaseAdmin).toBeDefined()
2828
expect(result.data!.authType).toBe('always')
29+
expect(result.data!.authKeyName).toBeNull()
2930
})
3031

3132
it('returns user and claims as null for non-user auth', async () => {
@@ -72,6 +73,28 @@ describe('createSupabaseContext', () => {
7273

7374
expect(result.error).toBeNull()
7475
expect(result.data!.authType).toBe('public')
76+
expect(result.data!.authKeyName).toBe('default')
77+
expect(result.data!.supabase).toBeDefined()
78+
expect(result.data!.supabaseAdmin).toBeDefined()
79+
})
80+
81+
it('accepts public named key auth', async () => {
82+
const req = new Request('http://localhost', {
83+
headers: { apikey: 'sb_publishable_web' },
84+
})
85+
const result = await createSupabaseContext(req, {
86+
allow: 'public:web',
87+
env: {
88+
...baseEnv,
89+
publishableKeys: {
90+
web: 'sb_publishable_web',
91+
},
92+
},
93+
})
94+
95+
expect(result.error).toBeNull()
96+
expect(result.data!.authType).toBe('public')
97+
expect(result.data!.authKeyName).toBe('web')
7598
expect(result.data!.supabase).toBeDefined()
7699
expect(result.data!.supabaseAdmin).toBeDefined()
77100
})
@@ -87,6 +110,28 @@ describe('createSupabaseContext', () => {
87110

88111
expect(result.error).toBeNull()
89112
expect(result.data!.authType).toBe('secret')
113+
expect(result.data!.authKeyName).toBe('default')
114+
})
115+
116+
it('accepts secret named key auth', async () => {
117+
const req = new Request('http://localhost', {
118+
headers: { apikey: 'sb_secret_web' },
119+
})
120+
const result = await createSupabaseContext(req, {
121+
allow: 'secret:web',
122+
env: {
123+
...baseEnv,
124+
secretKeys: {
125+
web: 'sb_secret_web',
126+
},
127+
},
128+
})
129+
130+
expect(result.error).toBeNull()
131+
expect(result.data!.authType).toBe('secret')
132+
expect(result.data!.authKeyName).toBe('web')
133+
expect(result.data!.supabase).toBeDefined()
134+
expect(result.data!.supabaseAdmin).toBeDefined()
90135
})
91136

92137
it('rejects invalid secret key', async () => {

src/create-supabase-context.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ export async function createSupabaseContext<Database = unknown>(
5252
supabaseOptions: options?.supabaseOptions,
5353
}
5454

55+
const publicKeyName = auth.authType === 'public' ? auth.keyName : undefined
5556
const supabase = createContextClient<Database>({
56-
auth: { token: auth.token, keyName: auth.keyName },
57+
auth: { token: auth.token, keyName: publicKeyName },
5758
...config,
5859
})
5960

@@ -70,6 +71,7 @@ export async function createSupabaseContext<Database = unknown>(
7071
userClaims: auth.userClaims,
7172
claims: auth.claims,
7273
authType: auth.authType,
74+
authKeyName: auth.keyName,
7375
},
7476
error: null,
7577
}

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,7 @@ export interface SupabaseContext<Database = unknown> {
313313

314314
/** The auth mode that was used for this request. */
315315
authType: Allow
316+
317+
/** The auth key name of the API key that was used for this request. */
318+
authKeyName?: string | null
316319
}

0 commit comments

Comments
 (0)