Skip to content

useAuthenticateOAuth

Hook for authenticating with an OAuth provider

Import

import { useAuthenticateOAuth, OAUTH_PROVIDERS } from '@zerodev/wallet-react'

Usage

import {
  useAuthenticateOAuth,
  OAUTH_PROVIDERS,
} from '@zerodev/wallet-react'
import { useAccount } from 'wagmi'
 
function OAuthLogin() {
  const authenticateOAuth = useAuthenticateOAuth()
  const { address, isConnected } = useAccount()
 
  if (isConnected) {
    return <p>Logged in: {address}</p>
  }
 
  return (
    <button
      onClick={() =>
        authenticateOAuth.mutateAsync({
          provider: OAUTH_PROVIDERS.GOOGLE,
        })
      }
      disabled={authenticateOAuth.isPending}
    >
      {authenticateOAuth.isPending ? 'Signing in...' : 'Sign in with Google'}
    </button>
  )
}

Parameters

provider

OAuthProvider

Required. The OAuth provider to authenticate with. Use the OAUTH_PROVIDERS constant:

  • OAUTH_PROVIDERS.GOOGLE — Google OAuth

Return Types

TanStack Query mutation docs

mutate

(variables: { provider: OAuthProvider }) => void

The mutation function to start the OAuth flow. Opens a popup window for the user to sign in.

mutateAsync

(variables: { provider: OAuthProvider }) => Promise<void>

Similar to mutate but returns a promise. Resolves when the OAuth flow completes and the wallet is connected.

data

void

This mutation does not return data. On success, the Wagmi connector is connected and the account is available via useAccount.

error

Error | null

The error object for the mutation, if an error was encountered.

isError / isIdle / isPending / isSuccess

boolean

Boolean variables derived from status.

isPaused

boolean

  • will be true if the mutation has been paused.
  • see Network Mode for more information.

status

'idle' | 'pending' | 'error' | 'success'

  • 'idle' initial status prior to the mutation function executing.
  • 'pending' if the mutation is currently executing.
  • 'error' if the last mutation attempt resulted in an error.
  • 'success' if the last mutation attempt was successful.

reset

() => void

A function to clean the mutation internal state (e.g. it resets the mutation to its initial state).