-
Notifications
You must be signed in to change notification settings - Fork 385
fix(clerk-js): Send client_id in CHIPS build #6758
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
Draft
jacekradko
wants to merge
12
commits into
main
Choose a base branch
from
jr.fix/signal-iframe-in-chips
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4ce98f8
fix(clerk-js): Signal iframe context in CHIPS build
jacekradko cb5a23f
add TSDoc
jacekradko f59c43e
wip
jacekradko ff88ba4
test improvements
jacekradko 21e25ab
update types
jacekradko 1b80c5e
Merge branch 'main' into jr.fix/signal-iframe-in-chips
jacekradko 5158344
wip
jacekradko a4feabd
use session_id to verify correct session at origin
jacekradko 44e5eaa
use client_id
jacekradko e24c213
Merge branch 'main' into jr.fix/signal-iframe-in-chips
jacekradko 9cef617
lint fixes
jacekradko e124825
fix tests
jacekradko 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@clerk/clerk-js': minor | ||
'@clerk/types': minor | ||
--- | ||
|
||
Adding iframeContext to SignIn and SignUp params when a CHIPS build is running in an iframe context | ||
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
231 changes: 231 additions & 0 deletions
231
packages/clerk-js/src/core/resources/__tests__/SignIn.spec.ts
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,231 @@ | ||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
import { inIframe } from '../../../utils/runtime'; | ||
import { BaseResource } from '../internal'; | ||
import { SignIn } from '../SignIn'; | ||
|
||
vi.mock('../../../utils/runtime', () => ({ | ||
inIframe: vi.fn(), | ||
})); | ||
|
||
const originalBuildVariant = (globalThis as any).__BUILD_VARIANT_CHIPS__; | ||
(globalThis as any).__BUILD_VARIANT_CHIPS__ = true; | ||
|
||
describe('SignIn', () => { | ||
let signIn: SignIn; | ||
let mockCreate: any; | ||
let mockBuildUrlWithAuth: any; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
|
||
const mockClerk = { | ||
buildUrlWithAuth: vi.fn((url: string) => `https://clerk.example.com${url}`), | ||
client: { | ||
id: 'test-client-id', | ||
}, | ||
}; | ||
|
||
const mockFapiClient = { | ||
buildEmailAddress: vi.fn(() => '[email protected]'), | ||
}; | ||
|
||
SignIn.clerk = mockClerk as any; | ||
|
||
// Mock BaseResource.clerk for client ID access | ||
BaseResource.clerk = mockClerk as any; | ||
|
||
Object.defineProperty(SignIn, 'fapiClient', { | ||
get: () => mockFapiClient, | ||
configurable: true, | ||
}); | ||
|
||
signIn = new SignIn({ | ||
id: 'test-signin-id', | ||
status: 'needs_first_factor', | ||
first_factor_verification: { | ||
status: 'unverified', | ||
external_verification_redirect_url: 'https://oauth.provider.com/auth', | ||
}, | ||
} as any); | ||
|
||
mockCreate = vi.fn().mockResolvedValue({}); | ||
signIn.create = mockCreate; | ||
|
||
mockBuildUrlWithAuth = vi.fn((url: string) => `https://clerk.example.com${url}`); | ||
SignIn.clerk.buildUrlWithAuth = mockBuildUrlWithAuth; | ||
}); | ||
|
||
afterEach(() => { | ||
(globalThis as any).__BUILD_VARIANT_CHIPS__ = originalBuildVariant; | ||
}); | ||
|
||
describe('authenticateWithRedirectOrPopup', () => { | ||
it('should set clientId to true when CHIPS build and in iframe', async () => { | ||
vi.mocked(inIframe).mockReturnValue(true); | ||
|
||
const params = { | ||
strategy: 'oauth_google' as const, | ||
redirectUrl: '/callback', | ||
identifier: '[email protected]', | ||
}; | ||
|
||
const mockNavigate = vi.fn(); | ||
|
||
Object.defineProperty(signIn, 'firstFactorVerification', { | ||
value: { | ||
status: 'unverified', | ||
externalVerificationRedirectURL: 'https://oauth.provider.com/auth', | ||
}, | ||
writable: true, | ||
}); | ||
|
||
await signIn.authenticateWithRedirectOrPopup(params, mockNavigate); | ||
|
||
expect(mockCreate).toHaveBeenCalledWith({ | ||
strategy: 'oauth_google', | ||
identifier: '[email protected]', | ||
redirectUrl: 'https://clerk.example.com/callback', | ||
actionCompleteRedirectUrl: undefined, | ||
clientId: 'test-client-id', | ||
}); | ||
}); | ||
|
||
it('should not set clientId when not in iframe', async () => { | ||
vi.mocked(inIframe).mockReturnValue(false); | ||
|
||
const params = { | ||
strategy: 'oauth_google' as const, | ||
redirectUrl: '/callback', | ||
identifier: '[email protected]', | ||
}; | ||
|
||
const mockNavigate = vi.fn(); | ||
|
||
Object.defineProperty(signIn, 'firstFactorVerification', { | ||
value: { | ||
status: 'unverified', | ||
externalVerificationRedirectURL: 'https://oauth.provider.com/auth', | ||
}, | ||
writable: true, | ||
}); | ||
|
||
await signIn.authenticateWithRedirectOrPopup(params, mockNavigate); | ||
|
||
expect(mockCreate).toHaveBeenCalledWith({ | ||
strategy: 'oauth_google', | ||
identifier: '[email protected]', | ||
redirectUrl: 'https://clerk.example.com/callback', | ||
actionCompleteRedirectUrl: undefined, | ||
}); | ||
expect(mockCreate).toHaveBeenCalledWith(expect.not.objectContaining({ clientId: 'test-client-id' })); | ||
}); | ||
|
||
it('should not set clientId when not CHIPS build', async () => { | ||
(globalThis as any).__BUILD_VARIANT_CHIPS__ = false; | ||
|
||
vi.mocked(inIframe).mockReturnValue(true); | ||
|
||
const params = { | ||
strategy: 'oauth_google' as const, | ||
redirectUrl: '/callback', | ||
identifier: '[email protected]', | ||
}; | ||
|
||
const mockNavigate = vi.fn(); | ||
|
||
Object.defineProperty(signIn, 'firstFactorVerification', { | ||
value: { | ||
status: 'unverified', | ||
externalVerificationRedirectURL: 'https://oauth.provider.com/auth', | ||
}, | ||
writable: true, | ||
}); | ||
|
||
await signIn.authenticateWithRedirectOrPopup(params, mockNavigate); | ||
|
||
expect(mockCreate).toHaveBeenCalledWith(expect.not.objectContaining({ clientId: 'test-client-id' })); | ||
}); | ||
|
||
it('should not set clientId when continueSignIn is true', async () => { | ||
vi.mocked(inIframe).mockReturnValue(true); | ||
|
||
const params = { | ||
strategy: 'oauth_google' as const, | ||
redirectUrl: '/callback', | ||
identifier: '[email protected]', | ||
continueSignIn: true, | ||
}; | ||
|
||
const mockNavigate = vi.fn(); | ||
|
||
Object.defineProperty(signIn, 'firstFactorVerification', { | ||
value: { | ||
status: 'unverified', | ||
externalVerificationRedirectURL: 'https://oauth.provider.com/auth', | ||
}, | ||
writable: true, | ||
}); | ||
|
||
await signIn.authenticateWithRedirectOrPopup(params, mockNavigate); | ||
|
||
expect(mockCreate).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('SignInFuture.create', () => { | ||
it('should set clientId to true when CHIPS build and in iframe', async () => { | ||
(globalThis as any).__BUILD_VARIANT_CHIPS__ = true; | ||
vi.mocked(inIframe).mockReturnValue(true); | ||
|
||
const params = { | ||
strategy: 'oauth_google' as const, | ||
redirectUrl: '/callback', | ||
identifier: '[email protected]', | ||
}; | ||
|
||
const signInFuture = signIn.__internal_future; | ||
|
||
const mockBasePost = vi.fn().mockResolvedValue({}); | ||
signInFuture.resource.__internal_basePost = mockBasePost; | ||
|
||
await signInFuture.create(params); | ||
|
||
expect(mockBasePost).toHaveBeenCalledWith({ | ||
path: '/client/sign_ins', | ||
body: { | ||
strategy: 'oauth_google', | ||
redirectUrl: '/callback', | ||
identifier: '[email protected]', | ||
clientId: 'test-client-id', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should not set clientId when not in iframe', async () => { | ||
vi.mocked(inIframe).mockReturnValue(false); | ||
|
||
const params = { | ||
strategy: 'oauth_google' as const, | ||
redirectUrl: '/callback', | ||
identifier: '[email protected]', | ||
}; | ||
|
||
const signInFuture = signIn.__internal_future; | ||
|
||
const mockBasePost = vi.fn().mockResolvedValue({}); | ||
signInFuture.resource.__internal_basePost = mockBasePost; | ||
|
||
await signInFuture.create(params); | ||
|
||
expect(mockBasePost).toHaveBeenCalledWith({ | ||
path: '/client/sign_ins', | ||
body: { | ||
strategy: 'oauth_google', | ||
redirectUrl: '/callback', | ||
identifier: '[email protected]', | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
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.