Skip to content

Commit

Permalink
feat(auth): add useAuthIdTokenResult (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
andipaetzold committed Oct 14, 2022
1 parent f351734 commit 654340a
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 @@ -40,6 +40,7 @@ This library consists of 4 modules with many hooks:

- [`auth`](#Auth)
- [`useAuthIdToken`](#useAuthIdToken)
- [`useAuthIdTokenResult`](#useAuthIdTokenResult)
- [`useAuthState`](#useAuthState)
- [`database`](#Database)
- [`useObject`](#useObject)
Expand Down Expand Up @@ -90,6 +91,24 @@ Returns:
- `loading`: `true` while fetching the JWT; `false` if the JWT was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

#### useAuthIdTokenResult

Returns and updates the deserialized JWT of the currently authenticated user

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

Params:

- `auth`: Firebase Auth instance

Returns:

- `value`: Deserialized 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 @@ -104,7 +123,7 @@ Params:

Returns:

- `value`: User; `undefined` if user is currently being fetched, or an error occurred
- `value`: User; `undefined` if the 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
- `error`: `undefined` if no 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,2 +1,3 @@
export * from "./useAuthIdToken.js";
export * from "./useAuthIdTokenResult.js";
export * from "./useAuthState.js";
38 changes: 38 additions & 0 deletions src/auth/useAuthIdTokenResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Auth, AuthError, getIdTokenResult, IdTokenResult, 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 UseAuthIdTokenResultResult = ValueHookResult<IdTokenResult | null, AuthError>;

/**
* Returns and updates the deserialized JWT of the currently authenticated user
*
* @param {Auth} auth Firebase Auth instance
* @returns {UseAuthIdTokenResultResult} Deserialized JWT, loading state, and error
* * value: Deserialized JWT; `undefined` if the JWT is currently being fetched, or an error occurred
* * loading: `true` while fetching JWT; `false` if the JWT was fetched successfully or an error occurred
* * error: `undefined` if no error occurred
*/
export function useAuthIdTokenResult(auth: Auth): UseAuthIdTokenResultResult {
const onChange: UseListenOnChange<IdTokenResult | 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 idTokenResult = await getIdTokenResult(user);
next(idTokenResult);
} catch (e) {
error(e as AuthError);
}
} else {
next(null);
}
}),
[]
);

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

0 comments on commit 654340a

Please sign in to comment.