thomabig
12/02/2025, 1:05 AMconst client = new MedplumClient({
baseUrl,
clientId,
clientSecret,
});
Our account creation flow is as follow, from our NestJS app:
1. POST /auth/newuser to Medplum
2. POST /auth/newpatient to Medplum
3. POST /oauth2/token (manual HTTP call) to Medplum
4. client.readResource('Patient', ...)
5. client.updateResource(...)
π The first time the flow runs: everything works.
π The second time: /auth/newuser creates the User in the wrong project (Super Admin project instead of the clientApplication project).
βΈ»
π§ Root cause
We discovered that:
await client.readResource('Patient', id);
implicitly authenticates the MedplumClient using client_credentials.
After this call, client.sessionDetails is populated with a ClientApplication session.
"sessionDetails":
{
"project":
{
"resourceType": "*",
"id": "****",
"name": "*"
},
"membership":
{
"resourceType": "ProjectMembership",
"id": "****",
"user": {...}
"profile":
{
...
},
"admin": true
},
From then on, /auth/newuser is no longer anonymous and behaves differently β Users get created in the wrong project (whereas session details shows that the session is linked to another project.
β Questions
What Is the recommended pattern:
β’ Create one new client per HTTP request instead of having a singleton in the service ?
βοΈ Workaround
this.medplum.clear(); reinitialisation works
Thanks for your help.Maddy
12/03/2025, 12:44 AM