{"version":3,"file":"HeadlessB2BOAuthClient.mjs","sources":["../../../../../../../web/src/b2b/HeadlessB2BOAuthClient.ts"],"sourcesContent":["import {\n  DisabledDFPProtectedAuthProvider,\n  HeadlessB2BOAuthClient as BaseHeadlessB2BOAuthClient,\n  IB2BSubscriptionService,\n  IDFPProtectedAuthProvider,\n  INetworkClient,\n  IPKCEManager,\n} from '@stytch/core';\nimport {\n  IHeadlessB2BOAuthClient,\n  OneTapPositions,\n  StytchProjectConfigurationInput,\n  StytchSDKUsageError,\n} from '@stytch/core/public';\n\nimport { OneTapRenderResult } from '../oneTap/GoogleOneTapClient';\nimport { B2BOneTapProvider } from './oneTap/B2BOneTapProvider';\n\nexport type B2BGoogleOneTapDiscoveryOAuthOptions = {\n  /**\n   * The URL that Stytch redirects to after the Google One Tap discovery flow is completed.\n   * This should be a URL that verifies the request by querying Stytch's /oauth/discovery/authenticate endpoint.\n   * If this value is not passed, the default discovery redirect URL that you set in your Dashboard is used.\n   * If you have not set a default discovery redirect URL, an error is returned.\n   */\n  discovery_redirect_url?: string;\n  /**\n   * Controls whether clicking outside the One Tap prompt dismisses the prompt.\n   * Defaults to true.\n   */\n  cancel_on_tap_outside?: boolean;\n};\n\nexport type B2BGoogleOneTapOAuthOptions = {\n  /**\n   * The ID of the organization that the end user is logging in to.\n   */\n  organization_id: string;\n\n  /**\n   * The URL that Stytch redirects to after the Google One Tap flow is completed for a member who already exists.\n   * This should be a URL that verifies the request by querying Stytch's /oauth/authenticate endpoint.\n   * If this value is not passed, the default login redirect URL that you set in your Dashboard is used.\n   * If you have not set a default login redirect URL, an error is returned.\n   */\n  login_redirect_url?: string;\n\n  /**\n   * The URL that Stytch redirects to after the Google One Tap flow is completed for a member who does not yet exist.\n   * This should be a URL that verifies the request by querying Stytch's /oauth/authenticate endpoint.\n   * If this value is not passed, the default signup redirect URL that you set in your Dashboard is used.\n   * If you have not set a default signup redirect URL, an error is returned.\n   */\n  signup_redirect_url?: string;\n  /**\n   * Controls whether clicking outside the One Tap prompt dismisses the prompt.\n   * Defaults to true.\n   */\n  cancel_on_tap_outside?: boolean;\n};\n\ninterface IB2BGoogleOneTapOAuthProvider {\n  discovery: {\n    /**\n     * Start a discovery OAuth flow by showing the Google one tap prompt in the top right corner of the user's browser.\n     * You can configure this to be started by a user action (i.e Button click) or on load/render.\n     * @example\n     * const showGoogleOneTap = useCallback(()=> {\n     *   stytch.oauth.googleOneTap.discovery.start({\n     *     discovery_redirect_url: 'https://example.com/oauth/callback',\n     *   })\n     * }, [stytch]);\n     * return (\n     *   <Button onClick={showGoogleOneTap}> Show Google one tap </Button>\n     * );\n     *\n     * @param options - A {@link B2BGoogleOneTapDiscoveryOAuthOptions} object\n     *\n     * @returns A {@link OneTapRenderResult} object. The result object includes if the one-tap prompt\n     * was rendered, and a reason if it couldn't be rendered.\n     *\n     * @throws An Error if the one tap client cannot be created.\n     */\n    start(options?: B2BGoogleOneTapDiscoveryOAuthOptions): Promise<OneTapRenderResult>;\n  };\n  /**\n   * Start an OAuth flow by showing the Google one tap prompt in the top right corner of the user's browser.\n   * You can configure this to be started by a user action (i.e Button click) or on load/render.\n   * @example\n   * const showGoogleOneTap = useCallback(()=> {\n   *   stytch.oauth.googleOneTap.start({\n   *     organization_id: 'organization-test-123',\n   *   })\n   * }, [stytch]);\n   * return (\n   *   <Button onClick={showGoogleOneTap}> Show Google one tap </Button>\n   * );\n   *\n   * @param options - A {@link B2BGoogleOneTapOAuthOptions} object\n   *\n   * @returns A {@link OneTapRenderResult} object. The result object includes if the one-tap prompt\n   * was rendered, and a reason if it couldn't be rendered.\n   *\n   * @throws An Error if the one tap client cannot be created.\n   */\n  start(options?: B2BGoogleOneTapOAuthOptions): Promise<OneTapRenderResult>;\n}\n\ntype DynamicConfig = Promise<{\n  cnameDomain: null | string;\n  pkceRequiredForOAuth: boolean;\n}>;\ntype Config = {\n  publicToken: string;\n  testAPIURL: string;\n  liveAPIURL: string;\n};\n\nexport interface IWebB2BOAuthClient<TProjectConfiguration extends StytchProjectConfigurationInput>\n  extends IHeadlessB2BOAuthClient<TProjectConfiguration> {\n  googleOneTap: IB2BGoogleOneTapOAuthProvider;\n}\n\nexport class HeadlessB2BOAuthClient<TProjectConfiguration extends StytchProjectConfigurationInput>\n  extends BaseHeadlessB2BOAuthClient<TProjectConfiguration>\n  implements IWebB2BOAuthClient<TProjectConfiguration>\n{\n  constructor(\n    _networkClient: INetworkClient,\n    _subscriptionService: IB2BSubscriptionService<TProjectConfiguration>,\n    _pkceManager: IPKCEManager,\n    _dynamicConfig: DynamicConfig,\n    _config: Config,\n    _dfpProtectedAuth: IDFPProtectedAuthProvider = DisabledDFPProtectedAuthProvider(),\n    private _oneTap: B2BOneTapProvider,\n  ) {\n    super(_networkClient, _subscriptionService, _pkceManager, _dynamicConfig, _config, _dfpProtectedAuth);\n  }\n\n  private startOneTapDiscovery = async (options: B2BGoogleOneTapDiscoveryOAuthOptions): Promise<OneTapRenderResult> => {\n    const clientResult = await this._oneTap.createOneTapClient();\n    if (!clientResult.success) {\n      throw new Error(`One Tap could not load: ${clientResult.reason}`);\n    }\n\n    const { client } = clientResult;\n\n    const onSuccessCallback = this._oneTap.createOnDiscoverySuccessHandler({\n      discoveryRedirectUrl: options.discovery_redirect_url,\n      onSuccess: this._oneTap.redirectOnSuccess,\n    });\n\n    return client.render({\n      style: {\n        position: OneTapPositions.floating,\n      },\n      callback: onSuccessCallback,\n      cancelOnTapOutside: options.cancel_on_tap_outside,\n    });\n  };\n\n  private startOneTap = async (options: B2BGoogleOneTapOAuthOptions): Promise<OneTapRenderResult> => {\n    if (!options.organization_id) {\n      throw new StytchSDKUsageError('stytch.oauth.googleOneTap.start', 'organization_id is a required argument');\n    }\n    const clientResult = await this._oneTap.createOneTapClient();\n    if (!clientResult.success) {\n      throw new Error(`One Tap could not load: ${clientResult.reason}`);\n    }\n\n    const { client } = clientResult;\n\n    const onSuccessCallback = this._oneTap.createOnSuccessHandler({\n      organizationId: options.organization_id,\n      signupRedirectUrl: options.signup_redirect_url,\n      loginRedirectUrl: options.login_redirect_url,\n      onSuccess: this._oneTap.redirectOnSuccess,\n    });\n\n    return client.render({\n      style: {\n        position: OneTapPositions.floating,\n      },\n      callback: onSuccessCallback,\n      cancelOnTapOutside: options.cancel_on_tap_outside,\n    });\n  };\n\n  googleOneTap: IB2BGoogleOneTapOAuthProvider = {\n    discovery: {\n      start: this.startOneTapDiscovery,\n    },\n    start: this.startOneTap,\n  };\n}\n"],"names":["HeadlessB2BOAuthClient","BaseHeadlessB2BOAuthClient","_networkClient","_subscriptionService","_pkceManager","_dynamicConfig","_config","_dfpProtectedAuth","DisabledDFPProtectedAuthProvider","_oneTap","startOneTapDiscovery","options","clientResult","createOneTapClient","success","Error","reason","client","onSuccessCallback","createOnDiscoverySuccessHandler","discoveryRedirectUrl","discovery_redirect_url","onSuccess","redirectOnSuccess","render","style","position","OneTapPositions","floating","callback","cancelOnTapOutside","cancel_on_tap_outside","startOneTap","organization_id","StytchSDKUsageError","createOnSuccessHandler","organizationId","signupRedirectUrl","signup_redirect_url","loginRedirectUrl","login_redirect_url","googleOneTap","discovery","start"],"mappings":";;;;;;;;;;AA2HO,MAAMA,sBAAAA,SACHC,wBAAAA,CAAAA;;AAGR,IAAA,WAAA,CACEC,cAA8B,EAC9BC,oBAAoE,EACpEC,YAA0B,EAC1BC,cAA6B,EAC7BC,OAAe,EACfC,iBAAAA,GAA+CC,gCAAAA,EAAkC,EACzEC,OAA0B,CAClC;AACA,QAAA,KAAK,CAACP,cAAAA,EAAgBC,oBAAAA,EAAsBC,cAAcC,cAAAA,EAAgBC,OAAAA,EAASC,yBAF3EE,OAAAA,GAAAA,OAAAA;AAGV,IAAA;AAEQC,IAAAA,oBAAAA,GAAuB,OAAOC,OAAAA,GAAAA;AACpC,QAAA,MAAMC,eAAe,MAAM,IAAI,CAACH,OAAO,CAACI,kBAAkB,EAAA;QAC1D,IAAI,CAACD,YAAAA,CAAaE,OAAO,EAAE;AACzB,YAAA,MAAM,IAAIC,KA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