javascript icon indicating copy to clipboard operation
javascript copied to clipboard

Organisation created with @clerk/clerk-sdk-node not returning for user for useOrganization() from @clerk/nextjs

Open tomheather-50 opened this issue 3 years ago • 4 comments

Organisation created with @clerk/clerk-sdk-node not returning for user for useOrganization() from @clerk/nextjs

Flow as follows:

Request is made to backend api from an authorised active session


import { Response, Request, NextFunction } from "express";
import clerk from "@clerk/clerk-sdk-node";

export default () => {
    
     /**
     * @route POST /api/organisations/create
    */

    const createOrganisation = async (req: Request, res: Response, next: NextFunction) => {

        const sessionId = req && req.auth && req.auth.sessionId;

        if (sessionId) {
            const session = await clerk.sessions.getSession(sessionId);
            const clerkUserId = session.userId;
            console.log("session", session);
            console.log("clerkUserId", clerkUserId);
            try {
                const { name, subDomain } = req.body;
               
                const response = await clerk.organizations.createOrganization({
                  createdBy: clerkUserId.toString(),
                    slug: subDomain,
                    name: name,
                     publicMetadata: {},
                     privateMetadata: {}
                 });
                console.log("createOrganization response", response);
                res.status(200).json({ success: true, message: "Organization created successfully", data: response });
             } catch (error) {
                console.log("createOrganization error", error);
                res.status(500).json({ success: false, message: "Failed to create organization", error, body: req.body });
             }
         } else {
             console.log("no session");
             res.status(401).json({ success: false, message: "Unauthorized" });
         }
 
     };
};

Session is successful and clerk.organizations.createOrganization also successful - it creates the organisation as seen from the clerk dashboard.

Screenshot 2023-08-17 at 22 31 48 Screenshot 2023-08-17 at 22 32 05 Frontend code
const {
    organization,
  } = useOrganization();
  
  ...
  console.log({ organization });
Screenshot 2023-08-17 at 22 33 01

Frontend API key from your application dashboard. pk_test_bmF0aW9uYWwtZWxlcGhhbnQtMzEuY2xlcmsuYWNjb3VudHMuZGV2JA

Dependencies + versions

Frontend

"dependencies": {
    "@clerk/nextjs": "^4.23.2",
}

Backend

"dependencies": {
    "@clerk/clerk-sdk-node": "^4.12.2",
}

tomheather-50 avatar Aug 18 '23 04:08 tomheather-50

The user almost certainly needs to select an active Organization through the component or have it set programmatically with the setActive() method from the useOrganizationList() hook.

royanger avatar Aug 18 '23 05:08 royanger

It is worth noting that you can only set an organization as active on the client. As @royanger mentioned you can use the setActive function.

You could do the following


fetch('/api/organisations/create',{body: <YOUR-DATA>}).then(organization=> setActive(organization.id))

panteliselef avatar Aug 18 '23 08:08 panteliselef

@panteliselef awesome that clears that up - would be nice to set an active org through the backend - any reason why that can't be done ?

tomheather-50 avatar Aug 18 '23 15:08 tomheather-50

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

clerk-cookie avatar Aug 18 '24 00:08 clerk-cookie