-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix(auth): make DISABLE_AUTH usable for /workspace #3297
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
waleedlatif1
merged 1 commit into
simstudioai:staging
from
jayy-77:fix/disable-auth-workspace-2524
Feb 24, 2026
+152
−3
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { createMockRequest, setupCommonApiMocks } from '@sim/testing' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const handlerMocks = vi.hoisted(() => ({ | ||
| betterAuthGET: vi.fn(), | ||
| betterAuthPOST: vi.fn(), | ||
| ensureAnonymousUserExists: vi.fn(), | ||
| createAnonymousGetSessionResponse: vi.fn(() => ({ | ||
| data: { | ||
| user: { id: 'anon' }, | ||
| session: { id: 'anon-session' }, | ||
| }, | ||
| })), | ||
| })) | ||
|
|
||
| vi.mock('better-auth/next-js', () => ({ | ||
| toNextJsHandler: () => ({ | ||
| GET: handlerMocks.betterAuthGET, | ||
| POST: handlerMocks.betterAuthPOST, | ||
| }), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/auth', () => ({ | ||
| auth: { handler: {} }, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/auth/anonymous', () => ({ | ||
| ensureAnonymousUserExists: handlerMocks.ensureAnonymousUserExists, | ||
| createAnonymousGetSessionResponse: handlerMocks.createAnonymousGetSessionResponse, | ||
| })) | ||
|
|
||
| describe('auth catch-all route (DISABLE_AUTH get-session)', () => { | ||
| beforeEach(() => { | ||
| vi.resetModules() | ||
| setupCommonApiMocks() | ||
| handlerMocks.betterAuthGET.mockReset() | ||
| handlerMocks.betterAuthPOST.mockReset() | ||
| handlerMocks.ensureAnonymousUserExists.mockReset() | ||
| handlerMocks.createAnonymousGetSessionResponse.mockClear() | ||
| }) | ||
|
|
||
| it('returns anonymous session in better-auth response envelope when auth is disabled', async () => { | ||
| vi.doMock('@/lib/core/config/feature-flags', () => ({ isAuthDisabled: true })) | ||
|
|
||
| const req = createMockRequest( | ||
| 'GET', | ||
| undefined, | ||
| {}, | ||
| 'http://localhost:3000/api/auth/get-session' | ||
| ) | ||
| const { GET } = await import('@/app/api/auth/[...all]/route') | ||
|
|
||
| const res = await GET(req as any) | ||
| const json = await res.json() | ||
|
|
||
| expect(handlerMocks.ensureAnonymousUserExists).toHaveBeenCalledTimes(1) | ||
| expect(handlerMocks.betterAuthGET).not.toHaveBeenCalled() | ||
| expect(json).toEqual({ | ||
| data: { | ||
| user: { id: 'anon' }, | ||
| session: { id: 'anon-session' }, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('delegates to better-auth handler when auth is enabled', async () => { | ||
| vi.doMock('@/lib/core/config/feature-flags', () => ({ isAuthDisabled: false })) | ||
|
|
||
| handlerMocks.betterAuthGET.mockResolvedValueOnce( | ||
| new (await import('next/server')).NextResponse(JSON.stringify({ data: { ok: true } }), { | ||
| headers: { 'content-type': 'application/json' }, | ||
| }) as any | ||
| ) | ||
|
|
||
| const req = createMockRequest( | ||
| 'GET', | ||
| undefined, | ||
| {}, | ||
| 'http://localhost:3000/api/auth/get-session' | ||
| ) | ||
| const { GET } = await import('@/app/api/auth/[...all]/route') | ||
|
|
||
| const res = await GET(req as any) | ||
| const json = await res.json() | ||
|
|
||
| expect(handlerMocks.ensureAnonymousUserExists).not.toHaveBeenCalled() | ||
| expect(handlerMocks.betterAuthGET).toHaveBeenCalledTimes(1) | ||
| expect(json).toEqual({ data: { ok: true } }) | ||
| }) | ||
| }) |
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,31 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { extractSessionDataFromAuthClientResult } from '@/lib/auth/session-response' | ||
|
|
||
| describe('extractSessionDataFromAuthClientResult', () => { | ||
| it('returns null for non-objects', () => { | ||
| expect(extractSessionDataFromAuthClientResult(null)).toBeNull() | ||
| expect(extractSessionDataFromAuthClientResult(undefined)).toBeNull() | ||
| expect(extractSessionDataFromAuthClientResult('nope')).toBeNull() | ||
| expect(extractSessionDataFromAuthClientResult(123)).toBeNull() | ||
| }) | ||
|
|
||
| it('prefers .data when present', () => { | ||
| expect(extractSessionDataFromAuthClientResult({ data: null })).toBeNull() | ||
|
|
||
| const session = { user: { id: 'u1' }, session: { id: 's1' } } | ||
| expect(extractSessionDataFromAuthClientResult({ data: session })).toEqual(session) | ||
| }) | ||
|
|
||
| it('falls back to raw session payload shape', () => { | ||
| const raw = { user: { id: 'u1' }, session: { id: 's1' } } | ||
| expect(extractSessionDataFromAuthClientResult(raw)).toEqual(raw) | ||
| }) | ||
|
|
||
| it('returns null for unknown object shapes', () => { | ||
| expect(extractSessionDataFromAuthClientResult({})).toBeNull() | ||
| expect(extractSessionDataFromAuthClientResult({ ok: true })).toBeNull() | ||
| }) | ||
| }) |
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,19 @@ | ||
| export function extractSessionDataFromAuthClientResult(result: unknown): unknown | null { | ||
| if (!result || typeof result !== 'object') { | ||
| return null | ||
| } | ||
|
|
||
| const record = result as Record<string, unknown> | ||
|
|
||
| // Expected shape from better-auth client: { data: <session> } | ||
| if ('data' in record) { | ||
| return (record as { data?: unknown }).data ?? null | ||
| } | ||
|
|
||
| // Fallback for raw session payloads: { user, session } | ||
| if ('user' in record) { | ||
| return record | ||
| } | ||
|
|
||
| return null | ||
| } |
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.