v1.0.0
Usage Examples
Verify the user ID Token issued by Google or Firebase
NOTE: The credentials
argument in the examples below is expected to be a serialized JSON string of a Google Cloud service account key, apiKey
is Google Cloud API Key (Firebase API Key), and projectId
is a Google Cloud project ID.
import { verifyIdToken } from "web-auth-library/google";
const token = await verifyIdToken({
idToken,
credentials: env.GOOGLE_CLOUD_CREDENTIALS,
});
// => {
// iss: 'https://securetoken.google.com/example',
// aud: 'example',
// auth_time: 1677525930,
// user_id: 'temp',
// sub: 'temp',
// iat: 1677525930,
// exp: 1677529530,
// firebase: {}
// }
Create an access token for accessing Google Cloud APIs
import { getAccessToken } from "web-auth-library/google";
// Generate a short lived access token from the service account key credentials
const accessToken = await getAccessToken({
credentials: env.GOOGLE_CLOUD_CREDENTIALS,
scope: "https://www.googleapis.com/auth/cloud-platform",
});
// Make a request to one of the Google's APIs using that token
const res = await fetch(
"https://cloudresourcemanager.googleapis.com/v1/projects",
{
headers: { Authorization: `Bearer ${accessToken}` },
}
);
Create a custom ID token using Service Account credentials
import { getIdToken } from "web-auth-library/google";
const idToken = await getIdToken({
credentials: env.GOOGLE_CLOUD_CREDENTIALS,
audience: "https://example.com",
});
An alternative way passing credentials
Instead of passing credentials via options.credentials
argument, you can also let the library pick up credentials from the list of environment variables using standard names such as GOOGLE_CLOUD_CREDENTIALS
, GOOGLE_CLOUD_PROJECT
, FIREBASE_API_KEY
, for example:
import { verifyIdToken } from "web-auth-library/google";
const env = { GOOGLE_CLOUD_CREDENTIALS: "..." };
const token = await verifyIdToken({ idToken, env });
Optimize cache renewal background tasks
Pass the optional waitUntil(promise)
function provided by the target runtime to optimize the way authentication tokens are being renewed in background. For example, using Cloudflare Workers and Hono.js:
import { Hono } from "hono";
import { verifyIdToken } from "web-auth-library/google";
const app = new Hono();
app.get("/", ({ env, executionCtx, json }) => {
const idToken = await verifyIdToken({
idToken: "...",
waitUntil: executionCtx.waitUntil,
env,
});
return json({ ... });
})