diff --git a/src/types/onramp.ts b/src/types/onramp.ts index 59f5308..f534ba8 100644 --- a/src/types/onramp.ts +++ b/src/types/onramp.ts @@ -60,6 +60,13 @@ type BaseOnRampAppParams = { * `{ "0x1": ["base"] }` */ addresses?: Record; + /** + * The session token used to initialize the Coinbase Onramp experience. + * This token expires after a short period of time and can only be used once. + * A new token must be obtained for every new session. + * @see https://docs.cdp.coinbase.com/onramp/docs/api-initializing#getting-an-onramp-session-token + */ + sessionToken?: string; /** * This optional parameter will restrict the assets available for the user to buy/send. It acts as a filter on the * networks specified in the {addresses} param. diff --git a/src/types/widget.ts b/src/types/widget.ts index cd09640..d025128 100644 --- a/src/types/widget.ts +++ b/src/types/widget.ts @@ -20,19 +20,10 @@ export type EmbeddedContentStyles = { top?: string; }; -export type CBPayExperienceWithAppId = { - appId: string; - sessionToken?: never; -} & CBPayExperienceBaseOptions; - -export type CBPayExperienceWithSessionToken = { - sessionToken: string; - appId?: never; -} & CBPayExperienceBaseOptions; - -type CBPayExperienceBaseOptions = { +export type CBPayExperienceOptions = { widgetParameters: T; target?: string; + appId: string; host?: string; debug?: boolean; theme?: Theme; @@ -46,7 +37,3 @@ type CBPayExperienceBaseOptions = { experienceLoggedIn?: Experience; experienceLoggedOut?: Experience; }; - -export type CBPayExperienceOptions = - | CBPayExperienceWithAppId - | CBPayExperienceWithSessionToken; diff --git a/src/utils/CoinbasePixel.test.ts b/src/utils/CoinbasePixel.test.ts index bbd970e..9182c26 100644 --- a/src/utils/CoinbasePixel.test.ts +++ b/src/utils/CoinbasePixel.test.ts @@ -51,9 +51,14 @@ describe('CoinbasePixel', () => { }); it('should initialize with sessionToken', () => { - const instance = createUntypedPixel({ sessionToken: 'test', appParams: defaultAppParams }); + const sessionToken = 'test-session-token'; + const instance = createUntypedPixel({ + appId: 'test', + appParams: { ...defaultAppParams, sessionToken }, + }); - expect(instance.sessionToken).toEqual('test'); + expect(instance.appId).toEqual('test'); + expect(instance.appParams.sessionToken).toEqual(sessionToken); }); it('should handle opening the embedded experience when logged out', () => { diff --git a/src/utils/CoinbasePixel.ts b/src/utils/CoinbasePixel.ts index 7cf4d2d..6b96e3e 100644 --- a/src/utils/CoinbasePixel.ts +++ b/src/utils/CoinbasePixel.ts @@ -26,8 +26,7 @@ export type ExperienceListeners = { export type CoinbasePixelConstructorParams = { host?: string; - appId?: string; - sessionToken?: string; + appId: string; appParams: JsonObject; debug?: boolean; theme?: Theme; @@ -43,8 +42,7 @@ export type OpenExperienceOptions = { export class CoinbasePixel { private debug: boolean; private host: string; - private appId: string | undefined; - private sessionToken: string | undefined; + private appId: string; private eventStreamListeners: Partial void)[]>> = {}; private unsubs: (() => void)[] = []; private appParams: JsonObject; @@ -54,14 +52,12 @@ export class CoinbasePixel { constructor({ host = DEFAULT_HOST, appId, - sessionToken, appParams, debug, theme, }: CoinbasePixelConstructorParams) { this.host = host; this.appId = appId; - this.sessionToken = sessionToken; this.appParams = appParams; this.debug = debug || false; this.theme = theme; @@ -79,7 +75,6 @@ export class CoinbasePixel { const url = generateOnRampURL({ appId: this.appId, - sessionToken: this.sessionToken, host: this.host, theme: this.theme ?? undefined, ...this.appParams, @@ -176,10 +171,6 @@ export class CoinbasePixel { }; private startDirectSignin = (callback: () => void) => { - if (!this.appId) { - throw new Error('appId is required for direct signin'); - } - const queryParams = new URLSearchParams(); queryParams.set('appId', this.appId); queryParams.set('type', 'direct');