react-query-firebase 1.2.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/auth/useIdToken.js +16 -0
- package/dist/src/firestore/index.d.ts +1 -0
- package/dist/src/firestore/index.js +1 -0
- package/dist/src/firestore/useGetRealtimeDocData.d.ts +48 -0
- package/dist/src/firestore/useGetRealtimeDocData.js +41 -0
- package/package.json +1 -1
- package/src/auth/useIdToken.ts +18 -0
- package/src/firestore/index.ts +1 -0
- package/src/firestore/useGetRealtimeDocData.ts +76 -0
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { useCallback, useEffect, useState } from "react";
|
|
2
2
|
import { useCurrentUser } from "./useCurrentUser";
|
|
3
|
+
import { onIdTokenChanged } from "firebase/auth";
|
|
4
|
+
import { useAuth } from "./useAuth";
|
|
3
5
|
/**
|
|
4
6
|
* Custom hook to manage an ID token for the current user.
|
|
5
7
|
* This hook provides an `idToken` value and a `refresh` function to manually refresh the token.
|
|
@@ -9,6 +11,7 @@ import { useCurrentUser } from "./useCurrentUser";
|
|
|
9
11
|
* @returns {Function} refresh - A function to refresh the ID token.
|
|
10
12
|
*/
|
|
11
13
|
export const useIdToken = () => {
|
|
14
|
+
const auth = useAuth();
|
|
12
15
|
const currentUser = useCurrentUser();
|
|
13
16
|
const [idToken, setIdToken] = useState("");
|
|
14
17
|
const callback = useCallback(async () => {
|
|
@@ -29,6 +32,19 @@ export const useIdToken = () => {
|
|
|
29
32
|
useEffect(() => {
|
|
30
33
|
callback();
|
|
31
34
|
}, [currentUser?.uid ?? ""]);
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
const unsubscribe = onIdTokenChanged(auth, (user) => {
|
|
37
|
+
if (user) {
|
|
38
|
+
user.getIdToken().then((idToken) => {
|
|
39
|
+
setIdToken(idToken);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
setIdToken("");
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
return () => unsubscribe();
|
|
47
|
+
}, [idToken]);
|
|
32
48
|
return {
|
|
33
49
|
idToken,
|
|
34
50
|
refresh
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
|
|
2
|
+
import { FirebaseError } from "firebase/app";
|
|
3
|
+
/**
|
|
4
|
+
* @inline
|
|
5
|
+
*/
|
|
6
|
+
export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
|
|
7
|
+
/**
|
|
8
|
+
* A slash-separated path to a document. Has to be omitted to use
|
|
9
|
+
*/
|
|
10
|
+
path?: string;
|
|
11
|
+
/**
|
|
12
|
+
* A reference to a collection.
|
|
13
|
+
*/
|
|
14
|
+
reference: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
15
|
+
/**
|
|
16
|
+
* Additional path segments that will be applied relative
|
|
17
|
+
* to the first argument.
|
|
18
|
+
*/
|
|
19
|
+
pathSegments?: string[];
|
|
20
|
+
/**
|
|
21
|
+
* A callback to be called if the listen fails or is
|
|
22
|
+
* cancelled. No further callbacks will occur.
|
|
23
|
+
*/
|
|
24
|
+
onError: (error: FirebaseError) => unknown;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* A hook to get realtime updates to a firestore document.
|
|
28
|
+
*
|
|
29
|
+
* @group Hook
|
|
30
|
+
*
|
|
31
|
+
* @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
|
|
32
|
+
*
|
|
33
|
+
* @returns {AppModelType | null}
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```jsx
|
|
37
|
+
* const firebaseConfig = {};
|
|
38
|
+
* export const MyComponent = () => {
|
|
39
|
+
* const doc = useGetRealtimeDocData('collection/documentId');
|
|
40
|
+
* return (
|
|
41
|
+
* <div>
|
|
42
|
+
* {JSON.stringify(doc)}
|
|
43
|
+
* </div>
|
|
44
|
+
* );
|
|
45
|
+
* };
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare const useGetRealtimeDocData: <AppModelType, DbModelType extends DocumentData = DocumentData>({ path, pathSegments, reference, onError }: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>) => AppModelType | null;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { onSnapshot } from "firebase/firestore";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { useDocReference } from "./useDocReference";
|
|
4
|
+
/**
|
|
5
|
+
* A hook to get realtime updates to a firestore document.
|
|
6
|
+
*
|
|
7
|
+
* @group Hook
|
|
8
|
+
*
|
|
9
|
+
* @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
|
|
10
|
+
*
|
|
11
|
+
* @returns {AppModelType | null}
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```jsx
|
|
15
|
+
* const firebaseConfig = {};
|
|
16
|
+
* export const MyComponent = () => {
|
|
17
|
+
* const doc = useGetRealtimeDocData('collection/documentId');
|
|
18
|
+
* return (
|
|
19
|
+
* <div>
|
|
20
|
+
* {JSON.stringify(doc)}
|
|
21
|
+
* </div>
|
|
22
|
+
* );
|
|
23
|
+
* };
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export const useGetRealtimeDocData = ({ path, pathSegments, reference, onError }) => {
|
|
27
|
+
const ref = useDocReference({ path, reference, pathSegments });
|
|
28
|
+
const [doc, setDoc] = useState(null);
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
const unsubscribe = ref
|
|
31
|
+
? onSnapshot(ref, {
|
|
32
|
+
next: async (snapshot) => {
|
|
33
|
+
setDoc(snapshot.data() || null);
|
|
34
|
+
},
|
|
35
|
+
error: onError
|
|
36
|
+
})
|
|
37
|
+
: () => { };
|
|
38
|
+
return () => unsubscribe();
|
|
39
|
+
}, [ref, doc]);
|
|
40
|
+
return doc;
|
|
41
|
+
};
|
package/package.json
CHANGED
package/src/auth/useIdToken.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { useCallback, useEffect, useState } from "react";
|
|
2
2
|
import { useCurrentUser } from "./useCurrentUser";
|
|
3
|
+
import { onIdTokenChanged } from "firebase/auth";
|
|
4
|
+
import { useAuth } from "./useAuth";
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Custom hook to manage an ID token for the current user.
|
|
@@ -10,6 +12,8 @@ import { useCurrentUser } from "./useCurrentUser";
|
|
|
10
12
|
* @returns {Function} refresh - A function to refresh the ID token.
|
|
11
13
|
*/
|
|
12
14
|
export const useIdToken = () => {
|
|
15
|
+
const auth = useAuth();
|
|
16
|
+
|
|
13
17
|
const currentUser = useCurrentUser();
|
|
14
18
|
const [idToken, setIdToken] = useState("");
|
|
15
19
|
|
|
@@ -36,6 +40,20 @@ export const useIdToken = () => {
|
|
|
36
40
|
callback();
|
|
37
41
|
}, [currentUser?.uid ?? ""]);
|
|
38
42
|
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
const unsubscribe = onIdTokenChanged(auth, (user) => {
|
|
45
|
+
if (user) {
|
|
46
|
+
user.getIdToken().then((idToken) => {
|
|
47
|
+
setIdToken(idToken);
|
|
48
|
+
});
|
|
49
|
+
} else {
|
|
50
|
+
setIdToken("");
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
return () => unsubscribe();
|
|
55
|
+
}, [idToken]);
|
|
56
|
+
|
|
39
57
|
return {
|
|
40
58
|
idToken,
|
|
41
59
|
refresh
|
package/src/firestore/index.ts
CHANGED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { CollectionReference, DocumentData, DocumentReference, onSnapshot } from "firebase/firestore";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState } from "react";
|
|
4
|
+
import { FirebaseError } from "firebase/app";
|
|
5
|
+
import { useDocReference } from "./useDocReference";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @inline
|
|
9
|
+
*/
|
|
10
|
+
export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
|
|
11
|
+
/**
|
|
12
|
+
* A slash-separated path to a document. Has to be omitted to use
|
|
13
|
+
*/
|
|
14
|
+
path?: string;
|
|
15
|
+
/**
|
|
16
|
+
* A reference to a collection.
|
|
17
|
+
*/
|
|
18
|
+
reference: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
19
|
+
/**
|
|
20
|
+
* Additional path segments that will be applied relative
|
|
21
|
+
* to the first argument.
|
|
22
|
+
*/
|
|
23
|
+
pathSegments?: string[];
|
|
24
|
+
/**
|
|
25
|
+
* A callback to be called if the listen fails or is
|
|
26
|
+
* cancelled. No further callbacks will occur.
|
|
27
|
+
*/
|
|
28
|
+
onError: (error: FirebaseError) => unknown;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A hook to get realtime updates to a firestore document.
|
|
33
|
+
*
|
|
34
|
+
* @group Hook
|
|
35
|
+
*
|
|
36
|
+
* @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
|
|
37
|
+
*
|
|
38
|
+
* @returns {AppModelType | null}
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```jsx
|
|
42
|
+
* const firebaseConfig = {};
|
|
43
|
+
* export const MyComponent = () => {
|
|
44
|
+
* const doc = useGetRealtimeDocData('collection/documentId');
|
|
45
|
+
* return (
|
|
46
|
+
* <div>
|
|
47
|
+
* {JSON.stringify(doc)}
|
|
48
|
+
* </div>
|
|
49
|
+
* );
|
|
50
|
+
* };
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export const useGetRealtimeDocData = <AppModelType, DbModelType extends DocumentData = DocumentData>({
|
|
54
|
+
path,
|
|
55
|
+
pathSegments,
|
|
56
|
+
reference,
|
|
57
|
+
onError
|
|
58
|
+
}: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>): AppModelType | null => {
|
|
59
|
+
const ref = useDocReference({ path, reference, pathSegments });
|
|
60
|
+
const [doc, setDoc] = useState<AppModelType | null>(null);
|
|
61
|
+
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
const unsubscribe = ref
|
|
64
|
+
? onSnapshot(ref, {
|
|
65
|
+
next: async (snapshot) => {
|
|
66
|
+
setDoc(snapshot.data() || null);
|
|
67
|
+
},
|
|
68
|
+
error: onError
|
|
69
|
+
})
|
|
70
|
+
: () => {};
|
|
71
|
+
|
|
72
|
+
return () => unsubscribe();
|
|
73
|
+
}, [ref, doc]);
|
|
74
|
+
|
|
75
|
+
return doc;
|
|
76
|
+
};
|