Skip to content

useGuestAuth

Create a temporary guest user and optionally provision an embedded wallet automatically.

Request

type Request = {
  hook: 'useGuestAuth',
  params: [options?: GuestHookOptions]
}
 
type GuestHookOptions = OpenfortHookOptions<GuestHookResult> & CreateWalletPostAuthOptions
 
type CreateWalletPostAuthOptions = {
  logoutOnError?: boolean
  recoverWalletAutomatically?: boolean
}
 
type OpenfortHookOptions<T> = {
  onSuccess?: (data: T) => void
  onError?: (error: import("@openfort/react").OpenfortError) => void
  onSettled?: (data: T | undefined | null, error: import("@openfort/react").OpenfortError | null) => void
  throwOnError?: boolean
}

Response

type Response = GuestAuthReturn
 
type GuestAuthReturn = GuestStatusFlags & {
  signUpGuest(options?: GuestHookOptions): Promise<GuestHookResult>
}
 
type GuestStatusFlags = {
  isLoading: boolean
  isError: boolean
  isSuccess: boolean
  error?: import("@openfort/react").OpenfortError | null
}
 
type GuestHookResult = {
  error?: import("@openfort/react").OpenfortError
  user?: import("@openfort/openfort-js").AuthPlayerResponse
  wallet?: import("@openfort/react").UserWallet
}

Example

import { useGuestAuth } from "@openfort/react"
 
function GuestEntry() {
  const { signUpGuest, isLoading, isError, isSuccess, error } = useGuestAuth({
    recoverWalletAutomatically: true,
    throwOnError: true,
    onSuccess: () => {},
    onError: () => {},
    onSettled: () => {},
  })
 
  const handleClick = async () => {
    const { user } = await signUpGuest()
    if (user) {
      console.log("Guest created", user.player.publicId)
    }
  }
 
  return <button onClick={handleClick} disabled={isLoading}>Continue as guest</button>
}