clerk / clerk-sdk-go

Access the Clerk Backend API from Go
MIT License
79 stars 19 forks source link

UpdateMetadata appears to fail silently. #272

Open bnewland123 opened 6 months ago

bnewland123 commented 6 months ago

I'm testing updating user metadata with the SDK. The call appears to succeed with no error, but the user is not updated. Is there something else that I'm missing here? I've also made sure the ID is the correct one.

privateStuff := map[string]interface{}{
    "pid": "<id>",
}
publicStuff := map[string]interface{}{
    "likes": "videogames",
}
unsafe := make(map[string]interface{})
privateBytes, err := json.Marshal(privateStuff)
if err != nil {
    log.Error(err)
    return nil, err
}
publicBytes, err := json.Marshal(publicStuff)
if err != nil {
    log.Error(err)
    return nil, err
}
unsafeBytes, err := json.Marshal(unsafe)
if err != nil {
    log.Error(err)
    return nil, err
}
log.Infof("Updating user with ID: %s", user.ID)
updatedUser, err := client.Users().UpdateMetadata(user.ID, &clerk.UpdateUserMetadata{
    PrivateMetadata: privateBytes,
    PublicMetadata:  publicBytes,
    UnsafeMetadata:  unsafeBytes,
})
gkats commented 6 months ago

Thanks for reporting @bnewland123. This is indeed a bug.

The issue is fixed in the v2 version of the Go SDK. https://github.com/clerk/clerk-sdk-go/tree/v2

We'll take a look into the problem and try to fix it for v1 as well.

reginaldl commented 5 months ago

@gkats I have the same problem with v2:

        data, err := json.Marshal(publicMetadata)
        if err != nil {
                return err 
        }   
        rawMessage := json.RawMessage(data)
        u, err := user.UpdateMetadata(
                context.Background(),
                userId,
                &user.UpdateMetadataParams{
                        PublicMetadata: &rawMessage,
                },  
        )   
        log.Printf("%v", string(u.Response.RawJSON))
        return err 

Same behavior as described earlier, succeeds but won't update. Switching to user.Update works though. Also, is v2 still in beta?

reginaldl commented 5 months ago

So it turns out to be a backend issue and X-Clerk-Sdk header, I can repro with the following curl command:

curl -vvv -X PATCH 'https://api.clerk.com/v1/users/<user_id>/metadata' \
-H 'Authorization: Bearer <secret>' \
-H 'Content-Type: application/json' \
-H 'User-Agent: clerk/clerk-sdk-go@v2.0.0' \
-H 'X-Clerk-Sdk: go/v2.0.0' \
-d '{"public_metadata":{"test":"ok"}}'

This won't work. Remove X-Clerk-Sdk header and it works.

gkats commented 5 months ago

Hello @reginaldl thank you for reporting. This was indeed a backend issue as you found out yourself.

The fix is now live.

Also, is v2 still in beta?

Yes, v2 is still in beta, at least officially. It's pretty stable though and we don't expect the APIs to change. The only thing that's missing is cookie-based authentication support.

reginaldl commented 5 months ago

Great! Thanks for the update. Let's close this ticket.

pedro-sharpi commented 1 week ago

I'm facing the same issue in the v2 sdk. Any solutions?