firebase-nextjs logoFirebase-NextJS

Not logged in

(Log in to see auth demo)

Authentication

This guide will explain how you can access the authentication status and user details on client side and server side.

Overview

User can log in and log out using the default login pages or custom login pages. This guide will explain how to access the user object programmatically.

This is supported out of the box for client side and server side. You DO NOT have to work with sessions, tokens, or cookies manually.

Client Side

Make use of the function getUserCS (Stands for get user client side).

1 2 3 4 5 6 7 8 9 10 "use client"; import { getUserCS } from "firebase-nextjs/client/auth"; export default function Page() { const { currentUser } = getUserCS(); return <div> Current User CS: {currentUser?.displayName} </div> }

RESULT (log in if you see null)

Current User CS: null

The returned currentUser object is of type User in firebase/auth.

Server Side

Make use of the function getUserSS (Stands for get user server side).

1 2 3 4 5 6 7 8 9 10 "use server"; import { getUserSS } from "firebase-nextjs/server/auth"; export default async function Page() { const user = await getUserSS(); return <div> Current User SS: {user?.name} </div> }

RESULT (log in if you see null)

Current User SS: null

The returned user object is of type DecodedIdToken in firebase/auth.