I'm using Clerk to do user auth for my app's back office and i applied an organization on a user with the admin role on Clerk's dashboard. However when inside the Clerk middleware and trying to access user infos it shows the user but the orgId is undefined.
First, i applied org to user on Clerk dashboard :

Tried console.log the user infos with the auth() method provided in the afterAuth option :
import { authMiddleware, redirectToSignUp } from "@clerk/nextjs";
export default authMiddleware({
afterAuth(auth, req, evt) {
console.log(auth)
},
publicRoutes: [
"/api/albums/all",
"/",
"/player/(.*)",
"/login"
],
});
export const config = {
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
};
This is the console.log result :
{
actor: undefined,
sessionClaims: {
azp: 'http://localhost:3000',
exp: 1698860167,
iat: 1698860107,
iss: 'https://able-pelican-18.clerk.accounts.dev',
nbf: 1698860097,
sid: 'sess_2XaGCNVMHEqLEujCfqDe88xxVo7',
sub: 'user_2XaG41***********RM8'
},
sessionId: 'sess_2XaGCNVMHEqLEujCfqDe88xxVo7',
session: undefined,
userId: 'user_2XaG41***********RM8',
user: undefined,
orgId: undefined,
orgRole: undefined,
orgSlug: undefined,
organization: undefined,
getToken: [Function],
debug: [Function],
isPublicRoute: true,
isApiRoute: true
}
By default, the user, after logging in to your application, won't have a predefined org. To switch to a specific organization, you could use the Organization Switcher in the UI of your application, and if you want to handle the organization switcher directly with your code, you can use the setActive function of this hook. I haven't found a way to set up an organization automatically using backend code.