Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add docs for react auth hooks #139

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
90 changes: 89 additions & 1 deletion docs/react/auth/hooks/useDeleteUserMutation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,92 @@
title: useDeleteUserMutation
---

This hook is implemented, but the documentation is a work in progress, check back soon.
The `useDeleteUserMutation` hook simplifies the process of deleting a user from Firebase Authentication.
It provides built-in state management for handling the mutation lifecycle (e.g., loading, success, error) and supports custom behaviors like `onSuccess` or `onError`.

## Features

- **Easy Firebase Auth Integration**: Leverages Firebase's `deleteUser` functionality.
- **Built-in State Management**: Automatically manages `idle`, `loading`, `success`, and `error` states.
- **Customizable Callbacks**: Supports `onSuccess`, `onError`, and other TanStack Query mutation options.
- **Optimized for TanStack Query**: Seamlessly integrates with TanStack Query for caching and refetching strategies.

## Installation

Ensure you have the necessary Firebase, TanStack Query and TanStack Query Firebase packages installed:

```bash
npm install firebase @tanstack/react-query @tanstack-query-firebase/react
```

## Usage

```jsx
import { useDeleteUserMutation } from "@tanstack-query-firebase/react/auth";

function Component() {
const { mutate, isPending, isSuccess, isError, error } =
useDeleteUserMutation();

const handleDeleteUser = () => {
const user = auth.currentUser;
if (user) {
mutate(user);
}
};

return (
<div>
{isPending && <p>Deleting user...</p>}
{isSuccess && <p>User deleted successfully!</p>}
{isError && <p>Error: {error.message}</p>}

<button onClick={handleDeleteUser} disabled={isPending}>
Delete Account
</button>
</div>
);
}
```

## API Reference

`useDeleteUserMutation(options?)`

### Parameters

- `options` (optional):
Object for customizing mutation behavior. Accepts all options supported by TanStack Query's `useMutation`, such as:
- `onSuccess`: Callback fired when the mutation succeeds.
- `onError`: Callback fired when the mutation fails.
- `onSettled`: Callback fired after the mutation finishes, regardless of success or failure.

See [useMutation](https://tanstack.com/query/latest/docs/react/reference/useMutation) for more information on mutation options and behavior.

### Returns

An object containing the following properties, provided by TanStack Query's [useMutation](https://tanstack.com/query/latest/docs/react/reference/useMutation):

- `mutate`:
Function to trigger the mutation. In this case, it deletes the user.

```ts
mutate(user: User): void;
```

- `isPending`:
Boolean indicating if the mutation is in progress (alias for `isLoading`).

- `isSuccess`:
Boolean indicating if the mutation has successfully completed.

- `isError`:
Boolean indicating if the mutation has failed.

- `error`:
The error object, if the mutation failed.

- `data`:
The result of the mutation (typically `undefined` for delete operations).

For a complete list of returned properties, see the official TanStack Query [useMutation](https://tanstack.com/query/latest/docs/react/reference/useMutation) documentation.
89 changes: 88 additions & 1 deletion docs/react/auth/hooks/useReloadMutation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,91 @@
title: useReloadMutation
---

This hook is implemented, but the documentation is a work in progress, check back soon.
The `useReloadMutation` hook provides a simple way to reload user data in Firebase Authentication.
This hook enables re-authenticating users and ensures the latest user state is fetched from Firebase.

## Features

- **Seamless Firebase Integration**: Reloads the current Firebase user to fetch updated user state.
- **Built-in State Management**: Tracks the mutation lifecycle (`idle`, `loading`, `success`, `error`).
- **Customizable Callbacks**: Supports `onSuccess`, `onError`, and other TanStack Query mutation options.
- **Type-Safe Handling**: Ensures that only valid Firebase user objects can be passed.

## Installation

Ensure you have the necessary Firebase, TanStack Query and TanStack Query Firebase packages installed:

```bash
npm install firebase @tanstack/react-query @tanstack-query-firebase/react
```

## Usage

```jsx
import { useReloadMutation } from "@tanstack-query-firebase/react/auth";

function Component() {
const { mutate, isPending, isSuccess, isError, error } = useReloadMutation();

const handleReloadUser = () => {
const user = auth.currentUser;
if (user) {
mutate(user);
}
};

return (
<div>
{isPending && <p>Reloading user data...</p>}
{isSuccess && <p>User data reloaded successfully!</p>}
{isError && <p>Error: {error.message}</p>}

<button onClick={handleReloadUser} disabled={isPending}>
Reload User Data
</button>
</div>
);
}
```

## API Reference

`useReloadMutation(options?)`

### Parameters

- `options` (optional):
An object for customizing the mutation behavior. Accepts all options supported by TanStack Query's `useMutation`, such as:
- `onSuccess`: Callback fired when the mutation succeeds.
- `onError`: Callback fired when the mutation fails.
- `onSettled`: Callback fired after the mutation finishes, regardless of success or failure.

For a full list of supported options, see the TanStack Query [useMutation documentation](https://tanstack.com/query/latest/docs/react/reference/useMutation)

### Returns

The hook returns the following properties, provided by TanStack Query's [useMutation](https://tanstack.com/query/latest/docs/react/reference/useMutation):

- `mutate`:
Function to trigger the mutation. In this case, it reloads the user data.

```ts
mutate(user: User): void;
```

- `isPending`:
Boolean indicating if the mutation is in progress (alias for `isLoading`).

- `isSuccess`:
Boolean indicating if the mutation has successfully completed.

- `isError`:
Boolean indicating if the mutation has failed.

- `error`:
The error object, if the mutation failed.

- `data`:
The result of the mutation (typically `undefined` for reload operations).

For a complete list of returned properties, see the official TanStack Query [useMutation](https://tanstack.com/query/latest/docs/react/reference/useMutation) documentation.
87 changes: 86 additions & 1 deletion docs/react/auth/hooks/useSignInAnonymouslyMutation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,89 @@
title: useSignInAnonymouslyMutation
---

This hook is implemented, but the documentation is a work in progress, check back soon.
The `useSignInAnonymouslyMutation` hook provides a simple way to sign in users anonymously using Firebase Authentication.
This hook integrates Firebase’s `signInAnonymously` method with TanStack Query's mutation lifecycle, allowing seamless state management and error handling.

## Features

- **Firebase Authentication Integration**: Allows users to sign in anonymously with Firebase Authentication.
- **Built-in State Management**: Tracks the mutation lifecycle (`idle`, `loading`, `success`, `error`).
- **Customizable Callbacks**: Supports `onSuccess`, `onError`, and other TanStack Query mutation options.
- **Type-Safe Handling**: Ensures only valid data is passed and received from Firebase Authentication.

## Installation

Ensure you have the necessary Firebase, TanStack Query and TanStack Query Firebase packages installed:

```bash
npm install firebase @tanstack/react-query @tanstack-query-firebase/react
```

## Usage

```jsx
import { useSignInAnonymouslyMutation } from "@tanstack-query-firebase/react/auth";

function Component() {
const { mutate, isPending, isSuccess, isError, error } = useSignInAnonymouslyMutation(auth);

const handleSignIn = () => {
mutate(); // Trigger anonymous sign-in
};

return (
<div>
{isPending && <p>Signing in...</p>}
{isSuccess && <p>Signed in anonymously!</p>}
{isError && <p>Error: {error?.message}</p>}

<button onClick={handleSignIn} disabled={isPending}>
Sign In Anonymously
</button>
</div>
);
}
```

## API Reference

`useSignInAnonymouslyMutation(auth, options?)`

### Parameters

-`auth`: The Firebase `Auth` instance used to manage authentication.
- `options` (optional):
An object for customizing the mutation behavior. Accepts all options supported by TanStack Query's `useMutation`, such as:
- `onSuccess`: Callback fired when the mutation succeeds.
- `onError`: Callback fired when the mutation fails.
- `onSettled`: Callback fired after the mutation finishes, regardless of success or failure.

For a full list of supported options, see the TanStack Query [useMutation documentation](https://tanstack.com/query/latest/docs/react/reference/useMutation)

### Returns

The hook returns the following properties, provided by TanStack Query's [useMutation](https://tanstack.com/query/latest/docs/react/reference/useMutation):

- `mutate`:
Function to trigger the mutation(signin anonymously).

```ts
mutate(): void;
```

- `isPending`:
Boolean indicating if the mutation is in progress (alias for `isLoading`).

- `isSuccess`:
Boolean indicating if the mutation has successfully completed.

- `isError`:
Boolean indicating if the mutation has failed.

- `error`:
The error object, if the mutation failed.

- `data`:
The result of the mutation, typically containing the authenticated user data.

For a complete list of returned properties, see the official TanStack Query [useMutation](https://tanstack.com/query/latest/docs/react/reference/useMutation) documentation.
91 changes: 90 additions & 1 deletion docs/react/auth/hooks/useSignInWithCredentialMutation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,93 @@
title: useSignInWithCredentialMutation
---

This hook is implemented, but the documentation is a work in progress, check back soon.
The `useSignInWithCredentialMutation` hook is designed to handle user sign-ins using Firebase Authentication credentials, such as Google sign-in credentials.
It integrates Firebase's `signInWithCredential` method with TanStack Query’s mutation lifecycle for easy state management and error handling.

## Features

- **Firebase Authentication Integration**: Simplifies signing in with any valid Firebase `AuthCredential` (e.g., Google, Facebook, etc.).
- **Built-in State Management**: Tracks the mutation lifecycle (`idle`, `loading`, `success`, `error`).
- **Customizable Callbacks**: Supports `onSuccess`, `onError`, and other TanStack Query mutation options.
- **Type-Safe Handling**: Ensures type safety with TanStack Query and Firebase types.

## Installation

Ensure you have the necessary Firebase, TanStack Query and TanStack Query Firebase packages installed:

```bash
npm install firebase @tanstack/react-query @tanstack-query-firebase/react
```

## Usage

```jsx
import { useSignInWithCredentialMutation } from "@tanstack-query-firebase/react/auth";
import { GoogleAuthProvider } from "firebase/auth";

const authCredential = GoogleAuthProvider.credential("your-id-token");

function SignInComponent() {
const { mutate, isPending, isSuccess, isError, error, data } = useSignInWithCredentialMutation(auth, authCredential);

const handleSignIn = () => {
mutate(); // Trigger the sign-in mutation
};

return (
<div>
{isPending && <p>Signing in...</p>}
{isSuccess && <p>Signed in as {data?.user.email}</p>}
{isError && <p>Error: {error?.message}</p>}

<button onClick={handleSignIn} disabled={isPending}>
Sign In
</button>
</div>
);
}
```

## API Reference

`useSignInWithCredentialMutation(auth, credential, options?)`

### Parameters

- `auth`: The Firebase `Auth` instance used to manage authentication.
- `credential`: An instance of `AuthCredential` (e.g., `GoogleAuthProvider.credential`).
- `options` (optional):
An object for customizing the mutation behavior. Accepts all options supported by TanStack Query's `useMutation`, such as:
- `onSuccess`: Callback fired when the mutation succeeds.
- `onError`: Callback fired when the mutation fails.
- `onSettled`: Callback fired after the mutation finishes, regardless of success or failure.

For a full list of supported options, see the TanStack Query [useMutation documentation](https://tanstack.com/query/latest/docs/react/reference/useMutation)

### Returns

The hook returns the following properties, provided by TanStack Query's [useMutation](https://tanstack.com/query/latest/docs/react/reference/useMutation):

- `mutate`:
Function to trigger the sign-in mutation with the provided credential.

```ts
mutate(): void;
```

- `isPending`:
Boolean indicating if the mutation is in progress (alias for `isLoading`).

- `isSuccess`:
Boolean indicating if the mutation has successfully completed.

- `isError`:
Boolean indicating if the mutation has failed.

- `error`:
The error object, if the mutation failed.

- `data`:
The result of the sign-in, typically containing the authenticated `user` object.

For a complete list of returned properties, see the official TanStack Query [useMutation](https://tanstack.com/query/latest/docs/react/reference/useMutation) documentation.
Loading
Loading