Authorize Firebase Users in Next.js Route Handlers
Verify Firebase sessions server-side, enforce per-resource authorization, and test denial, revocation and response caching boundaries.
Record the Firebase project, Next.js route runtime, and whether the route uses bearer ID tokens or a Firebase session cookie. Use the server-only Firebase Admin SDK in a supported Node.js runtime. Use verifyIdToken for a bearer ID token and verifySessionCookie for a Firebase session cookie. Each verifier has its own optional revocation check. A Firebase custom token is a sign-in exchange artifact; it is not the ID token expected by `verifyIdToken`. Keep credential initialization server-side and confirm local fixtures and production point at their intended project. ## Key Points - [Firebase session management](https://firebase.google.com/docs/auth/admin/manage-sessions) - [Next.js route handlers](https://nextjs.org/docs/app/getting-started/route-handlers)
skilldb get nextjs-skills/firebase-route-authFull skill: 30 linesAuthorize Firebase Users in Next.js Route Handlers
Define identity and session transport
Record the Firebase project, Next.js route runtime, and whether the route uses bearer ID tokens or a Firebase session cookie. Use the server-only Firebase Admin SDK in a supported Node.js runtime. Use verifyIdToken for a bearer ID token and verifySessionCookie for a Firebase session cookie. Each verifier has its own optional revocation check. A Firebase custom token is a sign-in exchange artifact; it is not the ID token expected by verifyIdToken. Keep credential initialization server-side and confirm local fixtures and production point at their intended project.
Parse the credential from the documented request location and reject missing or malformed input. Use verified identity from the Admin SDK rather than a decoded token or a client-submitted UID. Decide which operations need a revocation check and how the SDK version handles it. Cookie-authenticated mutations also need the application's intended CSRF defense. Keep validation errors actionable for the client while excluding token contents and account data from logs.
Authorize the actual resource
For resource authorization, identify the route and stored data that decides access. Compare the verified UID and required role or tenant membership with the target record. Use a server-side ownership lookup or a query already scoped to the caller. An Admin SDK data access path can have broader privileges than client security rules, so enforce application authorization explicitly. Return a consistent denied or missing response where existence is sensitive.
Perform policy and write operations with consistency appropriate to the data store. A role or membership might change while a request is in flight. Ensure responses contain only approved fields, and review any caching configuration for private content. Server logs should contain a correlation identifier and policy outcome rather than the credential or full sensitive resource.
Verify expected and denied paths
Create a permitted fixture user, a second user with no access to the resource, and a revoked fixture session. Test missing token, expired token, wrong project, valid authorized session and unauthorized resource access. Exercise a denied mutation and inspect the stored record to confirm nothing changed. Test revocation against the chosen verifier and session transport, since it is a separate check from signature validity.
If an emulator does not reproduce all deployment behavior, record those differences. Include one authorized staging verification when available, covering response behavior through the real route and any proxy or caching layer. Record SDK versions, project role, policy cases, and observed status and state changes without storing credentials.
References
Install this skill directly: skilldb add nextjs-skills
Related Skills
Image Optimization
Image optimization with next/image, asset handling, fonts, and performance tuning for Next.js applications
Middleware
Middleware patterns for request interception, redirects, rewrites, authentication guards, and geo-routing in Next.js
Server Actions
Server Actions and mutations for handling form submissions, data mutations, and revalidation in Next.js
API Routes
Route Handlers for building REST APIs, handling webhooks, streaming responses, and CORS in Next.js App Router
App Router
App Router fundamentals including file-based routing, layouts, loading states, and parallel routes in Next.js
Authentication
Authentication patterns using NextAuth.js (Auth.js) and Clerk for protecting routes and managing sessions in Next.js