From 3445b5e9a20ceca1f1317c70bf21cbcf81667dee Mon Sep 17 00:00:00 2001 From: Nicolas Lopes Date: Tue, 3 Sep 2024 16:01:01 -0300 Subject: [PATCH] feat: add new method on users' client to delete passkey --- user/api.go | 5 +++++ user/client.go | 12 ++++++++++++ user/client_test.go | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/user/api.go b/user/api.go index 56149a3..eed259f 100644 --- a/user/api.go +++ b/user/api.go @@ -90,6 +90,11 @@ func ListOrganizationMemberships(ctx context.Context, id string, params *ListOrg return getClient().ListOrganizationMemberships(ctx, id, params) } +// DeletePasskey deletes a passkey by it's identification ID. +func DeletePasskey(ctx context.Context, userID, identificationID string) (*clerk.DeletedResource, error) { + return getClient().DeletePasskey(ctx, userID, identificationID) +} + func getClient() *Client { return &Client{ Backend: clerk.GetBackend(), diff --git a/user/client.go b/user/client.go index b6cd72a..84d114e 100644 --- a/user/client.go +++ b/user/client.go @@ -428,3 +428,15 @@ func (c *Client) ListOrganizationMemberships(ctx context.Context, id string, par err = c.Backend.Call(ctx, req, list) return list, err } + +// DeletePasskey deletes a passkey by it's identification ID. +func (c *Client) DeletePasskey(ctx context.Context, userID, identificationID string) (*clerk.DeletedResource, error) { + path, err := clerk.JoinPath(path, userID, "/passkeys", identificationID) + if err != nil { + return nil, err + } + req := clerk.NewAPIRequest(http.MethodDelete, path) + resource := &clerk.DeletedResource{} + err = c.Backend.Call(ctx, req, resource) + return resource, err +} diff --git a/user/client_test.go b/user/client_test.go index fe5602c..dbab216 100644 --- a/user/client_test.go +++ b/user/client_test.go @@ -422,3 +422,22 @@ func TestUserClientListOrganizationMemberships(t *testing.T) { require.Equal(t, organizationID, list.OrganizationMemberships[0].Organization.ID) require.Equal(t, userID, list.OrganizationMemberships[0].PublicUserData.UserID) } + +func TestUserClientDeletePasskey(t *testing.T) { + t.Parallel() + userID := "user_123" + passkeyIdentificationID := "idn_345" + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + Out: json.RawMessage(fmt.Sprintf(`{"id":"%s"}`, passkeyIdentificationID)), + Method: http.MethodDelete, + Path: "/v1/users/" + userID + "/passkeys/" + passkeyIdentificationID, + }, + } + client := NewClient(config) + passkey, err := client.DeletePasskey(context.Background(), userID, passkeyIdentificationID) + require.NoError(t, err) + require.Equal(t, passkeyIdentificationID, passkey.ID) +}