From the course: Master React 19 and Next.js 16 with Hands-On Projects and Real-World Applications

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Authentication: Restrict pages

Authentication: Restrict pages

Hello, and welcome. In this session, you'll learn how to restrict access to certain pages, ensuring that only logged-in users can view them. By the end of this session, you will be able to protect any client-side page in your application, showing a login prompt to visitors who haven't signed in yet. Alright, so right now our chat page is accessible to anyone, even if they're not logged in. That's not what we want. Let's lock that down and restrict access to the chat page to only users who are actually logged in. Now I am going to modify our imports to bring in the tools we need from NextAuth. You'll see I'm adding two new items to our import statements. First, I'm importing useSession from the NextAuth React library. This is a custom React hook that is the key to accessing the user's authentication status on the client side. It gives you information about whether a user is logged in, logged out, or if the session is still loading. Second, I'm importing signIn. This is a handy…

Contents