Protect Next.js route handlers with machine-to-machine application?
Hi,
I want to expose the route handlers defined in Next.js 14 application for external applications to call using OAuth 2 client credentials flow. Can I do this with machine-to-machine application?
I tried to get the access token like this,
but it failed with this error,
Do you know what could be the problem?
9 Replies
Hey TJ, I'll grab our Next.js guy and get back to you soon.
Hey TJ, seems like you're on the right track. You may need to visit this page and enable APIs for applications.
This article may help: https://kinde.com/docs/build/add-a-m2m-application-for-api-access/
Kinde Docs
Add a machine to machine application - Build on Kinde - Help center
Our developer tools provide everything you need to get started with Kinde.
@Peter (Kinde) I think what is missing from the document is to create an API (e.g. m2m) under Settings, and add this m2m API to the machine-to-machine application.
also, may I know what is the purpose of API Id and Audience?
I see that we need to specify the audience whe requesting the access token, but does it serve any purpose?
Hi,
I have a Next.js application which I want the external applications to call the API using the access token obtain from the client credentials flow.
I created one Next.js machine-to-machine application in Kinde.
app/api/auth/[kindeAuth]/route.js
the protected API in app/api/payment/[id]/route.js
and protect the API in middleware.js
and I only defined these environment variables in my Next.js,
.env.local
Now, I have requested an access token successfully,
but I got a 307 redirect when I called the API with the access token
Do you know what am I missing here?Hi,
I try to summarize what I did in the GitHub repository at https://github.com/tjhoo/kinde-client-credentials and the steps I performed in README.md
GitHub
GitHub - tjhoo/kinde-client-credentials
Contribute to tjhoo/kinde-client-credentials development by creating an account on GitHub.
@Claire_Kinde Are you able to guide me on this? 🥹
@Claire_Kinde I tried https://auth0.com/docs/quickstart/backend/nodejs/01-authorization
Can I achieve the same using Kinde?
Auth0 Docs
Auth0 Node (Express) API SDK Quickstarts: Authorization
This tutorial demonstrates how to add authorization to an Express.js API.
Hi TJ. Just catching up on this - will get back to you