This quickstart showcases how to correctly add authentication to a Next.js 13 project using the new App Router and Server Components. The other parts of the SDK (Storage / GraphQL/ Functions) should work the same as before.
Saving the auth session
To enable authentication with Server Components we have to store the auth session in a cookie. This should be done right after any signIn or signUp operation. See example here.
Oauth & refresh session middleware
Create a middleware at the root of your project that calls the helper method manageAuthSession
. Feel free to copy paste the the contents of the /utils
folder to your project. The second argument for manageAuthSession
is for handling the case where there's an error refreshing the current session with the refreshToken
stored in the cookie.
Protected routes
To make sure only authenticated users access some Server Components, wrap them in the Higher Order Server Component withAuthAsync
.
Clone the repository
Install and build dependencies
Terminal 1: Start the Nhost Backend
Make sure you have the Nhost CLI installed.
Terminal 2: Start the Next.js application