In this lesson, we’ll protect some of the route of our Next.js application to allow only authenticated users to access them.

For that, we’ll need to replace the getStaticProps method by the getServerSideProps method, also provided by the Next.js framework. This method is pre-rendered by Next.js on each request using the data we return inside it. This is exactly what we need to protect our route and check if the current user is authenticated on the server-side before rendering the page to the client. If not, we’ll redirect the user to the sign-in page.

Using the same method, we’ll also restrict the access to the editing page of a blog post to its author and only its author. Indeed, no one want his posts to be edited by someone else. So we’ll see how to protect our users post from that.

#next.js

Protect Some Of The Route Of Next.js Application - #17
1.45 GEEK