kinde-oss / kinde-auth-nextjs

Kinde NextJS SDK - authentication for server rendered apps
https://kinde.com/docs/developer-tools/nextjs-sdk/
MIT License
132 stars 17 forks source link

I can't create user and organization by `createKindeManagementAPIClient`. #175

Closed kingRayhan closed 1 month ago

kingRayhan commented 1 month ago

Prerequisites

Describe the issue

Basically, I want to create a signup API where I will create a separate organization for every user signed up. I planned to create an organization like this <username>'s space name. I was digging through documentation, I found this createKindeManagementAPIClient.

It has lots of helpful methods for managing kinde management API. I attempted the way below but every time I got bad request. Maybe I am making some silly mistake.

CleanShot 2024-05-28 at 8  48 04@2x

Library URL

https://github.com/kinde-oss/kinde-auth-react

Library version

^2.2.11

Operating system(s)

macOS

Operating system version(s)

14.5 (23F79)

Further environment details

No response

Reproducible test case URL

https://github.com/kingRayhan/kinde-play-org

Additional information

No response

DanielRivers commented 1 month ago

Hi @kingRayhan I would recommend switching to use @kinde/management-api-js.

We are updating our guidance on this away from the one inside the Next SDK See here: https://docs.kinde.com/developer-tools/sdks/backend/nextjs-sdk/#kinde-management-api

kingRayhan commented 1 month ago

I tried to use that @kinde/management-api-js, but still got the credential error.

Would you please give me an idea of how to pass credentials?

import { Users } from "@kinde/management-api-js";
import { NextRequest } from "next/server";

export const POST = async (request: NextRequest) => {
  try {
    const { users } = await Users.getUsers();
    return Response.json(users);
  } catch (error) {
    return Response.json({ error: error });
  }
};

Response

{
    "error": {
        "url": "https://app.kinde.com/api/v1/users",
        "status": 403,
        "statusText": "Forbidden",
        "body": {
            "errors": [
                {
                    "code": "INVALID_CREDENTIALS",
                    "message": "Invalid credentials used to access API"
                }
            ]
        },
        "request": {
            "method": "GET",
            "url": "/api/v1/users",
            "query": {},
            "errors": {
                "403": "Invalid credentials.",
                "429": "Request was throttled."
            }
        },
        "name": "ApiError"
    }
}
kingRayhan commented 1 month ago

I found the init() function from @kinde/management-api-js library, I initialized but still now outputs going to the catch.

import { Users, init } from "@kinde/management-api-js";
import { NextRequest } from "next/server";

init({
  kindeDomain: process.env.KINDE_ISSUER_URL!,
  clientId: process.env.KINDE_CLIENT_ID,
  clientSecret: process.env.KINDE_CLIENT_SECRET!,
});

export const POST = async (request: NextRequest) => {
  try {
    const { users } = await Users.getUsers();
    return Response.json(users);
  } catch (error) {
    return Response.json({ error: JSON.stringify(error) });
  }
};

Also, I enabled the application’s access from the Kinde Management API interface CleanShot 2024-05-28 at 11  44 50@2x

DanielRivers commented 1 month ago

Can you try setting the management specific ENV variables and ensuring you're using M2M application https://github.com/kinde-oss/management-api-js#configuration

kingRayhan commented 1 month ago

@DanielRivers thanks man, finally works 😍