-
Notifications
You must be signed in to change notification settings - Fork 70
Hydrate dashboard creator emails from Supabase auth #334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ben-fornefeld
merged 4 commits into
main
from
pr-15-hydrate-dashboard-user-emails-from-supabase-auth-eng-4086
May 19, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
90714f8
Hydrate dashboard creator emails from Supabase auth
ben-fornefeld fb3c547
Prefer Supabase auth for dashboard creator emails
ben-fornefeld 7c7cf04
Log dashboard creator email lookup failures
ben-fornefeld 5077f02
Remove unreachable API key hydration guard
ben-fornefeld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import 'server-only' | ||
|
|
||
| import { l, serializeErrorForLog } from '@/core/shared/clients/logger/logger' | ||
| import { supabaseAdmin } from '@/core/shared/clients/supabase/admin' | ||
|
|
||
| export type AuthUserEmailResolver = ( | ||
| userIds: string[] | ||
| ) => Promise<Map<string, string | null>> | ||
|
|
||
| export async function getAuthUserEmailsById( | ||
| userIds: string[] | ||
| ): Promise<Map<string, string | null>> { | ||
| const uniqueUserIds = [...new Set(userIds.filter(Boolean))] | ||
| if (uniqueUserIds.length === 0) { | ||
| return new Map() | ||
| } | ||
|
|
||
| const { data, error } = await supabaseAdmin | ||
| .from('auth_users') | ||
| .select('id,email') | ||
| .in('id', uniqueUserIds) | ||
|
|
||
| if (error) { | ||
| throw error | ||
| } | ||
|
|
||
| return new Map( | ||
| data | ||
| ?.filter((user) => user.id) | ||
| .map((user) => [user.id as string, user.email]) ?? [] | ||
| ) | ||
| } | ||
|
|
||
| export async function resolveCreatorEmails< | ||
| T extends { | ||
| createdBy?: { id: string; email?: string | null } | null | ||
| }, | ||
| >(items: T[], resolveEmails: AuthUserEmailResolver): Promise<T[]> { | ||
| const creatorUserIds = items.flatMap((item) => { | ||
| const createdBy = item.createdBy | ||
| if (!createdBy) { | ||
| return [] | ||
| } | ||
|
|
||
| return [createdBy.id] | ||
| }) | ||
|
|
||
| if (creatorUserIds.length === 0) { | ||
| return items | ||
| } | ||
|
|
||
| let emailByUserId: Map<string, string | null> | ||
| try { | ||
| emailByUserId = await resolveEmails(creatorUserIds) | ||
| } catch (error) { | ||
| l.warn( | ||
| { | ||
| key: 'auth_user_emails:resolve_failed', | ||
| error: serializeErrorForLog(error), | ||
| context: { | ||
| userCount: new Set(creatorUserIds).size, | ||
| }, | ||
| }, | ||
| 'Failed to resolve creator emails from Supabase Auth' | ||
| ) | ||
|
|
||
| return items | ||
| } | ||
|
|
||
| return items.map((item) => { | ||
| const createdBy = item.createdBy | ||
| if (!createdBy) { | ||
| return item | ||
| } | ||
|
|
||
| return { | ||
| ...item, | ||
| createdBy: { | ||
| ...createdBy, | ||
| email: emailByUserId.get(createdBy.id) ?? null, | ||
| }, | ||
| } | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { createKeysRepository } from '@/core/modules/keys/repository.server' | ||
|
|
||
| const loggerMocks = vi.hoisted(() => ({ | ||
| warn: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/core/shared/clients/supabase/admin', () => ({ | ||
| supabaseAdmin: { | ||
| from: vi.fn(), | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock('@/core/shared/clients/logger/logger', () => ({ | ||
| l: loggerMocks, | ||
| serializeErrorForLog: vi.fn((error: unknown) => error), | ||
| })) | ||
|
|
||
| function createApiResponse<T>(input: { | ||
| ok: boolean | ||
| status: number | ||
| data?: T | ||
| error?: { message?: string } | null | ||
| }) { | ||
| return { | ||
| data: input.data, | ||
| error: input.error ?? null, | ||
| response: { | ||
| ok: input.ok, | ||
| status: input.status, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| const baseApiKey = { | ||
| createdAt: '2026-01-01T00:00:00Z', | ||
| id: 'api-key-id', | ||
| mask: { | ||
| prefix: 'e2b', | ||
| valueLength: 16, | ||
| maskedValuePrefix: 'abc', | ||
| maskedValueSuffix: 'xyz', | ||
| }, | ||
| name: 'Key', | ||
| } | ||
|
|
||
| describe('createKeysRepository', () => { | ||
| it('hydrates creator emails from Supabase Auth when listing API keys', async () => { | ||
| const firstUserId = '11111111-1111-1111-1111-111111111111' | ||
| const secondUserId = '22222222-2222-2222-2222-222222222222' | ||
| const resolveAuthUserEmailsById = vi.fn().mockResolvedValue( | ||
| new Map([ | ||
| [firstUserId, 'first@e2b.dev'], | ||
| [secondUserId, 'second@e2b.dev'], | ||
| ]) | ||
| ) | ||
|
|
||
| const infraClient = { | ||
| GET: vi.fn().mockResolvedValue( | ||
| createApiResponse({ | ||
| ok: true, | ||
| status: 200, | ||
| data: [ | ||
| { | ||
| ...baseApiKey, | ||
| id: 'first-key', | ||
| createdBy: { | ||
| id: firstUserId, | ||
| email: null, | ||
| }, | ||
| }, | ||
| { | ||
| ...baseApiKey, | ||
| id: 'second-key', | ||
| createdBy: { | ||
| id: secondUserId, | ||
| email: 'deprecated-response-value@e2b.dev', | ||
| }, | ||
| }, | ||
| ], | ||
| }) | ||
| ), | ||
| POST: vi.fn(), | ||
| DELETE: vi.fn(), | ||
| } | ||
|
|
||
| const repository = createKeysRepository( | ||
| { | ||
| accessToken: 'token', | ||
| teamId: 'team-id', | ||
| }, | ||
| { | ||
| infraClient: | ||
| infraClient as unknown as typeof import('@/core/shared/clients/api').infra, | ||
| authHeaders: vi.fn(() => ({ 'X-Supabase-Token': 'token' })), | ||
| resolveAuthUserEmailsById, | ||
| } | ||
| ) | ||
|
|
||
| const result = await repository.listTeamApiKeys() | ||
|
|
||
| expect(resolveAuthUserEmailsById).toHaveBeenCalledWith([ | ||
| firstUserId, | ||
| secondUserId, | ||
| ]) | ||
| expect(result).toEqual({ | ||
| ok: true, | ||
| data: [ | ||
| expect.objectContaining({ | ||
| id: 'first-key', | ||
| createdBy: { | ||
| id: firstUserId, | ||
| email: 'first@e2b.dev', | ||
| }, | ||
| }), | ||
| expect.objectContaining({ | ||
| id: 'second-key', | ||
| createdBy: { | ||
| id: secondUserId, | ||
| email: 'second@e2b.dev', | ||
| }, | ||
| }), | ||
| ], | ||
| }) | ||
| }) | ||
|
|
||
| it('keeps API key listing usable when creator email lookup fails', async () => { | ||
| loggerMocks.warn.mockClear() | ||
| const userId = '11111111-1111-1111-1111-111111111111' | ||
| const lookupError = new Error('lookup failed') | ||
| const resolveAuthUserEmailsById = vi.fn().mockRejectedValue(lookupError) | ||
|
|
||
| const apiKey = { | ||
| ...baseApiKey, | ||
| createdBy: { | ||
| id: userId, | ||
| email: null, | ||
| }, | ||
| } | ||
|
|
||
| const infraClient = { | ||
| GET: vi.fn().mockResolvedValue( | ||
| createApiResponse({ | ||
| ok: true, | ||
| status: 200, | ||
| data: [apiKey], | ||
| }) | ||
| ), | ||
| POST: vi.fn(), | ||
| DELETE: vi.fn(), | ||
| } | ||
|
|
||
| const repository = createKeysRepository( | ||
| { | ||
| accessToken: 'token', | ||
| teamId: 'team-id', | ||
| }, | ||
| { | ||
| infraClient: | ||
| infraClient as unknown as typeof import('@/core/shared/clients/api').infra, | ||
| authHeaders: vi.fn(() => ({ 'X-Supabase-Token': 'token' })), | ||
| resolveAuthUserEmailsById, | ||
| } | ||
| ) | ||
|
|
||
| await expect(repository.listTeamApiKeys()).resolves.toEqual({ | ||
| ok: true, | ||
| data: [apiKey], | ||
| }) | ||
|
|
||
| expect(loggerMocks.warn).toHaveBeenCalledWith( | ||
| { | ||
| key: 'auth_user_emails:resolve_failed', | ||
| error: lookupError, | ||
| context: { | ||
| userCount: 1, | ||
| }, | ||
| }, | ||
| 'Failed to resolve creator emails from Supabase Auth' | ||
| ) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.