Skip to content

useVerifyOTP

Hook for verifying a one-time code and authenticating

Import

import { useVerifyOTP } from '@zerodev/wallet-react'

Usage

import { useState } from 'react'
import { useVerifyOTP } from '@zerodev/wallet-react'
import { useAccount } from 'wagmi'
 
function VerifyCode({ otpId }: { otpId: string }) {
  const [code, setCode] = useState('')
  const verifyOTP = useVerifyOTP()
  const { address, isConnected } = useAccount()
 
  if (isConnected) {
    return <p>Authenticated: {address}</p>
  }
 
  return (
    <div>
      <input
        type="text"
        value={code}
        onChange={(e) => setCode(e.target.value)}
        placeholder="Enter verification code"
      />
      <button
        onClick={() => verifyOTP.mutateAsync({ code, otpId })}
        disabled={verifyOTP.isPending || !code}
      >
        {verifyOTP.isPending ? 'Verifying...' : 'Verify'}
      </button>
      {verifyOTP.isError && <p>Error: {verifyOTP.error.message}</p>}
    </div>
  )
}

Parameters

code

string

Required. The verification code entered by the user.

otpId

string

Required. The OTP identifier returned by useSendOTP.

Return Types

TanStack Query mutation docs

mutate

(variables: { code: string; otpId: string }) => void

The mutation function to verify the OTP.

mutateAsync

(variables: { code: string; otpId: string }) => Promise<void>

Similar to mutate but returns a promise. Resolves when the code is verified 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).