Skip to content

Commit

Permalink
feat(app-check): add useAppCheckToken (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
andipaetzold committed Oct 14, 2022
1 parent 654340a commit 81d95c2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/test-results/

# modules
/app-check/
/auth/
/database/
/firestore/
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ If you previously used [`react-firebase-hooks`](https://www.npmjs.com/package/re

This library consists of 4 modules with many hooks:

- [`app-check`](#AppCheck)
- [`useAppCheckToken`](#useAppCheckToken)
- [`auth`](#Auth)
- [`useAuthIdToken`](#useAuthIdToken)
- [`useAuthIdTokenResult`](#useAuthIdTokenResult)
Expand Down Expand Up @@ -67,6 +69,30 @@ This library consists of 4 modules with many hooks:

All hooks can be imported from `react-firehooks` directly or via `react-firehooks/<module>` to improve tree-shaking and bundle size.

### App Check

```javascript
import { ... } from 'react-firehooks/app-check';
```

#### useAuthState

Returns and updates the current App Check token

```javascript
const [user, loading, error] = useAppCheckToken(auth);
```

Params:

- `appCheck`: Firebase App Check instance

Returns:

- `value`: App Check token; `undefined` if the token is currently being fetched, or an error occurred
- `loading`: `true` while fetching the token; `false` if the token was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

### Auth

```javascript
Expand Down
2 changes: 1 addition & 1 deletion scripts/create-modules.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";

const modules = ["auth", "database", "firestore", "messaging", "storage"];
const modules = ["app-check", "auth", "database", "firestore", "messaging", "storage"];
for (const module of modules) {
const packageJson = {
main: `../lib/${module}/index.js`,
Expand Down
1 change: 1 addition & 0 deletions src/app-check/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./useAppCheckToken.js";
25 changes: 25 additions & 0 deletions src/app-check/useAppCheckToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { AppCheck, AppCheckTokenResult, onTokenChanged } from "firebase/app-check";
import { useCallback } from "react";
import { ValueHookResult } from "../common/index.js";
import { useListen, UseListenOnChange } from "../internal/useListen.js";
import { LoadingState } from "../internal/useLoadingValue.js";

export type UseAppCheckToken = ValueHookResult<AppCheckTokenResult | null, Error>;

/**
* Returns and updates the current App Check token
*
* @param {AppCheck} appCheck Firebase App Check instance
* @returns {UseAppCheckToken} App Check token, loading state, and error
* * value: App Check token; `undefined` if token is currently being fetched, or an error occurred
* * loading: `true` while fetching the token; `false` if the token was fetched successfully or an error occurred
* * error: `undefined` if no error occurred
*/
export function useAppCheckToken(appCheck: AppCheck): UseAppCheckToken {
const onChange: UseListenOnChange<AppCheckTokenResult | null, Error, AppCheck> = useCallback(
(stableAppCheck, next, error) => onTokenChanged(stableAppCheck, next, error),
[]
);

return useListen(appCheck, onChange, () => true, LoadingState);
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./app-check/index.js";
export * from "./auth/index.js";
export * from "./common/index.js";
export * from "./database/index.js";
Expand Down

0 comments on commit 81d95c2

Please sign in to comment.