# `useOAuth`

Authenticates with third-party providers like Google, Apple, Facebook, Discord, and Twitter.

:::info
Native Sign in with Apple is used on iOS when available. For other providers, a secure web-based OAuth flow is used.
:::

## Usage

```tsx
import { OAuthProvider, useOAuth } from '@openfort/react-native';

function SocialLogin() {
  const { initOAuth, linkOauth, isLoading, isError, error } = useOAuth({
    onSuccess: ({ user }) => console.log('OAuth completed for', user?.id),
  });

  const loginWithGoogle = () => initOAuth({ provider: OAuthProvider.GOOGLE });
  const loginWithApple = () => initOAuth({ provider: OAuthProvider.APPLE });

  // Link another provider for the signed-in user
  const linkDiscord = () => linkOauth({ provider: OAuthProvider.DISCORD });

  return null;
}
```

## Hook options

Pass these options when initializing the hook:

```ts
type AuthHookOptions = {
  redirectTo?: string
  onSuccess?: (data: OAuthResult) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}
```

## Return type

The hook returns the following:

```ts
type UseOAuthReturn = {
  initOAuth(options: InitOAuthOptions): Promise<OAuthResult>
  linkOauth(options: InitOAuthOptions): Promise<OAuthResult>
  isLoading: boolean
  isError: boolean
  isSuccess: boolean
  error: Error | null
}

type OAuthResult = {
  user?: User
  error?: OpenfortError
}
```

## Parameters

### `initOAuth` / `linkOauth`

Initiates an OAuth flow with the specified provider. Use `linkOauth` to link a provider to an existing authenticated account.

```ts
type InitOAuthOptions = {
  provider: OAuthProvider
  redirectTo?: string
  onSuccess?: (data: OAuthResult) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}

const OAuthProvider = {
  GOOGLE: "google",
  FACEBOOK: "facebook",
  DISCORD: "discord",
  TWITTER: "twitter",
  EPIC_GAMES: "epic_games",
  APPLE: "apple",
  LINE: "line",
} as const
```
