Next quickstart
Add authentication and permissions to your Next.js application using Kobble
You will learn how to:
- Create a new React application using Next
- Configure
@kobbleio/next
SDK - Protect your application routes
- Add permissions and quotas to your application
Example repository
To make your life simpler, we crafted various Next.js examples that you ca find in our Next Examples Repository.
Installation
Create your Next application
Scaffold your new Next application using Next Create App.
Then, when prompted, choose the options you prefer for your project.
We recommend the following setup:
Install @kobbleio/next
Setup the middleware
Our auth middleware allows you to protect your routes and handle the authentication flow seamlessly.
Create a middleware.ts
file in your project and add the following code:
This middleware will automatically expose various routes to handle authentication.
Setup the Kobble Provider
To leverage kobble utilities inside client side components, you need to wrap your app with the KobbleProvider
component.
This is commonly done in the top level layout.tsx
file.
Here’s how your layout.tsx
file should look like:
Note that KobbleProvider
itself is a server side component, and as such should not be rendered by a client component directly.
Setup the environment variables
At this stage, if you run the project, you will get the following errors:
This is because we need to add our Kobble Client ID and Customer Portal Domain to the environment variables.
Create a .env
file at the root of your project and add the following environment variables.
User authentication
Handling user state (client side)
In your client side code you can directly use the useAuth
hook and various built-in components to manage your user’s state and permissions.
Here’s a simple example using <SignedIn />
, <SignedOut />
, <LoginButton />
, <LogoutButton />
components:
Handling user state (server side)
On your server code you can leverage @kobbleio/next/server utilities to handle user state and permissions.
Permissions and quotas
Managing permissions (client side)
At this stage, you can manage your user’s permissions and quotas client using our <IsAllowed />
and <IsForbidden />
components.
Managing permissions (server side)
Managing quotas (client side)
At this stage, you can manage your user’s permissions and quotas client using our <IsAllowed />
and <IsForbidden />
components.
Managing quotas (server side)
Going further
This guide contained the basics to get you started with Kobble in your Next.js application.
However, there are many more features you can leverage to enhance your application.
Find more information in the Kobble Next.js reference.