Skip to content

Commit d851cd6

Browse files
authored
feat(react): added react example
feat: added react example
2 parents 6c3fc42 + 65fa04f commit d851cd6

28 files changed

+553
-0
lines changed

examples/react/.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

examples/react/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# React + Vite
2+
3+
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
4+
5+
Currently, two official plugins are available:
6+
7+
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) for Fast Refresh
8+
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
9+
10+
## Expanding the ESLint configuration
11+
12+
If you are developing a production application, we recommend using TypeScript with type-aware lint rules enabled. Check out the [TS template](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts) for information on how to integrate TypeScript and [`typescript-eslint`](https://typescript-eslint.io) in your project.

examples/react/eslint.config.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import js from '@eslint/js'
2+
import globals from 'globals'
3+
import reactHooks from 'eslint-plugin-react-hooks'
4+
import reactRefresh from 'eslint-plugin-react-refresh'
5+
6+
export default [
7+
{ ignores: ['dist'] },
8+
{
9+
files: ['**/*.{js,jsx}'],
10+
languageOptions: {
11+
ecmaVersion: 2020,
12+
globals: globals.browser,
13+
parserOptions: {
14+
ecmaVersion: 'latest',
15+
ecmaFeatures: { jsx: true },
16+
sourceType: 'module',
17+
},
18+
},
19+
plugins: {
20+
'react-hooks': reactHooks,
21+
'react-refresh': reactRefresh,
22+
},
23+
rules: {
24+
...js.configs.recommended.rules,
25+
...reactHooks.configs.recommended.rules,
26+
'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }],
27+
'react-refresh/only-export-components': [
28+
'warn',
29+
{ allowConstantExport: true },
30+
],
31+
},
32+
},
33+
]

examples/react/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + React</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.jsx"></script>
12+
<link rel="stylesheet" href="src/index.css">
13+
</body>
14+
</html>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use client';
2+
3+
import { NavLink } from "react-router";
4+
import { useUser } from "../firebase/hooks";
5+
import { signOut, type User } from "firebase/auth";
6+
import { auth } from "../firebase/clientApp";
7+
8+
export function Header() {
9+
const user = useUser();
10+
11+
async function onSignOut() {
12+
await signOut(auth);
13+
router.push("/sign-in");
14+
}
15+
16+
return (
17+
<header className="border-b border-gray-200">
18+
<div className="max-w-6xl mx-auto h-12 flex items-center">
19+
<div className="font-bold">
20+
<NavLink to="/">FirebaseUI</NavLink>
21+
</div>
22+
<div className="flex-grow flex items-center justify-end">
23+
<ul className="text-sm flex items-center gap-6 *:hover:opacity-75">
24+
{user ? <li><button onClick={onSignOut}>Sign Out</button></li> : <li><NavLink to="/sign-in">Sign In</NavLink></li>}
25+
</ul>
26+
</div>
27+
</div>
28+
</header>
29+
);
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use client";
2+
3+
import { initializeApp, getApps } from "firebase/app";
4+
import { firebaseConfig } from "./config";
5+
import { connectAuthEmulator, getAuth } from "firebase/auth";
6+
import { autoAnonymousLogin, initializeUI } from "@firebase-ui/core";
7+
import { customLanguage, english } from "@firebase-ui/translations";
8+
9+
export const firebaseApp =
10+
getApps().length === 0 ? initializeApp(firebaseConfig) : getApps()[0];
11+
12+
export const auth = getAuth(firebaseApp);
13+
14+
export const ui = initializeUI({
15+
app: firebaseApp,
16+
behaviors: [autoAnonymousLogin()],
17+
translations: [
18+
customLanguage(english.locale, {
19+
labels: {
20+
signIn: "Sign In",
21+
},
22+
prompts: {
23+
signInToAccount: "Sign in to your account",
24+
},
25+
errors: {
26+
invalidEmail: "Please enter a valid email address",
27+
},
28+
}),
29+
],
30+
});
31+
32+
if (import.meta.env.MODE === "development") {
33+
connectAuthEmulator(auth, "http://localhost:9099");
34+
}

examples/react/lib/firebase/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const firebaseConfig = {
2+
apiKey: "AIzaSyAotbJXqnZxg9aAsULFn8MLwp_twtMUl2k",
3+
authDomain: "ff-test-74aeb.firebaseapp.com",
4+
databaseURL:
5+
"https://ff-test-74aeb-default-rtdb.asia-southeast1.firebasedatabase.app",
6+
projectId: "ff-test-74aeb",
7+
storageBucket: "ff-test-74aeb.appspot.com",
8+
messagingSenderId: "950537677105",
9+
appId: "1:950537677105:web:da72ccc1718279f3cde810",
10+
measurementId: "G-B5Y2YD83TJ",
11+
};

examples/react/lib/firebase/hooks.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { useState } from "react";
2+
3+
import { onAuthStateChanged } from "firebase/auth";
4+
import { User } from "firebase/auth";
5+
import { useEffect } from "react";
6+
import { auth } from "./clientApp";
7+
8+
export function useUser(initalUser?: User | null) {
9+
const [user, setUser] = useState<User | null>(initalUser ?? null);
10+
11+
useEffect(() => {
12+
const unsubscribe = onAuthStateChanged(auth, setUser);
13+
return () => unsubscribe();
14+
}, []);
15+
16+
return user;
17+
}

examples/react/lib/firebase/ui.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use client";
2+
3+
import { ui } from "./clientApp";
4+
import { ConfigProvider } from "@firebase-ui/react";
5+
6+
export function FirebaseUIProvider({
7+
children,
8+
}: {
9+
children: React.ReactNode;
10+
}) {
11+
return (
12+
<ConfigProvider
13+
ui={ui}
14+
policies={{
15+
termsOfServiceUrl: "https://www.google.com",
16+
privacyPolicyUrl: "https://www.google.com",
17+
}}
18+
>
19+
{children}
20+
</ConfigProvider>
21+
);
22+
}

examples/react/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "react",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vite build",
9+
"lint": "eslint .",
10+
"preview": "vite preview"
11+
},
12+
"dependencies": {
13+
"@firebase-ui/core": "workspace:*",
14+
"@firebase-ui/react": "workspace:*",
15+
"@firebase-ui/styles": "workspace:*",
16+
"@firebase-ui/translations": "workspace:*",
17+
"firebase": "^11.6.0",
18+
"react": "^19.0.0",
19+
"react-dom": "^19.0.0",
20+
"react-router": "^7.5.1"
21+
},
22+
"devDependencies": {
23+
"@tailwindcss/vite": "^4.1.4",
24+
"@eslint/js": "^9.22.0",
25+
"@types/react": "^19.0.10",
26+
"@types/react-dom": "^19.0.4",
27+
"@vitejs/plugin-react": "^4.3.4",
28+
"eslint": "^9.22.0",
29+
"eslint-plugin-react-hooks": "^5.2.0",
30+
"eslint-plugin-react-refresh": "^0.4.19",
31+
"globals": "^16.0.0",
32+
"vite": "^6.3.1",
33+
"tailwindcss": "^4.1.4"
34+
}
35+
}

examples/react/public/vite.svg

Lines changed: 1 addition & 0 deletions
Loading

examples/react/src/App.css

Whitespace-only changes.

examples/react/src/App.jsx

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { NavLink } from "react-router";
2+
import { useUser } from "../lib/firebase/hooks";
3+
4+
function App() {
5+
const user = useUser();
6+
7+
return (
8+
<div className="p-8 ">
9+
<h1 className="text-3xl font-bold mb-6">Firebase UI Demo</h1>
10+
<div className="mb-6">
11+
{user && <div>Welcome: {user.email || user.phoneNumber}</div>}
12+
</div>
13+
<div>
14+
<h2 className="text-2xl font-bold mb-4">Auth Screens</h2>
15+
<ul className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4">
16+
<li>
17+
<NavLink
18+
to="/screens/sign-in-auth-screen"
19+
className="text-blue-500 hover:underline"
20+
>
21+
Sign In Auth Screen
22+
</NavLink>
23+
</li>
24+
<li>
25+
<NavLink
26+
to="/screens/sign-in-auth-screen-w-handlers"
27+
className="text-blue-500 hover:underline"
28+
>
29+
Sign In Auth Screen with Handlers
30+
</NavLink>
31+
</li>
32+
<li>
33+
<NavLink
34+
to="/screens/sign-in-auth-screen-w-oauth"
35+
className="text-blue-500 hover:underline"
36+
>
37+
Sign In Auth Screen with OAuth
38+
</NavLink>
39+
</li>
40+
<li>
41+
<NavLink
42+
to="/screens/email-link-auth-screen"
43+
className="text-blue-500 hover:underline"
44+
>
45+
Email Link Auth Screen
46+
</NavLink>
47+
</li>
48+
<li>
49+
<NavLink
50+
to="/screens/email-link-auth-screen-w-oauth"
51+
className="text-blue-500 hover:underline"
52+
>
53+
Email Link Auth Screen with OAuth
54+
</NavLink>
55+
</li>
56+
<li>
57+
<NavLink
58+
to="/screens/phone-auth-screen"
59+
className="text-blue-500 hover:underline"
60+
>
61+
Phone Auth Screen
62+
</NavLink>
63+
</li>
64+
<li>
65+
<NavLink
66+
to="/screens/phone-auth-screen-w-oauth"
67+
className="text-blue-500 hover:underline"
68+
>
69+
Phone Auth Screen with OAuth
70+
</NavLink>
71+
</li>
72+
<li>
73+
<NavLink
74+
to="/screens/sign-up-auth-screen"
75+
className="text-blue-500 hover:underline"
76+
>
77+
Sign Up Auth Screen
78+
</NavLink>
79+
</li>
80+
<li>
81+
<NavLink
82+
to="/screens/sign-up-auth-screen-w-oauth"
83+
className="text-blue-500 hover:underline"
84+
>
85+
Sign Up Auth Screen with OAuth
86+
</NavLink>
87+
</li>
88+
<li>
89+
<NavLink
90+
to="/screens/oauth-screen"
91+
className="text-blue-500 hover:underline"
92+
>
93+
OAuth Screen
94+
</NavLink>
95+
</li>
96+
<li>
97+
<NavLink
98+
to="/screens/password-reset-screen"
99+
className="text-blue-500 hover:underline"
100+
>
101+
Password Reset Screen
102+
</NavLink>
103+
</li>
104+
</ul>
105+
</div>
106+
</div>
107+
);
108+
}
109+
110+
export default App;

examples/react/src/assets/react.svg

Lines changed: 1 addition & 0 deletions
Loading

examples/react/src/index.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import "tailwindcss";
2+
@import "@firebase-ui/styles/src/base.css";
3+
4+
/* @import "@firebase-ui/styles/src/themes/dark.css"; */
5+
/* @import "@firebase-ui/styles/src/themes/brutalist.css"; */

0 commit comments

Comments
 (0)