react-query-firebase 1.2.1 → 1.2.3
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.
|
@@ -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
|
|
@@ -70,7 +70,7 @@ export type FirebaseContextProviderProps = PropsWithChildren & {
|
|
|
70
70
|
* security_storage: "denied"
|
|
71
71
|
* }
|
|
72
72
|
*/
|
|
73
|
-
|
|
73
|
+
consentSettings?: ConsentSettings;
|
|
74
74
|
/**
|
|
75
75
|
* Flag indicating whether Firebase Firestore should be enabled.
|
|
76
76
|
* @defaultValue `true`
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useMemo } from "react";
|
|
1
|
+
import React, { useEffect, useMemo } from "react";
|
|
2
2
|
import { FirebaseContext } from "./FirebaseContext";
|
|
3
3
|
import { connectAuthEmulator, getAuth } from "firebase/auth";
|
|
4
4
|
import { getAnalytics, setAnalyticsCollectionEnabled, setConsent } from "firebase/analytics";
|
|
@@ -28,10 +28,22 @@ import { initializeApp } from "firebase/app";
|
|
|
28
28
|
* };
|
|
29
29
|
* ```
|
|
30
30
|
*/
|
|
31
|
-
export const FirebaseContextProvider = ({ emulators, options, children, authEnabled = true, firestoreEnabled = true, analyticsEnabled = true,
|
|
31
|
+
export const FirebaseContextProvider = ({ emulators, options, children, authEnabled = true, firestoreEnabled = true, analyticsEnabled = true, consentSettings = {}, remoteConfigEnabled = true, remoteConfigSettings, remoteConfigDefaults = {} }) => {
|
|
32
32
|
const firebase = useMemo(() => {
|
|
33
33
|
return initializeApp(options);
|
|
34
34
|
}, [options]);
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
setConsent({
|
|
37
|
+
ad_personalization: "denied",
|
|
38
|
+
ad_storage: "denied",
|
|
39
|
+
ad_user_data: "denied",
|
|
40
|
+
analytics_storage: "denied",
|
|
41
|
+
functionality_storage: "denied",
|
|
42
|
+
personalization_storage: "denied",
|
|
43
|
+
security_storage: "denied",
|
|
44
|
+
...consentSettings
|
|
45
|
+
});
|
|
46
|
+
}, [consentSettings]);
|
|
35
47
|
const contextValue = useMemo(() => {
|
|
36
48
|
const value = {};
|
|
37
49
|
if (firestoreEnabled) {
|
|
@@ -50,20 +62,9 @@ export const FirebaseContextProvider = ({ emulators, options, children, authEnab
|
|
|
50
62
|
}
|
|
51
63
|
value.auth = auth;
|
|
52
64
|
}
|
|
53
|
-
setConsent({
|
|
54
|
-
ad_personalization: "denied",
|
|
55
|
-
ad_storage: "denied",
|
|
56
|
-
ad_user_data: "denied",
|
|
57
|
-
analytics_storage: "denied",
|
|
58
|
-
functionality_storage: "denied",
|
|
59
|
-
personalization_storage: "denied",
|
|
60
|
-
security_storage: "denied",
|
|
61
|
-
...defaultConsent
|
|
62
|
-
});
|
|
63
65
|
if (analyticsEnabled && options.measurementId && typeof window !== "undefined") {
|
|
64
66
|
const analytics = getAnalytics(firebase);
|
|
65
67
|
value.analytics = analytics;
|
|
66
|
-
setAnalyticsCollectionEnabled(analytics, defaultConsent?.analytics_storage === "granted");
|
|
67
68
|
}
|
|
68
69
|
if (remoteConfigEnabled && typeof window !== "undefined") {
|
|
69
70
|
const remoteConfig = getRemoteConfig(firebase);
|
|
@@ -76,5 +77,10 @@ export const FirebaseContextProvider = ({ emulators, options, children, authEnab
|
|
|
76
77
|
}
|
|
77
78
|
return { firebase, ...value };
|
|
78
79
|
}, [firebase]);
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
if (contextValue.analytics) {
|
|
82
|
+
setAnalyticsCollectionEnabled(contextValue.analytics, consentSettings?.analytics_storage === "granted");
|
|
83
|
+
}
|
|
84
|
+
}, [consentSettings]);
|
|
79
85
|
return (React.createElement(FirebaseContext.Provider, { value: contextValue }, children));
|
|
80
86
|
};
|
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
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { PropsWithChildren, useMemo } from "react";
|
|
1
|
+
import React, { PropsWithChildren, useEffect, useMemo } from "react";
|
|
2
2
|
import { FirebaseContext } from "./FirebaseContext";
|
|
3
3
|
import { connectAuthEmulator, getAuth } from "firebase/auth";
|
|
4
4
|
import { ConsentSettings, getAnalytics, setAnalyticsCollectionEnabled, setConsent } from "firebase/analytics";
|
|
@@ -77,7 +77,7 @@ export type FirebaseContextProviderProps = PropsWithChildren & {
|
|
|
77
77
|
* security_storage: "denied"
|
|
78
78
|
* }
|
|
79
79
|
*/
|
|
80
|
-
|
|
80
|
+
consentSettings?: ConsentSettings;
|
|
81
81
|
/**
|
|
82
82
|
* Flag indicating whether Firebase Firestore should be enabled.
|
|
83
83
|
* @defaultValue `true`
|
|
@@ -129,7 +129,7 @@ export const FirebaseContextProvider: React.FC<FirebaseContextProviderProps> = (
|
|
|
129
129
|
authEnabled = true,
|
|
130
130
|
firestoreEnabled = true,
|
|
131
131
|
analyticsEnabled = true,
|
|
132
|
-
|
|
132
|
+
consentSettings = {},
|
|
133
133
|
remoteConfigEnabled = true,
|
|
134
134
|
remoteConfigSettings,
|
|
135
135
|
remoteConfigDefaults = {}
|
|
@@ -138,6 +138,19 @@ export const FirebaseContextProvider: React.FC<FirebaseContextProviderProps> = (
|
|
|
138
138
|
return initializeApp(options);
|
|
139
139
|
}, [options]);
|
|
140
140
|
|
|
141
|
+
useEffect(() => {
|
|
142
|
+
setConsent({
|
|
143
|
+
ad_personalization: "denied",
|
|
144
|
+
ad_storage: "denied",
|
|
145
|
+
ad_user_data: "denied",
|
|
146
|
+
analytics_storage: "denied",
|
|
147
|
+
functionality_storage: "denied",
|
|
148
|
+
personalization_storage: "denied",
|
|
149
|
+
security_storage: "denied",
|
|
150
|
+
...consentSettings
|
|
151
|
+
});
|
|
152
|
+
}, [consentSettings]);
|
|
153
|
+
|
|
141
154
|
const contextValue = useMemo(() => {
|
|
142
155
|
const value: Partial<React.ContextType<typeof FirebaseContext>> = {};
|
|
143
156
|
|
|
@@ -161,22 +174,9 @@ export const FirebaseContextProvider: React.FC<FirebaseContextProviderProps> = (
|
|
|
161
174
|
value.auth = auth;
|
|
162
175
|
}
|
|
163
176
|
|
|
164
|
-
setConsent({
|
|
165
|
-
ad_personalization: "denied",
|
|
166
|
-
ad_storage: "denied",
|
|
167
|
-
ad_user_data: "denied",
|
|
168
|
-
analytics_storage: "denied",
|
|
169
|
-
functionality_storage: "denied",
|
|
170
|
-
personalization_storage: "denied",
|
|
171
|
-
security_storage: "denied",
|
|
172
|
-
...defaultConsent
|
|
173
|
-
});
|
|
174
|
-
|
|
175
177
|
if (analyticsEnabled && options.measurementId && typeof window !== "undefined") {
|
|
176
178
|
const analytics = getAnalytics(firebase);
|
|
177
179
|
value.analytics = analytics;
|
|
178
|
-
|
|
179
|
-
setAnalyticsCollectionEnabled(analytics, defaultConsent?.analytics_storage === "granted");
|
|
180
180
|
}
|
|
181
181
|
|
|
182
182
|
if (remoteConfigEnabled && typeof window !== "undefined") {
|
|
@@ -192,6 +192,12 @@ export const FirebaseContextProvider: React.FC<FirebaseContextProviderProps> = (
|
|
|
192
192
|
return { firebase, ...value };
|
|
193
193
|
}, [firebase]);
|
|
194
194
|
|
|
195
|
+
useEffect(() => {
|
|
196
|
+
if (contextValue.analytics) {
|
|
197
|
+
setAnalyticsCollectionEnabled(contextValue.analytics, consentSettings?.analytics_storage === "granted");
|
|
198
|
+
}
|
|
199
|
+
}, [consentSettings]);
|
|
200
|
+
|
|
195
201
|
return (
|
|
196
202
|
<FirebaseContext.Provider value={contextValue as React.ContextType<typeof FirebaseContext>}>
|
|
197
203
|
{children}
|