Skip to content

useOAuth

Kick off OAuth authentication or linking flows for providers such as Google, Apple, Discord, and more.

Request

type Request = {
  hook: 'useOAuth',
  params: [options?: AuthHookOptions]
}
 
type AuthHookOptions = {
  redirectTo?: string
  logoutOnError?: boolean
  recoverWalletAutomatically?: boolean
  onSuccess?: (data: StoreCredentialsResult | InitOAuthReturnType) => void
  onError?: (error: import("@openfort/react").OpenfortError) => void
  onSettled?: (data: StoreCredentialsResult | InitOAuthReturnType | undefined | null, error: import("@openfort/react").OpenfortError | null) => void
  throwOnError?: boolean
}

Response

type Response = UseOAuthReturn
 
type UseOAuthReturn = OAuthStatusFlags & {
  initOAuth(options: InitializeOAuthOptions): Promise<InitOAuthReturnType>
  linkOauth(options: InitializeOAuthOptions): Promise<InitOAuthReturnType>
  storeCredentials(options: StoreCredentialsOptions): Promise<StoreCredentialsResult>
}
 
type OAuthStatusFlags = {
  isLoading: boolean
  isError: boolean
  isSuccess: boolean
  error?: import("@openfort/react").OpenfortError | null
}
 
type InitOAuthReturnType = {
  error?: import("@openfort/react").OpenfortError
}
 
type StoreCredentialsResult = {
  user?: import("@openfort/openfort-js").AuthPlayerResponse
  wallet?: import("@openfort/react").UserWallet
  error?: import("@openfort/react").OpenfortError
}
 
type InitializeOAuthOptions = {
  provider: import("@openfort/openfort-js").OAuthProvider
  redirectTo?: string
} & OpenfortHookOptions<InitOAuthReturnType>
 
type StoreCredentialsOptions = {
  player: string
  accessToken: string
  refreshToken: string
} & OpenfortHookOptions<StoreCredentialsResult> & CreateWalletPostAuthOptions

Example

import { OAuthProvider, useOAuth } from "@openfort/react"
 
function SocialLogin() {
  const { initOAuth, linkOauth, storeCredentials, isLoading, isError, isSuccess, error } = useOAuth({
    redirectTo: "https://app.example.com/auth/callback",
    throwOnError: true,
    onSuccess: () => {},
    onError: () => {},
    onSettled: () => {},
  })
 
  const loginWithGoogle = () => initOAuth({ provider: OAuthProvider.GOOGLE })
  const linkDiscord = () => linkOauth({ provider: OAuthProvider.DISCORD })
 
  return null
}