# `useEmailAuth`

Handles email/password authentication including sign in, sign up, password reset, and email verification.

## Usage

```tsx
import { useEmailAuth } from '@openfort/react-native';

function LoginForm() {
  const { signInEmail, signUpEmail, isLoading, requiresEmailVerification } = useEmailAuth({
    onSuccess: ({ user }) => console.log('Email auth successful:', user?.id),
    onError: (error) => console.error('Email auth failed:', error?.message),
  });

  const handleLogin = async (email: string, password: string) => {
    const { error, user, requiresEmailVerification } = await signInEmail({ email, password });
    if (requiresEmailVerification) {
      // Prompt user to check inbox
    }
  };

  return null;
}
```

## Hook options

Pass these options when initializing the hook:

```ts
type UseEmailHookOptions = {
  emailVerificationRedirectTo?: string
  onSuccess?: (data: EmailAuthResult | EmailVerificationResult) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}
```

## Return type

The hook returns the following:

```ts
type UseEmailAuthReturn = {
  signInEmail(options: SignInEmailOptions): Promise<EmailAuthResult>
  signUpEmail(options: SignUpEmailOptions): Promise<EmailAuthResult>
  verifyEmail(options: VerifyEmailOptions): Promise<EmailVerificationResult>
  linkEmail(): void
  requestResetPassword(options: RequestResetPasswordOptions): Promise<EmailAuthResult>
  resetPassword(options: ResetPasswordOptions): Promise<EmailAuthResult>
  reset(): void
  requiresEmailVerification: boolean
  isLoading: boolean
  isError: boolean
  isSuccess: boolean
  error: Error | null
}

type EmailAuthResult = {
  user?: User
  requiresEmailVerification?: boolean
  error?: OpenfortError
}

type EmailVerificationResult = {
  email?: string
  error?: OpenfortError
}
```

## Parameters

### `signInEmail`

Signs in an existing user with email and password.

```ts
type SignInEmailOptions = {
  email: string
  password: string
  emailVerificationRedirectTo?: string
  onSuccess?: (data: EmailAuthResult) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}
```

### `signUpEmail`

Signs up a new user with email and password.

```ts
type SignUpEmailOptions = {
  email: string
  password: string
  name?: string
  emailVerificationRedirectTo?: string
  onSuccess?: (data: EmailAuthResult) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}
```

### `verifyEmail`

Verifies a user's email address using the token from the email link.

```ts
type VerifyEmailOptions = {
  token: string  // Verification token from email link
  onSuccess?: (data: EmailVerificationResult) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}
```

### `requestResetPassword`

Sends a password reset email to the user.

```ts
type RequestResetPasswordOptions = {
  email: string
  emailVerificationRedirectTo?: string
  onSuccess?: (data: EmailAuthResult) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}
```

### `resetPassword`

Resets the user's password using the token from the reset email.

```ts
type ResetPasswordOptions = {
  password: string
  token: string  // Reset token from email link
  onSuccess?: (data: EmailAuthResult) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}
```
