Hello World
Only authenticated users can see this text
Note: I am using Next.js 13 and the app/
directory.
I'm learning Firebase and Next.js and I'm trying to understand how to solve a toy problem. Let's say I have a Home()
Component
/app/page.jsx
export default function Home() { return () } Hello World
Only authenticated users can see this text
My goal is to conditionally render everything in ...
based on whether the user requesting the page is a logged in user. Firebase renders this component server side using JWT, Next.js 13, so I believe this is possible, but I can't figure out how to do it.
I know about onAuthStateChanged, but as far as I know this can only be used on the client side. (Savvy users can still view this protected content.) How do I protect this content server-side?
To check if the user is logged in, you can use the 'onAuthStateChanged' method.
Store this information in the component's state, or use it to conditionally render parts of the component.
To perform user authentication on the server side, Next.js provides 'getServerSideProps' to get the user's authentication status
Updated solution:
Create a server-side route
Client code