react-query-firebase 1.0.2 → 1.0.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.
@@ -7,5 +7,6 @@ export * from "./useAuthStateReady";
7
7
  export * from "./useSignInWitRedirectMutation";
8
8
  export * from "./useUpdateProfileMutation";
9
9
  export * from "./useSignOutMutation";
10
+ export * from "./useIdToken";
10
11
  export * from "./useReauthenticateWitCredentialMutation";
11
12
  export * from "./useReauthenticateWitRedirectMutation";
@@ -7,5 +7,6 @@ export * from "./useAuthStateReady";
7
7
  export * from "./useSignInWitRedirectMutation";
8
8
  export * from "./useUpdateProfileMutation";
9
9
  export * from "./useSignOutMutation";
10
+ export * from "./useIdToken";
10
11
  export * from "./useReauthenticateWitCredentialMutation";
11
12
  export * from "./useReauthenticateWitRedirectMutation";
@@ -0,0 +1,4 @@
1
+ export declare const useIdToken: () => {
2
+ idToken: string;
3
+ refresh: () => Promise<void>;
4
+ };
@@ -0,0 +1,28 @@
1
+ import { useCallback, useEffect, useState } from "react";
2
+ import { useCurrentUser } from "./useCurrentUser";
3
+ export const useIdToken = () => {
4
+ const currentUser = useCurrentUser();
5
+ const [idToken, setIdToken] = useState("");
6
+ const callback = useCallback(async () => {
7
+ if (!currentUser) {
8
+ setIdToken("");
9
+ return;
10
+ }
11
+ const idToken = await currentUser.getIdToken();
12
+ setIdToken(idToken);
13
+ }, [currentUser, idToken]);
14
+ const refresh = useCallback(async () => {
15
+ if (!currentUser) {
16
+ return;
17
+ }
18
+ const idToken = await currentUser.getIdToken(true);
19
+ setIdToken(idToken);
20
+ }, [currentUser, idToken]);
21
+ useEffect(() => {
22
+ callback();
23
+ }, [currentUser?.uid ?? ""]);
24
+ return {
25
+ idToken,
26
+ refresh
27
+ };
28
+ };
package/package.json CHANGED
@@ -53,5 +53,5 @@
53
53
  "lint:staged": "lint-staged",
54
54
  "prepare": "husky"
55
55
  },
56
- "version": "1.0.2"
56
+ "version": "1.0.3"
57
57
  }
package/src/auth/index.ts CHANGED
@@ -7,5 +7,6 @@ export * from "./useAuthStateReady";
7
7
  export * from "./useSignInWitRedirectMutation";
8
8
  export * from "./useUpdateProfileMutation";
9
9
  export * from "./useSignOutMutation";
10
+ export * from "./useIdToken";
10
11
  export * from "./useReauthenticateWitCredentialMutation";
11
12
  export * from "./useReauthenticateWitRedirectMutation";
@@ -1,51 +0,0 @@
1
- import { useMutation, UseMutationOptions } from "@tanstack/react-query";
2
- import {
3
- DocumentData,
4
- updateDoc,
5
- getDoc,
6
- FirestoreDataConverter,
7
- DocumentReference,
8
- UpdateData
9
- } from "firebase/firestore";
10
-
11
- import { FirebaseError } from "firebase/app";
12
- import { useMemo } from "react";
13
-
14
- export type UseUpdateDocMutationValues<DbModelType> = {
15
- data: UpdateData<DbModelType>;
16
- };
17
-
18
- export type UseUpdateDocMutationOptions<
19
- AppModelType extends DocumentData = DocumentData,
20
- DbModelType extends DocumentData = DocumentData,
21
- TContext = unknown
22
- > = {
23
- reference: DocumentReference<AppModelType, DbModelType>;
24
- converter?: FirestoreDataConverter<AppModelType, DbModelType>;
25
- options?: Omit<
26
- UseMutationOptions<AppModelType, FirebaseError, UseUpdateDocMutationValues<DbModelType>, TContext>,
27
- "mutationFn" | "mutationKey"
28
- >;
29
- };
30
-
31
- export const useUpdateDocMutation = <
32
- AppModelType extends DocumentData = DocumentData,
33
- DbModelType extends DocumentData = DocumentData,
34
- TContext = unknown
35
- >({
36
- reference,
37
- converter,
38
- options = {}
39
- }: UseUpdateDocMutationOptions<AppModelType, DbModelType, TContext>) => {
40
- const mutationKey = useMemo(() => [reference.path], [reference.path]);
41
-
42
- return useMutation({
43
- ...options,
44
- mutationFn: async ({ data }) => {
45
- await updateDoc<AppModelType, DbModelType>(reference, data);
46
- const docSnap = await getDoc(converter ? reference.withConverter(converter) : reference);
47
- return docSnap.data() as AppModelType;
48
- },
49
- mutationKey
50
- });
51
- };