Organisation created with @clerk/clerk-sdk-node not returning for user for useOrganization() from @clerk/nextjs
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.
const {
organization,
} = useOrganization();
...
console.log({ organization });
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",
}
The user almost certainly needs to select an active Organization through the
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 awesome that clears that up - would be nice to set an active org through the backend - any reason why that can't be done ?
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.