Skip to content

Commit

Permalink
feat(auth): add useAuthIdToken (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
andipaetzold committed Oct 14, 2022
1 parent 0abc13f commit f351734
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ If you previously used [`react-firebase-hooks`](https://www.npmjs.com/package/re
This library consists of 4 modules with many hooks:

- [`auth`](#Auth)
- [`useAuthIdToken`](#useAuthIdToken)
- [`useAuthState`](#useAuthState)
- [`database`](#Database)
- [`useObject`](#useObject)
Expand Down Expand Up @@ -71,6 +72,24 @@ All hooks can be imported from `react-firehooks` directly or via `react-firehook
import { ... } from 'react-firehooks/auth';
```

#### useAuthIdToken

Returns and updates the JWT of the currently authenticated user

```javascript
const [idToken, loading, error] = useAuthIdToken(auth);
```

Params:

- `auth`: Firebase Auth instance

Returns:

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

#### useAuthState

Returns and updates the currently authenticated user
Expand All @@ -83,7 +102,7 @@ Params:

- `auth`: Firebase Auth instance

Params:
Returns:

- `value`: User; `undefined` if user is currently being fetched, or an error occurred
- `loading`: `true` while fetching the user; `false` if the user was fetched successfully or an error occurred
Expand Down
1 change: 1 addition & 0 deletions src/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./useAuthIdToken.js";
export * from "./useAuthState.js";
38 changes: 38 additions & 0 deletions src/auth/useAuthIdToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Auth, AuthError, getIdToken, onIdTokenChanged } from "firebase/auth";
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 UseAuthIdTokenResult = ValueHookResult<string | null, AuthError>;

/**
* Returns and updates the JWT of the currently authenticated user
*
* @param {Auth} auth Firebase Auth instance
* @returns {UseAuthIdTokenResult} JWT, loading state, and error
* * value: JWT; `undefined` if the JWT is currently being fetched, or an error occurred
* * loading: `true` while fetching the JWT; `false` if the JWT was fetched successfully or an error occurred
* * error: `undefined` if no error occurred
*/
export function useAuthIdToken(auth: Auth): UseAuthIdTokenResult {
const onChange: UseListenOnChange<string | null, AuthError, Auth> = useCallback(
(stableAuth, next, error) =>
onIdTokenChanged(stableAuth, async (user) => {
if (user) {
try {
// Can also be accessed via `user.accessToken`, but that's not officially documented
const idToken = await getIdToken(user);
next(idToken);
} catch (e) {
error(e as AuthError);
}
} else {
next(null);
}
}),
[]
);

return useListen(auth, onChange, () => true, LoadingState);
}

0 comments on commit f351734

Please sign in to comment.