Skip to content

useCoinbaseConnection

React hook for Coinbase OAuth connection state and actions.

When to use

Login / logout UI, and whenever you need Coinbase connected before loading balances or withdrawing.

Example

tsx
import { useCoinbaseConnection } from '@swapped/connect-sdk/react'

function ConnectCoinbase() {
  const {
    isConnected,
    isInitializing,
    connect,
    disconnect,
    isConnecting,
    isPopupOpen,
    error,
  } = useCoinbaseConnection()

  if (isInitializing) {
    return <p>Restoring Coinbase session…</p>
  }

  if (error) return <p>{error.message}</p>

  if (isConnected) {
    return (
      <button type="button" onClick={() => disconnect()}>
        Disconnect Coinbase
      </button>
    )
  }

  return (
    <button
      type="button"
      disabled={isConnecting || isPopupOpen}
      onClick={() => void connect()}
    >
      {isPopupOpen ? 'Complete login in the popup…' : 'Connect Coinbase'}
    </button>
  )
}

Returns

FieldPurpose
isConnectedCoinbase session is connected
isConnectingconnect() in progress
isPopupOpenLogin popup open
isInitializingRestoring stored token and/or validating the session
connect()Open OAuth popup
disconnect()Clear token and local withdraw/cooldown state
errorLast connect / validation failure

Errors

Surfaced on error and as rejected promises from connect():

CodeWhenWhat to do
OAUTH_POPUP_BLOCKEDBrowser blocked popupAsk to allow popups; retry
OAUTH_POPUP_CLOSEDUser closed popup earlyOffer retry
COINBASE_SESSION_EXPIREDToken invalid on later callsCall connect() again

Also listen with useOnCoinbaseSessionExpired / useOnOAuthError for mid-flow expiry and popup failures — see Event hooks and Errors.