Skip to content

Commit 0f2f827

Browse files
authored
Merge pull request #174 from andipaetzold/firebase9
Firebase 9
2 parents ef58420 + 424ea07 commit 0f2f827

27 files changed

+1238
-800
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ A set of reusable [React Hooks](https://reactjs.org/docs/hooks-intro.html) for [
55
[![npm version](https://img.shields.io/npm/v/react-firebase-hooks.svg?style=flat-square)](https://www.npmjs.com/package/react-firebase-hooks)
66
[![npm downloads](https://img.shields.io/npm/dm/react-firebase-hooks.svg?style=flat-square)](https://www.npmjs.com/package/react-firebase-hooks)
77

8-
**This documentation is for v3 of React Firebase Hooks which involved a number of breaking changes, including adding support for Firebase v8.0.0 - more details [here](https://github.com/CSFrequency/react-firebase-hooks/releases/tag/v3.0.0). For v2 documentation, see [here](https://github.com/CSFrequency/react-firebase-hooks/tree/v2.2.0).**
8+
This documentation is for v4 of React Firebase Hooks which makes the package compatible with Firebase v9 and drops support for previous versions of Firebase - more details [here](https://github.com/CSFrequency/react-firebase-hooks/releases/tag/v4.0.0).
9+
10+
- For v3 documentation (Firebase v8), see [here](https://github.com/CSFrequency/react-firebase-hooks/tree/v3.0.0).
11+
- For v2 documentation, see [here](https://github.com/CSFrequency/react-firebase-hooks/tree/v2.2.0).
912

1013
## Installation
1114

12-
React Firebase Hooks v3 requires **React 16.8.0 or later** and **Firebase v8.0.0 or later**.
15+
React Firebase Hooks v4 requires **React 16.8.0 or later** and **Firebase v9.0.0 or later**.
1316

1417
> Official support for Hooks was added to React Native in v0.59.0. React Firebase Hooks works with both the Firebase JS SDK and React Native Firebase, although some of the typings may be incorrect.
1518

auth/README.md

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# React Firebase Hooks - Auth
22

3-
React Firebase Hooks provides a convenience listener for Firebase Auth's auth state. The hook wraps around the `firebase.auth().onAuthStateChange()` method to ensure that it is always up to date.
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.
44

55
All hooks can be imported from `react-firebase-hooks/auth`, e.g.
66

@@ -24,30 +24,33 @@ Retrieve and monitor the authentication state from Firebase.
2424

2525
The `useAuthState` hook takes the following parameters:
2626

27-
- `auth`: `firebase.auth.Auth` instance for the app you would like to monitor
27+
- `auth`: `auth.Auth` instance for the app you would like to monitor
2828

2929
Returns:
3030

31-
- `user`: The `firebase.User` if logged in, or `undefined` if not
31+
- `user`: The `auth.User` if logged in, or `undefined` if not
3232
- `loading`: A `boolean` to indicate whether the the authentication state is still being loaded
33-
- `error`: Any `firebase.auth.Error` returned by Firebase when trying to load the user, or `undefined` if there is no error
33+
- `error`: Any `AuthError` returned by Firebase when trying to load the user, or `undefined` if there is no error
3434

3535
#### If you are registering or signing in the user for the first time consider using [useCreateUserWithEmailAndPassword](#usecreateuserwithemailandpassword), [useSignInWithEmailAndPassword](#usesigninwithemailandpassword)
3636

3737
#### Full Example
3838

3939
```js
40+
import { getAuth, signInWithEmailAndPassword, signOut } from 'firebase/auth';
4041
import { useAuthState } from 'react-firebase-hooks/auth';
4142

43+
const auth = getAuth(firebaseApp)
44+
4245
const login = () => {
43-
firebase.auth().signInWithEmailAndPassword('[email protected]', 'password');
46+
signInWithEmailAndPassword(auth, '[email protected]', 'password');
4447
};
4548
const logout = () => {
46-
firebase.auth().signOut();
49+
signOut(auth)
4750
};
4851

4952
const CurrentUser = () => {
50-
const [user, loading, error] = useAuthState(firebase.auth());
53+
const [user, loading, error] = useAuthState(auth);
5154

5255
if (loading) {
5356
return (
@@ -90,17 +93,17 @@ Create a user with email and password. Wraps the underlying `firebase.auth().cre
9093

9194
The `useCreateUserWithEmailAndPassword` hook takes the following parameters:
9295

93-
- `auth`: `firebase.auth.Auth` instance for the app you would like to monitor
96+
- `auth`: `auth.Auth` instance for the app you would like to monitor
9497
- `options`: (optional) `Object` with the following parameters:
95-
- `emailVerificationOptions`: (optional) `firebase.auth.ActionCodeSettings` to customise the email verification
98+
- `emailVerificationOptions`: (optional) `auth.ActionCodeSettings` to customise the email verification
9699
- `sendEmailVerification`: (optional) `boolean` to trigger sending of an email verification after the user has been created
97100

98101
Returns:
99102

100103
- `createUserWithEmailAndPassword(email: string, password: string)`: a function you can call to start the registration
101-
- `user`: The `firebase.User` if the user was created or `undefined` if not
104+
- `user`: The `User` if the user was created or `undefined` if not
102105
- `loading`: A `boolean` to indicate whether the the user creation is processing
103-
- `error`: Any `firebase.auth.Error` returned by Firebase when trying to create the user, or `undefined` if there is no error
106+
- `error`: Any `Error` returned by Firebase when trying to create the user, or `undefined` if there is no error
104107

105108
#### Full Example
106109

@@ -165,18 +168,18 @@ const [
165168
] = useSignInWithEmailAndPassword(auth, email, password);
166169
```
167170

168-
Login a user with email and password. Wraps the underlying `firebase.auth().signInWithEmailAndPassword` method and provides additional `loading` and `error` information.
171+
Login a user with email and password. Wraps the underlying `auth.signInWithEmailAndPassword` method and provides additional `loading` and `error` information.
169172

170173
The `useSignInWithEmailAndPassword` hook takes the following parameters:
171174

172-
- `auth`: `firebase.auth.Auth` instance for the app you would like to monitor
175+
- `auth`: `Auth` instance for the app you would like to monitor
173176

174177
Returns:
175178

176179
- `signInWithEmailAndPassword(email: string, password: string)`: a function you can call to start the login
177-
- `user`: The `firebase.User` if the user was logged in or `undefined` if not
180+
- `user`: The `auth.User` if the user was logged in or `undefined` if not
178181
- `loading`: A `boolean` to indicate whether the the user login is processing
179-
- `error`: Any `firebase.auth.Error` returned by Firebase when trying to login the user, or `undefined` if there is no error
182+
- `error`: Any `Error` returned by Firebase when trying to login the user, or `undefined` if there is no error
180183

181184
#### Full Example
182185

auth/index.js.flow

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// @flow
2-
import type { FirebaseUser as User } from 'firebase';
3-
import type { Auth } from 'firebase/auth';
4-
import typeof { Error as AuthError } from 'firebase/auth';
2+
import type { Auth, AuthError, User } from 'firebase/auth';
53

64
type LoadingHook<T> = [T | void, boolean, AuthError | void];
75

auth/types.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import firebase from 'firebase/app';
1+
import { ActionCodeSettings, AuthError, UserCredential } from 'firebase/auth';
22

33
export type AuthActionHook<T, E> = [
44
(email: string, password: string) => void,
@@ -7,10 +7,10 @@ export type AuthActionHook<T, E> = [
77
E | undefined
88
];
99
export type CreateUserOptions = {
10-
emailVerificationOptions?: firebase.auth.ActionCodeSettings;
10+
emailVerificationOptions?: ActionCodeSettings;
1111
sendEmailVerification?: boolean;
1212
};
1313
export type EmailAndPasswordActionHook = AuthActionHook<
14-
firebase.auth.UserCredential,
15-
firebase.FirebaseError
14+
UserCredential,
15+
AuthError
1616
>;

auth/useAuthState.ts

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
import firebase from 'firebase/app';
1+
import { Auth, onAuthStateChanged, User } from 'firebase/auth';
22
import { useEffect, useMemo } from 'react';
33
import { LoadingHook, useLoadingValue } from '../util';
44

5-
export type AuthStateHook = LoadingHook<
6-
firebase.User | null,
7-
firebase.auth.Error
8-
>;
5+
export type AuthStateHook = LoadingHook<User | null, Error>;
96

10-
export default (auth: firebase.auth.Auth): AuthStateHook => {
7+
export default (auth: Auth): AuthStateHook => {
118
const { error, loading, setError, setValue, value } = useLoadingValue<
12-
firebase.User | null,
13-
firebase.auth.Error
9+
User | null,
10+
Error
1411
>(() => auth.currentUser);
1512

1613
useEffect(() => {
17-
const listener = auth.onAuthStateChanged(setValue, setError);
18-
19-
return () => {
20-
listener();
21-
};
14+
return onAuthStateChanged(auth, setValue, setError);
2215
}, [auth]);
2316

2417
const resArray: AuthStateHook = [value, loading, error];

auth/useCreateUserWithEmailAndPassword.ts

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
import {
2+
Auth,
3+
UserCredential,
4+
createUserWithEmailAndPassword as firebaseCreateUserWithEmailAndPassword,
5+
sendEmailVerification,
6+
AuthError,
7+
} from 'firebase/auth';
18
import { useState, useMemo } from 'react';
2-
import firebase from 'firebase/app';
39
import { CreateUserOptions, EmailAndPasswordActionHook } from './types';
410

511
export default (
6-
auth: firebase.auth.Auth,
12+
auth: Auth,
713
options?: CreateUserOptions
814
): EmailAndPasswordActionHook => {
9-
const [error, setError] = useState<firebase.FirebaseError>();
10-
const [
11-
registeredUser,
12-
setRegisteredUser,
13-
] = useState<firebase.auth.UserCredential>();
15+
const [error, setError] = useState<AuthError>();
16+
const [registeredUser, setRegisteredUser] = useState<UserCredential>();
1417
const [loading, setLoading] = useState<boolean>(false);
1518

1619
const createUserWithEmailAndPassword = async (
@@ -19,14 +22,21 @@ export default (
1922
) => {
2023
setLoading(true);
2124
try {
22-
const user = await auth.createUserWithEmailAndPassword(email, password);
25+
const user = await firebaseCreateUserWithEmailAndPassword(
26+
auth,
27+
email,
28+
password
29+
);
2330
if (options && options.sendEmailVerification && user.user) {
24-
await user.user.sendEmailVerification(options.emailVerificationOptions);
31+
await sendEmailVerification(
32+
user.user,
33+
options.emailVerificationOptions
34+
);
2535
}
2636
setRegisteredUser(user);
2737
setLoading(false);
2838
} catch (error) {
29-
setError(error);
39+
setError(error as AuthError);
3040
setLoading(false);
3141
}
3242
};

auth/useSignInWithEmailAndPassword.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
import {
2+
Auth,
3+
UserCredential,
4+
signInWithEmailAndPassword as firebaseSignInWithEmailAndPassword,
5+
AuthError,
6+
} from 'firebase/auth';
17
import { useState, useMemo } from 'react';
2-
import firebase from 'firebase/app';
38
import { EmailAndPasswordActionHook } from './types';
49

5-
export default (auth: firebase.auth.Auth): EmailAndPasswordActionHook => {
6-
const [error, setError] = useState<firebase.FirebaseError>();
7-
const [
8-
loggedInUser,
9-
setLoggedInUser,
10-
] = useState<firebase.auth.UserCredential>();
10+
export default (auth: Auth): EmailAndPasswordActionHook => {
11+
const [error, setError] = useState<AuthError>();
12+
const [loggedInUser, setLoggedInUser] = useState<UserCredential>();
1113
const [loading, setLoading] = useState<boolean>(false);
1214

1315
const signInWithEmailAndPassword = async (
@@ -16,11 +18,15 @@ export default (auth: firebase.auth.Auth): EmailAndPasswordActionHook => {
1618
) => {
1719
setLoading(true);
1820
try {
19-
const user = await auth.signInWithEmailAndPassword(email, password);
21+
const user = await firebaseSignInWithEmailAndPassword(
22+
auth,
23+
email,
24+
password
25+
);
2026
setLoggedInUser(user);
2127
setLoading(false);
2228
} catch (err) {
23-
setError(err);
29+
setError(err as AuthError);
2430
setLoading(false);
2531
}
2632
};

0 commit comments

Comments
 (0)