NextFire main logoNextFire
Docs X
Introduction
Get Started
Clone repo
Run the app
Configuration
Firebase
Create firebase app
Authentication
Firestore
Storage
Functions
Global configuration
Public
Site config
Payments
Stripe
One time
Subscriptions
Webhooks
Usage
Project structure
Static pages
Public pages
Protected pages
Auth
Client auth
Server auth
Data fetching
Firestore client
Firestore admin
Functions
Storage
Content
Blog
Documentation
Components
Markdown
Cards
Diagrams
Filetree
Lists
Maths
Notes
Steps
Table
Tabs
Public
Deep
Deeper
Even deeper
  1. Auth
  2. Server Auth

Server Authentication

How to get the current user in server components using next-firebase-auth-edge in the boilerplate.

1

Usage in Server Components

You can now call getCurrentUser() in any server component to get the current authenticated user:

import {getCurrentUser} from "@/lib/session";

const currentUser = await getCurrentUser();
if (!currentUser) {
  // handle unauthenticated state
}
console.log(currentUser.role, currentUser.subscription);
2

Content of getCurrentUser Function

It's a cached async function to retrieve the current user:

export const getCurrentUser = cache(async () => {
  const tokens = await getTokens(await cookies(), authConfig);
  if (!tokens) return null;

  const user = toUser(tokens);

  const db = getFirestore(getFirebaseAdminApp());
  const snapshot = await db
    .collection('users')
    .doc(user.uid)
    .get();

  const userSubscription = await getUserSubscriptionPlanById(user.uid);

  if (snapshot.exists) {
    const data = snapshot.data();
    user.role = data?.role;
    user.subscription = userSubscription;
  }

  return user;
});

This function:

  • Retrieves authentication tokens from cookies.
  • Converts the tokens to a user object.
  • Fetches additional user data from Firestore.
  • Loads the user's subscription plan.
  • Returns the complete user object, or null if the user is not authenticated.

For more advanced server-side authentication patterns and middleware options, refer to the next-firebase-auth-edge documentation. This will help you customize authentication and token handling according to your app's needs.

Client authData fetching

Content

FeedbackEdit page

© 2025 Ship IT.

Rubix Studios logo