Next.js App Router SDK
SDKs and APIs
This SDK is for developers already using the Next.js Pages Router SDK. This document is out of date for new users, use this document instead: Next.js Pages Router.
We highly recommend using our dedicated Next.js App Router SDK with App Router instead of this one.
Whilst technically this SDK is compatible with Next.js 13, it isn’t optimal. It leverages the use client;
escape hatch, which we don’t love. It also requires a single API file to be stored in the legacy pages
directory.
http://localhost:3000/api/auth/kinde_callback
http://localhost:3000
If you would like to use our Environments feature as part of your development process. You will need to create them first within your Kinde account. In this case you would use the Environment subdomain in the code block above.
Put these variables in your .env file. You can find these variables on your Kinde Settings -> App keys page.
KINDE_SITE_URL
- where your app is runningKINDE_ISSUER_URL
- your kinde domainKINDE_POST_LOGOUT_REDIRECT_URL
- where you want users to be redirected to after logging out. Make sure this URL is under your allowed logout redirect URLs.KINDE_POST_LOGIN_REDIRECT_URL
- where you want users to be redirected to after authenticating.KINDE_CLIENT_ID
- you can find this on the Application details pageKINDE_CLIENT_SECRET
- you can find this on the Application details pageCreate the following file /pages/api/auth/[...kindeAuth].js
inside your Next.js project. Inside the file [...kindeAuth].js
put this code:
This will handle Kinde Auth endpoints in your Next.js app.
/api/auth/me
- this endpoint will get user information/api/auth/login
- will redirect you to login at the KindeAuth server/api/auth/logout
- will log you out of the app/api/auth/register
- will redirect you to register at the KindeAuth server.Our SDK relies on this file existing in this location specified above. This includes Next.js 13 projects.
Kinde uses a React Context Provider to maintain its internal state in your application.
Import the KindeProvider
component and wrap your application in it.
Next.js 13: we suggest you include this in app/layout.tsx
In the example above, there is a custom Auth component which handles routing depending on if the user is authenticated. Here is an example Auth component from our Starter Kit.
Next.js 12 and below: we suggest you include this in the root file of your application in _app.js
The SDK ships with predefined API routes to generate the auth urls for sign up and sign in.
Next.js 13
Link prefetching causes issues with preflight options, so we need to use standard <a>
tags for our links. So the build doesn’t break you’ll want to disable the linting that comes with Next as per the sample below:
Next.js 12 and below
You can use the <Link>
component that ships with earlier versions of Next.
This is implemented in much the same way as signing up or signing in. An API route is provided for you
Register your first user by signing up yourself. You’ll see your newly registered user on the Users page in Kinde.
You can get an authorized user’s profile from any component using the Kinde Next.js hoo
To be on the safe side we have also provided isAuthenticated
and isLoading
state to prevent rendering errors.
The getToken
method lets you to securely call your API and pass the bearer token to validate that your user is authenticated.
We recommend using JWT verification middleware on your back end to verify the token and protect your endpoints.
An audience
is the intended recipient of an access token - for example the API for your application. The audience
argument can be set against KINDE_AUDIENCE
in your environment variables.
The audience of a token is the intended recipient of the token.
For details on how to connect, see Register an API.
Once a user has been verified as login in, your product/application will be returned the JWT token with an array of permissions for that user. You will need to configure your product/application to read permissions and unlock the respective functions.
You set Permissions in your Kinde account (see help article), the below is an example set of permissions.
We provide helper functions to more easily access permissions:
A practical example in code might look something like:
When a user signs in the Access token your product/application receives contains a custom claim called feature_flags
which is an object detailing the feature flags for that user.
You can set feature flags in your Kinde account. Here’s an example.
In order to minimize the payload in the token we have used single letter keys / values where possible. The single letters represent the following:
t
= type
v
= value
s
= string
b
= boolean
i
= integer
We provide helper functions to more easily access feature flags:
A practical example in code might look something like:
We also require wrapper functions by type which should leverage getFlag
above.
Booleans:
Strings and integers work in the same way as booleans above:
A practical example in code might look something like:
To have a new organization created within your application, you will need to run a similar function to below:
Next.js 13
Next.js 12 and below
Every organization in Kinde has a unique code. To sign up a new user into a particular organization you will need to pass through this code in the register
method. (See where to find it).
Next.js 13
Next.js 12 and below
This code should also be passed along with the login
method if you wish for a user to be logged into a specific organization.
Next.js 13
Next.js 12 and below
For general information about how organizations work in Kinde, see Kinde organizations for developers.
If your Next.js application uses a custom base path for your API. The default path is /api/auth
but to override this setting you can change this in your .env
file as follows:
You need to enable the application’s access to the Kinde Management API. You can do this in Kinde by going to Settings > APIs > Kinde Management API and then toggling on your Next.js application under the Applications tab.
To use our management API please see @kinde/management-api-js
undefined
cannot be serialized as JSON
This happens when the API returns an object with a property that is undefined and we try to return that object from getServerSideProps
.
You can use this workaround to avoid this error:
If you want your project to remember which url your user was intending to visit before they were asked to authenticate, you can pass an additional parameter in the /login
and /register
links.
After the user has completed authentication at your defined callback url they will be redirected to the path you define here. This value does not need to be added to your allowed callback urls in Kinde.
Next.js 13
Next.js 12 and below
Note: the value of post_login_redirect_url
should either be a url on the same origin or a relative path.
If you need any assistance with getting Kinde connected reach out to us at support@kinde.com.