├── .editorconfig
├── .gitignore
├── .opensource
└── project.json
├── LICENSE
├── README.md
├── auth
├── README.md
├── index.ts
├── package.json
├── types.ts
├── useAuthState.ts
├── useCreateUserWithEmailAndPassword.ts
├── useSendEmailVerification.ts
├── useSendPasswordResetEmail.ts
├── useSignInWithEmailAndPassword.ts
├── useSignInWithPopup.ts
└── useUpdateUser.ts
├── database
├── README.md
├── helpers
│ ├── index.ts
│ └── useListReducer.ts
├── index.ts
├── package.json
├── types.ts
├── useList.ts
└── useObject.ts
├── firestore
├── README.md
├── helpers
│ └── index.ts
├── index.ts
├── package.json
├── types.ts
├── useCollection.ts
└── useDocument.ts
├── functions
├── README.md
├── index.ts
├── package.json
└── useHttpsCallable.ts
├── messaging
├── README.md
├── index.ts
├── package.json
└── useToken.ts
├── package-lock.json
├── package.json
├── prettier.config.js
├── rollup.config.js
├── storage
├── README.md
├── index.ts
├── package.json
├── useDownloadURL.ts
└── useUploadFile.ts
├── tsconfig.json
└── util
├── index.ts
├── package.json
├── refHooks.ts
└── useLoadingValue.ts
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | end_of_line = lf
5 | insert_final_newline = true
6 | trim_trailing_whitespace = true
7 |
8 | charset = utf-8
9 | indent_style = space
10 | indent_size = 2
11 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # NPM / Yarn
2 | .rpt2_cache
3 | node_modules
4 | npm-debug.log
5 |
6 | # Rollup build output
7 | **/dist
8 | **/*.d.ts
9 |
10 | # OS files
11 | .DS_Store
12 |
13 | # Testing output
14 | .nyc_output
15 | coverage
16 |
17 | #test files
18 | /example
19 |
--------------------------------------------------------------------------------
/.opensource/project.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "React Firebase Hooks",
3 | "platforms": ["Web"],
4 | "content": "README.md",
5 | "pages": [],
6 | "related": ["firebase/firebase-js-sdk"],
7 | "tabs": []
8 | }
9 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright 2018 CS Frequency Ltd.
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # React Firebase Hooks
2 |
3 | A set of reusable [React Hooks](https://reactjs.org/docs/hooks-intro.html) for [Firebase](https://firebase.google.com/).
4 |
5 | [](https://www.npmjs.com/package/react-firebase-hooks)
6 | [](https://www.npmjs.com/package/react-firebase-hooks)
7 |
8 | This documentation is for v5 of React Firebase Hooks which requires Firebase v9 or higher.
9 |
10 | - For v4 documentation (Firebase v9), see [here](https://github.com/CSFrequency/react-firebase-hooks/tree/v4.0.2).
11 | - For v3 documentation (Firebase v8), see [here](https://github.com/CSFrequency/react-firebase-hooks/tree/v3.0.4).
12 | - For v2 documentation, see [here](https://github.com/CSFrequency/react-firebase-hooks/tree/v2.2.0).
13 |
14 | ## Installation
15 |
16 | React Firebase Hooks v4 requires **React 16.8.0 or later** and **Firebase v9.0.0 or later**.
17 |
18 | > Whilst previous versions of React Firebase Hooks had some support for React Native Firebase, the underlying changes to v9 of the Firebase Web library have meant this is no longer as straightforward. We will investigate if this is possible in another way as part of a future release.
19 |
20 | ```bash
21 | # with npm
22 | npm install --save react-firebase-hooks
23 |
24 | # with yarn
25 | yarn add react-firebase-hooks
26 | ```
27 |
28 | This assumes that you’re using the [npm](https://npmjs.com) or [yarn](https://yarnpkg.com/) package managers with a module bundler like [Webpack](https://webpack.js.org/) or [Browserify](http://browserify.org/) to consume [CommonJS](http://webpack.github.io/docs/commonjs.html) modules.
29 |
30 | ## Why?
31 |
32 | This library explores how React Hooks can work to make integration with Firebase even more straightforward than it already is. It takes inspiration for naming from RxFire and is based on an internal library that we had been using in a number of apps prior to the release of React Hooks. The implementation with hooks is 10x simpler than our previous implementation.
33 |
34 | ## Upgrading from v4 to v5
35 |
36 | To upgrade your project from v4 to v5 check out the [Release Notes](https://github.com/CSFrequency/react-firebase-hooks/releases/tag/v5.0.0) which have full details of everything that needs to be changed.
37 |
38 | ## Documentation
39 |
40 | - [Authentication Hooks](/auth)
41 | - [Cloud Firestore Hooks](/firestore)
42 | - [Cloud Functions Hooks](/functions)
43 | - [Cloud Messaging Hooks](/messaging)
44 | - [Cloud Storage Hooks](/storage)
45 | - [Realtime Database Hooks](/database)
46 |
47 | ## License
48 |
49 | - See [LICENSE](/LICENSE)
50 |
--------------------------------------------------------------------------------
/auth/README.md:
--------------------------------------------------------------------------------
1 | # React Firebase Hooks - Auth
2 |
3 | React Firebase Hooks provides a convenience listener for Firebase Auth's auth state. The hook wraps around the `auth.onAuthStateChange(...)` method to ensure that it is always up to date.
4 |
5 | All hooks can be imported from `react-firebase-hooks/auth`, e.g.
6 |
7 | ```js
8 | import { useAuthState } from 'react-firebase-hooks/auth';
9 | ```
10 |
11 | List of Auth hooks:
12 |
13 | - [useAuthState](#useauthstate)
14 | - [useCreateUserWithEmailAndPassword](#usecreateuserwithemailandpassword)
15 | - [useSignInWithEmailAndPassword](#usesigninwithemailandpassword)
16 | - [useSignInWithApple](#usesigninwithapple)
17 | - [useSignInWithFacebook](#usesigninwithfacebook)
18 | - [useSignInWithGithub](#usesigninwithgithub)
19 | - [useSignInWithGoogle](#usesigninwithgoogle)
20 | - [useSignInWithMicrosoft](#usesigninwithmicrosoft)
21 | - [useSignInWithTwitter](#usesigninwithtwitter)
22 | - [useSignInWithYahoo](#usesigninwithyahoo)
23 | - [useUpdateEmail](#useupdateemail)
24 | - [useUpdatePassword](#useupdatepassword)
25 | - [useUpdateProfile](#useupdateprofile)
26 | - [useSendPasswordResetEmail](#usesendpasswordresetemail)
27 | - [useSendEmailVerification](#usesendemailverification)
28 |
29 | ### useAuthState
30 |
31 | ```js
32 | const [user, loading, error] = useAuthState(auth, options);
33 | ```
34 |
35 | Retrieve and monitor the authentication state from Firebase.
36 |
37 | The `useAuthState` hook takes the following parameters:
38 |
39 | - `auth`: `auth.Auth` instance for the app you would like to monitor
40 | - `options`: (optional) `Object with the following parameters:
41 | - `onUserChanged`: (optional) function to be called with `auth.User` each time the user changes. This allows you to do things like load custom claims.
42 |
43 | Returns:
44 |
45 | - `user`: The `auth.User` if logged in, or `null` if not
46 | - `loading`: A `boolean` to indicate whether the the authentication state is still being loaded
47 | - `error`: Any `AuthError` returned by Firebase when trying to load the user, or `undefined` if there is no error
48 |
49 | #### If you are registering or signing in the user for the first time consider using [useCreateUserWithEmailAndPassword](#usecreateuserwithemailandpassword), [useSignInWithEmailAndPassword](#usesigninwithemailandpassword)
50 |
51 | #### Full Example
52 |
53 | ```js
54 | import { getAuth, signInWithEmailAndPassword, signOut } from 'firebase/auth';
55 | import { useAuthState } from 'react-firebase-hooks/auth';
56 |
57 | const auth = getAuth(firebaseApp);
58 |
59 | const login = () => {
60 | signInWithEmailAndPassword(auth, 'test@test.com', 'password');
61 | };
62 | const logout = () => {
63 | signOut(auth);
64 | };
65 |
66 | const CurrentUser = () => {
67 | const [user, loading, error] = useAuthState(auth);
68 |
69 | if (loading) {
70 | return (
71 |
89 | );
90 | }
91 | return ;
92 | };
93 | ```
94 |
95 | ### useCreateUserWithEmailAndPassword
96 |
97 | ```js
98 | const [
99 | createUserWithEmailAndPassword,
100 | user,
101 | loading,
102 | error,
103 | ] = useCreateUserWithEmailAndPassword(auth);
104 | ```
105 |
106 | Create a user with email and password. Wraps the underlying `firebase.auth().createUserWithEmailAndPassword` method and provides additional `loading` and `error` information.
107 |
108 | The `useCreateUserWithEmailAndPassword` hook takes the following parameters:
109 |
110 | - `auth`: `auth.Auth` instance for the app you would like to monitor
111 | - `options`: (optional) `Object` with the following parameters:
112 | - `emailVerificationOptions`: (optional) `auth.ActionCodeSettings` to customise the email verification
113 | - `sendEmailVerification`: (optional) `boolean` to trigger sending of an email verification after the user has been created
114 |
115 | Returns:
116 |
117 | - `createUserWithEmailAndPassword(email: string, password: string)`: a function you can call to start the registration
118 | - `user`: The `User` if the user was created or `undefined` if not
119 | - `loading`: A `boolean` to indicate whether the the user creation is processing
120 | - `error`: Any `Error` returned by Firebase when trying to create the user, or `undefined` if there is no error
121 |
122 | #### Full Example
123 |
124 | ```jsx
125 | import { useCreateUserWithEmailAndPassword } from 'react-firebase-hooks/auth';
126 |
127 | const SignIn = () => {
128 | const [email, setEmail] = useState('');
129 | const [password, setPassword] = useState('');
130 | const [
131 | createUserWithEmailAndPassword,
132 | user,
133 | loading,
134 | error,
135 | ] = useCreateUserWithEmailAndPassword(auth);
136 |
137 | if (error) {
138 | return (
139 |
170 | );
171 | };
172 | ```
173 |
174 | ### useSignInWithEmailAndPassword
175 |
176 | ```js
177 | const [
178 | signInWithEmailAndPassword,
179 | user,
180 | loading,
181 | error,
182 | ] = useSignInWithEmailAndPassword(auth);
183 | ```
184 |
185 | Login a user with email and password. Wraps the underlying `auth.signInWithEmailAndPassword` method and provides additional `loading` and `error` information.
186 |
187 | The `useSignInWithEmailAndPassword` hook takes the following parameters:
188 |
189 | - `auth`: `Auth` instance for the app you would like to monitor
190 |
191 | Returns:
192 |
193 | - `signInWithEmailAndPassword(email: string, password: string)`: a function you can call to start the login
194 | - `user`: The `auth.User` if the user was logged in or `undefined` if not
195 | - `loading`: A `boolean` to indicate whether the the user login is processing
196 | - `error`: Any `Error` returned by Firebase when trying to login the user, or `undefined` if there is no error
197 |
198 | #### Full Example
199 |
200 | ```jsx
201 | import { useSignInWithEmailAndPassword } from 'react-firebase-hooks/auth';
202 |
203 | const SignIn = () => {
204 | const [email, setEmail] = useState('');
205 | const [password, setPassword] = useState('');
206 | const [
207 | signInWithEmailAndPassword,
208 | user,
209 | loading,
210 | error,
211 | ] = useSignInWithEmailAndPassword(auth);
212 |
213 | if (error) {
214 | return (
215 |
246 | );
247 | };
248 | ```
249 |
250 | ### useSignInWithApple
251 |
252 | ```js
253 | const [signInWithApple, user, loading, error] = useSignInWithApple(auth);
254 | ```
255 |
256 | Login a user with Apple Authenticatiton. Wraps the underlying `auth.signInWithPopup` method with the `auth.OAuthProvider` and provides additional `loading` and `error` information.
257 |
258 | The `useSignInWithApple` hook takes the following parameters:
259 |
260 | - `auth`: `Auth` instance for the app you would like to monitor
261 |
262 | Returns:
263 |
264 | - `signInWithApple(scopes: string[], customOAuthParameters: auth.CustomParameters)`: a function you can call to start the login
265 | - `user`: The `auth.User` if the user was logged in or `undefined` if not
266 | - `loading`: A `boolean` to indicate whether the the user login is processing
267 | - `error`: Any `Error` returned by Firebase when trying to login the user, or `undefined` if there is no error
268 |
269 | #### Full example
270 |
271 | See [social login example](#social-login-example)
272 |
273 | ### useSignInWithFacebook
274 |
275 | ```js
276 | const [signInWithFacebook, user, loading, error] = useSignInWithFacebook(auth);
277 | ```
278 |
279 | Login a user with Facebook Authenticatiton. Wraps the underlying `auth.signInWithPopup` method with the `auth.OAuthProvider` and provides additional `loading` and `error` information.
280 |
281 | The `useSignInWithApple` hook takes the following parameters:
282 |
283 | - `auth`: `Auth` instance for the app you would like to monitor
284 |
285 | Returns:
286 |
287 | - `signInWithFacebook(scopes: string[], customOAuthParameters: auth.CustomParameters)`: a function you can call to start the login
288 | - `user`: The `auth.User` if the user was logged in or `undefined` if not
289 | - `loading`: A `boolean` to indicate whether the the user login is processing
290 | - `error`: Any `Error` returned by Firebase when trying to login the user, or `undefined` if there is no error
291 |
292 | #### Full example
293 |
294 | See [social login example](#social-login-example)
295 |
296 | ### useSignInWithGithub
297 |
298 | ```js
299 | const [signInWithGithub, user, loading, error] = useSignInWithGithub(auth);
300 | ```
301 |
302 | Login a user with Github Authenticatiton. Wraps the underlying `auth.signInWithPopup` method with the `auth.OAuthProvider` and provides additional `loading` and `error` information.
303 |
304 | The `useSignInWithGithub` hook takes the following parameters:
305 |
306 | - `auth`: `Auth` instance for the app you would like to monitor
307 |
308 | Returns:
309 |
310 | - `signInWithGithub(scopes: string[], customOAuthParameters: auth.CustomParameters)`: a function you can call to start the login
311 | - `user`: The `auth.User` if the user was logged in or `undefined` if not
312 | - `loading`: A `boolean` to indicate whether the the user login is processing
313 | - `error`: Any `Error` returned by Firebase when trying to login the user, or `undefined` if there is no error
314 |
315 | #### Full example
316 |
317 | See [social login example](#social-login-example)
318 |
319 | ### useSignInWithGoogle
320 |
321 | ```js
322 | const [signInWithGoogle, user, loading, error] = useSignInWithGoogle(auth);
323 | ```
324 |
325 | Login a user with Google Authenticatiton. Wraps the underlying `auth.signInWithPopup` method with the `auth.GoogleProvider` and provides additional `loading` and `error` information.
326 |
327 | The `useSignInWithGoogle` hook takes the following parameters:
328 |
329 | - `auth`: `Auth` instance for the app you would like to monitor
330 |
331 | Returns:
332 |
333 | - `signInWithGoogle(scopes: string[], customOAuthParameters: auth.CustomParameters)`: a function you can call to start the login
334 | - `user`: The `auth.User` if the user was logged in or `undefined` if not
335 | - `loading`: A `boolean` to indicate whether the the user login is processing
336 | - `error`: Any `Error` returned by Firebase when trying to login the user, or `undefined` if there is no error
337 |
338 | #### Full example
339 |
340 | See [social login example](#social-login-example)
341 |
342 | ### useSignInWithMicrosoft
343 |
344 | ```js
345 | const [signInWithMicrosoft, user, loading, error] = useSignInWithMicrosoft(
346 | auth
347 | );
348 | ```
349 |
350 | Login a user with Microsoftt Authenticatiton. Wraps the underlying `auth.signInWithPopup` method with the `auth.OAuthProvider` and provides additional `loading` and `error` information.
351 |
352 | The `useSignInWithMicrosoft` hook takes the following parameters:
353 |
354 | - `auth`: `Auth` instance for the app you would like to monitor
355 |
356 | Returns:
357 |
358 | - `signInWithMicrosoft(scopes: string[], customOAuthParameters: auth.CustomParameters)`: a function you can call to start the login
359 | - `user`: The `auth.User` if the user was logged in or `undefined` if not
360 | - `loading`: A `boolean` to indicate whether the the user login is processing
361 | - `error`: Any `Error` returned by Firebase when trying to login the user, or `undefined` if there is no error
362 |
363 | #### Full example
364 |
365 | See [social login example](#social-login-example)
366 |
367 | ### useSignInWithTwittter
368 |
369 | ```js
370 | const [signInWithTwitter, user, loading, error] = useSignInWithTwitter(auth);
371 | ```
372 |
373 | Login a user with Twitter Authenticatiton. Wraps the underlying `auth.signInWithPopup` method with the `auth.OAuthProvider` and provides additional `loading` and `error` information.
374 |
375 | The `useSignInWithTwitter` hook takes the following parameters:
376 |
377 | - `auth`: `Auth` instance for the app you would like to monitor
378 |
379 | Returns:
380 |
381 | - `signInWithTwitter(scopes: string[], customOAuthParameters: auth.CustomParameters)`: a function you can call to start the login
382 | - `user`: The `auth.User` if the user was logged in or `undefined` if not
383 | - `loading`: A `boolean` to indicate whether the the user login is processing
384 | - `error`: Any `Error` returned by Firebase when trying to login the user, or `undefined` if there is no error
385 |
386 | #### Full example
387 |
388 | See [social login example](#social-login-example)
389 |
390 | ### useSignInWithYahoo
391 |
392 | ```js
393 | const [signInWithYahoo, user, loading, error] = useSignInWithYahoo(auth);
394 | ```
395 |
396 | Login a user with Yahoo Authenticatiton. Wraps the underlying `auth.signInWithPopup` method with the `auth.OAuthProvider` and provides additional `loading` and `error` information.
397 |
398 | The `useSignInWithYahoo` hook takes the following parameters:
399 |
400 | - `auth`: `Auth` instance for the app you would like to monitor
401 |
402 | Returns:
403 |
404 | - `signInWithYahoo(scopes: string[], customOAuthParameters: auth.CustomParameters)`: a function you can call to start the login
405 | - `user`: The `auth.User` if the user was logged in or `undefined` if not
406 | - `loading`: A `boolean` to indicate whether the the user login is processing
407 | - `error`: Any `Error` returned by Firebase when trying to login the user, or `undefined` if there is no error
408 |
409 | #### Full example
410 |
411 | See [social login example](#social-login-example)
412 |
413 | ### Social Login Example
414 |
415 | ```jsx
416 | import { useSignInWithXXX } from 'react-firebase-hooks/auth';
417 |
418 | const SignIn = () => {
419 | const [signInWithXXX, user, loading, error] = useSignInWithXXX(auth);
420 |
421 | if (error) {
422 | return (
423 |
452 | );
453 | };
454 | ```
455 |
456 | ### useUpdateEmail
457 |
458 | ```js
459 | const [updateEmail, updating, error] = useUpdateEmail(auth);
460 | ```
461 |
462 | Update the current user's email address. Wraps the underlying `auth.updateEmail` method and provides additional `updating` and `error` information.
463 |
464 | The `useUpdateEmail` hook takes the following parameters:
465 |
466 | - `auth`: `Auth` instance for the app you would like to monitor
467 |
468 | Returns:
469 |
470 | - `updateEmail(email: string)`: a function you can call to update the current user's email addres
471 | - `updating`: A `boolean` to indicate whether the user update is processing
472 | - `error`: Any `Error` returned by Firebase when trying to update the user, or `undefined` if there is no error
473 |
474 | #### Full Example
475 |
476 | ```jsx
477 | import { useUpdateEmail } from 'react-firebase-hooks/auth';
478 |
479 | const UpdateEmail = () => {
480 | const [email, setEmail] = useState('');
481 | const [updateEmail, updating, error] = useUpdateEmail(auth);
482 |
483 | if (error) {
484 | return (
485 |
509 | );
510 | };
511 | ```
512 |
513 | ### useUpdatePassword
514 |
515 | ```js
516 | const [updatePassword, updating, error] = useUpdatePassword(auth);
517 | ```
518 |
519 | Update the current user's password. Wraps the underlying `auth.updatePassword` method and provides additional `updating` and `error` information.
520 |
521 | The `useUpdatePassword` hook takes the following parameters:
522 |
523 | - `auth`: `Auth` instance for the app you would like to monitor
524 |
525 | Returns:
526 |
527 | - `updatePassword(password: string)`: a function you can call to update the current user's password
528 | - `updating`: A `boolean` to indicate whether the user update is processing
529 | - `error`: Any `Error` returned by Firebase when trying to update the user, or `undefined` if there is no error
530 |
531 | #### Full Example
532 |
533 | ```jsx
534 | import { useUpdatePassword } from 'react-firebase-hooks/auth';
535 |
536 | const UpdatePassword = () => {
537 | const [password, setPassword] = useState('');
538 | const [updatePassword, updating, error] = useUpdatePassword(auth);
539 |
540 | if (error) {
541 | return (
542 |
690 | );
691 | };
692 | ```
693 |
694 | ### useSendEmailVerification
695 |
696 | ```js
697 | const [sendEmailVerification, sending, error] = useSendEmailVerification(auth);
698 | ```
699 |
700 | Send a verification email to the current user. Wraps the underlying `auth.sendEmailVerification` method and provides additional `sending` and `error` information.
701 |
702 | The `useSendEmailVerification` hook takes the following parameters:
703 |
704 | - `auth`: `Auth` instance for the app you would like to monitor
705 |
706 | Returns:
707 |
708 | - `sendEmailVerification()`: a function you can call to send a password reset emaail
709 | - `sending`: A `boolean` to indicate whether the email is being sent
710 | - `error`: Any `Error` returned by Firebase when trying to send the email, or `undefined` if there is no error
711 |
712 | #### Full Example
713 |
714 | ```jsx
715 | import { useSendEmailVerification } from 'react-firebase-hooks/auth';
716 |
717 | const SendEmailVerification = () => {
718 | const [email, setEmail] = useState('');
719 | const [sendEmailVerification, sending, error] = useSendEmailVerification(
720 | auth
721 | );
722 |
723 | if (error) {
724 | return (
725 |
78 | );
79 | };
80 | ```
81 |
82 | ### useListKeys
83 |
84 | ```js
85 | const [keys, loading, error] = useListKeys(reference);
86 | ```
87 |
88 | As `useList`, but this hooks extracts the `database.DataSnapshot.key` values, rather than the the `database.DataSnapshot`s themselves.
89 |
90 | The `useListKeys` hook takes the following parameters:
91 |
92 | - `reference`: (optional) `database.Reference` for the data you would like to load
93 |
94 | Returns:
95 |
96 | - `keys`: an array of `string`, or `undefined` if no reference is supplied
97 | - `loading`: a `boolean` to indicate if the data is still being loaded
98 | - `error`: Any `Error` returned by Firebase when trying to load the data, or `undefined` if there is no error
99 |
100 | ### useListVals
101 |
102 | ```js
103 | const [values, loading, error] = useListVals (reference, options);
104 | ```
105 |
106 | As `useList`, but this hook extracts a typed list of the `database.DataSnapshot.val()` values, rather than the the
107 | `database.DataSnapshot`s themselves.
108 |
109 | The `useListVals` hook takes the following parameters:
110 |
111 | - `reference`: (optional) `database.Reference` for the data you would like to load
112 | - `options`: (optional) `Object` with the following parameters:
113 | - `keyField`: (optional) `string` field name that should be populated with the `database.DataSnapshot.id` property in the returned values.
114 | - `refField`: (optional) `string` field name that should be populated with the `database.DataSnapshot.ref` property.
115 | - `transform`: (optional) a function that receives the raw `database.DataSnapshot.val()` for each item in the list to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below.
116 |
117 | Returns:
118 |
119 | - `values`: an array of `T`, or `undefined` if no reference is supplied
120 | - `loading`: a `boolean` to indicate if the data is still being loaded
121 | - `error`: Any `Error` returned by Firebase when trying to load the data, or `undefined` if there is no error
122 |
123 | ### useObject
124 |
125 | ```js
126 | const [snapshot, loading, error] = useObject(reference);
127 | ```
128 |
129 | Retrieve and monitor an object or primitive value in the Firebase Realtime Database.
130 |
131 | The `useObject` hook takes the following parameters:
132 |
133 | - `reference`: (optional) `database.Reference` for the data you would like to load
134 |
135 | Returns:
136 |
137 | - `snapshot`: a `database.DataSnapshot`, or `undefined` if no reference is supplied
138 | - `loading`: a `boolean` to indicate if the data is still being loaded
139 | - `error`: Any `Error` returned by Firebase when trying to load the data, or `undefined` if there is no error
140 |
141 | #### Full Example
142 |
143 | ```js
144 | import { ref, getDatabase } from 'firebase/database';
145 | import { useObject } from 'react-firebase-hooks/database';
146 |
147 | const database = getDatabase(firebaseApp);
148 |
149 | const DatabaseValue = () => {
150 | const [snapshot, loading, error] = useObject(ref(database, 'value'));
151 |
152 | return (
153 |
90 | );
91 | };
92 | ```
93 |
94 | ### useCollectionOnce
95 |
96 | ```js
97 | const [snapshot, loading, error] = useCollectionOnce(query, options);
98 | ```
99 |
100 | Retrieve the current value of the `firestore.Query`.
101 |
102 | The `useCollectionOnce` hook takes the following parameters:
103 |
104 | - `query`: (optional) `firestore.Query` for the data you would like to load
105 | - `options`: (optional) `Object` with the following parameters:
106 | - `getOptions`: (optional) `Object` to customise how the collection is loaded
107 | - `source`: (optional): `'default' | 'server' | 'cache'` Describes whether we should get from server or cache.
108 |
109 | Returns:
110 |
111 | - `snapshot`: a `firestore.QuerySnapshot`, or `undefined` if no query is supplied
112 | - `loading`: a `boolean` to indicate if the data is still being loaded
113 | - `error`: Any `firestore.FirestoreError` returned by Firebase when trying to load the data, or `undefined` if there is no error
114 |
115 | ### useCollectionData
116 |
117 | ```js
118 | const [values, loading, error, snapshot] =
119 | useCollectionData < T > (query, options);
120 | ```
121 |
122 | As `useCollection`, but this hook extracts a typed list of the `firestore.QuerySnapshot.docs` values, rather than the
123 | `firestore.QuerySnapshot` itself.
124 |
125 | The `useCollectionData` hook takes the following parameters:
126 |
127 | - `query`: (optional) `firestore.Query` for the data you would like to load
128 | - `options`: (optional) `Object` with the following parameters:
129 | - `snapshotListenOptions`: (optional) `firestore.SnapshotListenOptions` to customise how the collection is loaded
130 | - `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots
131 |
132 | Returns:
133 |
134 | - `values`: an array of `T`, or `undefined` if no query is supplied
135 | - `loading`: a `boolean` to indicate if the data is still being loaded
136 | - `error`: Any `firestore.FirestoreError` returned by Firebase when trying to load the data, or `undefined` if there is no error
137 | - `snapshot`: a `firestore.QuerySnapshot`, or `undefined` if no query is supplied. This allows access to the underlying snapshot if needed for any reason, e.g. to view the snapshot metadata
138 |
139 | See [Transforming data](#transforming-data) for how to transform data as it leaves Firestore and access the underlying `id` and `ref` fields of the snapshot.
140 |
141 | ### useCollectionDataOnce
142 |
143 | ```js
144 | const [values, loading, error, snapshot] =
145 | useCollectionDataOnce < T > (query, options);
146 | ```
147 |
148 | As `useCollectionData`, but this hook will only read the current value of the `firestore.Query`.
149 |
150 | The `useCollectionDataOnce` hook takes the following parameters:
151 |
152 | - `query`: (optional) `firestore.Query` for the data you would like to load
153 | - `options`: (optional) `Object` with the following parameters:
154 | - `getOptions`: (optional) `Object` to customise how the collection is loaded
155 | - `source`: (optional): `'default' | 'server' | 'cache'` Describes whether we should get from server or cache.
156 | - `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots
157 |
158 | Returns:
159 |
160 | - `values`: an array of `T`, or `undefined` if no query is supplied
161 | - `loading`: a `boolean` to indicate if the data is still being loaded
162 | - `error`: Any `firestore.FirestoreError` returned by Firebase when trying to load the data, or `undefined` if there is no error
163 | - `snapshot`: a `firestore.QuerySnapshot`, or `undefined` if no query is supplied. This allows access to the underlying snapshot if needed for any reason, e.g. to view the snapshot metadata
164 |
165 | See [Transforming data](#transforming-data) for how to transform data as it leaves Firestore and access the underlying `id` and `ref` fields of the snapshot.
166 |
167 | ### useDocument
168 |
169 | ```js
170 | const [snapshot, loading, error] = useDocument(reference, options);
171 | ```
172 |
173 | Retrieve and monitor a document value in Cloud Firestore.
174 |
175 | The `useDocument` hook takes the following parameters:
176 |
177 | - `reference`: (optional) `firestore.DocumentReference` for the data you would like to load
178 | - `options`: (optional) `Object` with the following parameters:
179 | - `snapshotListenOptions`: (optional) `firestore.SnapshotListenOptions` to customise how the query is loaded
180 |
181 | Returns:
182 |
183 | - `snapshot`: a `firestore.DocumentSnapshot`, or `undefined` if no query is supplied
184 | - `loading`: a `boolean` to indicate if the data is still being loaded
185 | - `error`: Any `firestore.FirestoreError` returned by Firebase when trying to load the data, or `undefined` if there is no error
186 |
187 | #### Full example
188 |
189 | ```js
190 | import { getFirestore, doc } from 'firebase/firestore';
191 | import { useDocument } from 'react-firebase-hooks/firestore';
192 |
193 | const FirestoreDocument = () => {
194 | const [value, loading, error] = useDocument(
195 | doc(getFirestore(firebaseApp), 'hooks', 'nBShXiRGFAhuiPfBaGpt'),
196 | {
197 | snapshotListenOptions: { includeMetadataChanges: true },
198 | }
199 | );
200 | return (
201 |