Skip to content

Commit

Permalink
feat(user): implement delete external account method (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasLopes7 authored Oct 10, 2024
1 parent f6dae3f commit ad53b31
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions user/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions user/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,21 @@ func (c *Client) DeleteBackupCode(ctx context.Context, userID string) (*Multifac
err = c.Backend.Call(ctx, req, resource)
return resource, err
}

type DeleteExternalAccountParams struct {
clerk.APIParams
UserID string `json:"-"`
ID string `json:"-"`
}

// DeleteExternalAccount deletes an external account by its ID.
func (c *Client) DeleteExternalAccount(ctx context.Context, params *DeleteExternalAccountParams) (*clerk.DeletedResource, error) {
path, err := clerk.JoinPath(path, params.UserID, "/external_accounts", params.ID)
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
}
23 changes: 23 additions & 0 deletions user/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,26 @@ func TestUserClientDeleteBackupCode(t *testing.T) {
require.NoError(t, err)
require.Equal(t, totp.UserID, userID)
}

func TestUserClientDeleteExternalAccount(t *testing.T) {
t.Parallel()
userID := "user_123"
externalAccountID := "eac_123"
config := &clerk.ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Method: http.MethodDelete,
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","object":"external_account"}`, externalAccountID)),
Path: fmt.Sprintf("/v1/users/%s/external_accounts/%s", userID, externalAccountID),
},
}
client := NewClient(config)
externalAccount, err := client.DeleteExternalAccount(context.Background(), &DeleteExternalAccountParams{
UserID: userID,
ID: externalAccountID,
})
require.NoError(t, err)
require.Equal(t, externalAccountID, externalAccount.ID)
require.Equal(t, "external_account", externalAccount.Object)
}

0 comments on commit ad53b31

Please sign in to comment.