|
| 1 | +# Firebase UI React |
| 2 | + |
| 3 | +This document provides context for the `@firebase-ui/react` package. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The `@firebase-ui/react` package provides a set of React components and hooks to integrate Firebase UI for Web into a React application. It builds on top of `@firebase-ui/core` and `@firebase-ui/styles` to provide a seamless integration with the React ecosystem. |
| 8 | + |
| 9 | +The package offers two main ways to build your UI: |
| 10 | + |
| 11 | +1. **Pre-built Components**: A set of ready-to-use components for common authentication screens (e.g., Sign In, Register). |
| 12 | +2. **Hooks**: A collection of React hooks that provide access to the underlying UI state and authentication logic, allowing you to build fully custom UIs. |
| 13 | + |
| 14 | +## Setup |
| 15 | + |
| 16 | +To use the React package, you must first initialize Firebase UI using `initializeUI` from the core package, and then wrap your application with the `FirebaseUIProvider`. |
| 17 | + |
| 18 | +```tsx |
| 19 | +// In your main App.tsx or a similar entry point |
| 20 | + |
| 21 | +import { initializeUI } from "@firebase-ui/core"; |
| 22 | +import { enUs } from "@firebase-ui/translations"; |
| 23 | +import { FirebaseUIProvider } from "@firebase-ui/react"; |
| 24 | +import { firebaseApp } from "./firebase"; // Your firebase config |
| 25 | + |
| 26 | +// 1. Initialize the UI |
| 27 | +const ui = initializeUI({ |
| 28 | + app: firebaseApp, |
| 29 | + locale: enUs, |
| 30 | + // ... other configurations |
| 31 | +}); |
| 32 | + |
| 33 | +function App() { |
| 34 | + // 2. Wrap your app in the provider |
| 35 | + return ( |
| 36 | + <FirebaseUIProvider ui={ui}> |
| 37 | + {/* Your application components */} |
| 38 | + <SignInScreen /> |
| 39 | + </FirebaseUIProvider> |
| 40 | + ); |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## Pre-built Components |
| 45 | + |
| 46 | +The package includes several pre-built "screen" components for a quick setup. These components render a full-page authentication form. |
| 47 | + |
| 48 | +**Example: Sign-In Screen** |
| 49 | + |
| 50 | +```tsx |
| 51 | +import { SignInScreen } from "@firebase-ui/react"; |
| 52 | + |
| 53 | +function MySignInPage() { |
| 54 | + return <SignInScreen />; |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +Other available components include `RegisterScreen`, `ForgotPasswordScreen`, etc. |
| 59 | + |
| 60 | +## Hooks |
| 61 | + |
| 62 | +Hooks are the recommended way to build a custom user interface. |
| 63 | + |
| 64 | +### `useUI()` |
| 65 | + |
| 66 | +The main hook is `useUI()`. It returns the entire UI state object from the underlying `nanostores` store. This gives you access to the current `state` (`idle`, `pending`, `error`), any `error` messages, and the `auth` instance. |
| 67 | + |
| 68 | +**Example: Custom Button** |
| 69 | + |
| 70 | +```tsx |
| 71 | +import { useUI } from "@firebase-ui/react"; |
| 72 | +import { signInWithEmailAndPassword } from "@firebase-ui/core"; |
| 73 | + |
| 74 | +function CustomSignInButton() { |
| 75 | + const ui = useUI(); |
| 76 | + |
| 77 | + const handleClick = () => { |
| 78 | + // Functions from @firebase-ui/core require the `ui` instance |
| 79 | + signInWithEmailAndPassword( ui, "[email protected]", "password"); |
| 80 | + }; |
| 81 | + |
| 82 | + return ( |
| 83 | + <button onClick={handleClick} disabled={ui.state === "pending"}> |
| 84 | + {ui.state === "pending" ? "Signing in..." : "Sign In"} |
| 85 | + </button> |
| 86 | + ); |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +### Other Hooks |
| 91 | + |
| 92 | +The package also provides other specialized hooks: |
| 93 | + |
| 94 | +- `useSignInAuthFormSchema()`: Returns a Zod schema for sign-in form validation. |
| 95 | +- `useSignUpAuthFormSchema()`: Returns a Zod schema for sign-up form validation. |
| 96 | +- `useRecaptchaVerifier()`: A hook to easily integrate a reCAPTCHA verifier. |
0 commit comments