Firebase Auth
Firebase Auth signs users in to your app, and Firestore and Realtime Database already receive the user through the Firebase adapter. For your own REST API or Cloud Functions over HTTP, HyperFetch sends the user's ID token as a bearer token, and the backend checks it with the Firebase Admin SDK.
- A valid token on every request:
getIdToken()returns a cached token and refreshes it before it expires. - No early requests without a user:
authStateReady()waits until Firebase restores the session on page load. - One retry on 401:
getIdToken(true)forces a new token and the request is sent once more. - Verified on the server:
verifyIdTokenfromfirebase-adminchecks the token and returns the user id.
Install
- npm
- yarn
- pnpm
npm install @hyper-fetch/core firebase
yarn add @hyper-fetch/core firebase
pnpm add @hyper-fetch/core firebase
Your backend needs firebase-admin to verify the tokens.
Send the ID token
Read the token in onAuth. On page load Firebase restores the session asynchronously, so wait for authStateReady()
before reading currentUser, or the first requests go out without a token:
import { createClient } from "@hyper-fetch/core";
import { getAuth } from "firebase/auth";
import { app } from "./firebase";
const auth = getAuth(app);
export const client = createClient({ url: "https://api.example.com" }).onAuth(
async (request) => {
await auth.authStateReady();
// Cached, and refreshed when it expires within 5 minutes
const token = await auth.currentUser?.getIdToken();
if (!token) return request;
return request.setHeaders({
...request.headers,
Authorization: `Bearer ${token}`,
});
},
);
ID tokens last one hour. Call getIdToken() for every request instead of storing the token, so you always send one
Firebase has refreshed.
Retry once on 401
If the API rejects a token, for example after the user's claims changed, force a refresh and send the request once
more. setUsed(true) marks the retry, so a second 401 comes back instead of looping:
client.onError(async (response, request) => {
if (response.status !== 401 || request.used || !auth.currentUser)
return response;
// Skip the cache and get a new token
await auth.currentUser.getIdToken(true);
// onAuth runs again and reads the new token
return request.setUsed(true).send();
});
Clear data on sign out
onIdTokenChanged fires on sign in, sign out and every token refresh. Clear the HyperFetch client when the user is
gone, so the next user sees none of the previous user's data:
import { onIdTokenChanged } from "firebase/auth";
onIdTokenChanged(auth, (user) => {
if (!user) client.clear();
});
Verify the token on your API
The backend verifies the token with the Admin SDK and gets the user's id from it:
import { getAuth } from "firebase-admin/auth";
const header = req.headers.authorization ?? "";
const decoded = await getAuth().verifyIdToken(header.replace("Bearer ", ""));
const userId = decoded.uid;
verifyIdToken rejects expired tokens and tokens from other projects. Pass true as the second argument to also
reject tokens of users whose sessions you revoked; it costs one call to Firebase.
- Apps on Firebase Auth with their own REST API, or Cloud Functions over HTTP.
- APIs that should know the user without a session store of their own.
- Moving parts of a Firebase app to another backend while keeping the same sign in.
onAuthwaits forauthStateReady()and sendsgetIdToken().getIdToken(true)withsetUsed(true)retries once after a401.onIdTokenChangedclears HyperFetch data when the user signs out.verifyIdTokenfromfirebase-admin/authchecks the token on your API.