UNPKG

react-firehooks

Version:

Lightweight dependency-free collection of React hooks for Firebase

30 lines (29 loc) 1.09 kB
import { getIdToken, onIdTokenChanged } from "firebase/auth"; import { useListen } from "../internal/useListen.js"; import { LoadingState } from "../internal/useLoadingValue.js"; const onChange = (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); } } else { next(null); } }); /** * Returns and updates the JWT of the currently authenticated user * @param auth Firebase Auth instance * @returns 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) { return useListen(auth, onChange, () => true, LoadingState); }