Skip to content

Commit ac07281

Browse files
authored
Migrating DeleteUser() API to the internal.HTTPClient (#240)
1 parent 1ddd6f4 commit ac07281

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

auth/user_mgt.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727

2828
"firebase.google.com/go/internal"
2929
"google.golang.org/api/googleapi"
30-
"google.golang.org/api/identitytoolkit/v3"
3130
)
3231

3332
const (
@@ -297,23 +296,6 @@ func (u *UserToUpdate) validatedRequest() (map[string]interface{}, error) {
297296
return req, nil
298297
}
299298

300-
// DeleteUser deletes the user by the given UID.
301-
func (c *Client) DeleteUser(ctx context.Context, uid string) error {
302-
if err := validateUID(uid); err != nil {
303-
return err
304-
}
305-
request := &identitytoolkit.IdentitytoolkitRelyingpartyDeleteAccountRequest{
306-
LocalId: uid,
307-
}
308-
309-
call := c.is.Relyingparty.DeleteAccount(request)
310-
c.setHeader(call)
311-
if _, err := call.Context(ctx).Do(); err != nil {
312-
return handleServerError(err)
313-
}
314-
return nil
315-
}
316-
317299
// RevokeRefreshTokens revokes all refresh tokens issued to a user.
318300
//
319301
// RevokeRefreshTokens updates the user's TokensValidAfterMillis to the current UTC second.
@@ -726,6 +708,26 @@ func (c *userManagementClient) updateUser(ctx context.Context, uid string, user
726708
return nil
727709
}
728710

711+
// DeleteUser deletes the user by the given UID.
712+
func (c *userManagementClient) DeleteUser(ctx context.Context, uid string) error {
713+
if err := validateUID(uid); err != nil {
714+
return err
715+
}
716+
717+
payload := map[string]interface{}{
718+
"localId": uid,
719+
}
720+
resp, err := c.post(ctx, "/accounts:delete", payload)
721+
if err != nil {
722+
return err
723+
}
724+
725+
if resp.Status != http.StatusOK {
726+
return handleHTTPError(resp)
727+
}
728+
return nil
729+
}
730+
729731
// SessionCookie creates a new Firebase session cookie from the given ID token and expiry
730732
// duration. The returned JWT can be set as a server-side session cookie with a custom cookie
731733
// policy. Expiry duration must be at least 5 minutes but may not exceed 14 days.

auth/user_mgt_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"net/http"
2525
"net/http/httptest"
2626
"reflect"
27+
"strconv"
2728
"strings"
2829
"testing"
2930
"time"
@@ -518,6 +519,7 @@ func TestUpdateUser(t *testing.T) {
518519
}
519520
}
520521
}
522+
521523
func TestRevokeRefreshTokens(t *testing.T) {
522524
resp := `{
523525
"kind": "identitytoolkit#SetAccountInfoResponse",
@@ -531,12 +533,20 @@ func TestRevokeRefreshTokens(t *testing.T) {
531533
}
532534
after := time.Now().Unix()
533535

534-
req := &identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest{}
536+
var req struct {
537+
ValidSince string `json:"validSince"`
538+
}
535539
if err := json.Unmarshal(s.Rbody, &req); err != nil {
536-
t.Error(err)
540+
t.Fatal(err)
537541
}
538-
if req.ValidSince > after || req.ValidSince < before {
539-
t.Errorf("validSince = %d, expecting time between %d and %d", req.ValidSince, before, after)
542+
543+
validSince, err := strconv.ParseInt(req.ValidSince, 10, 64)
544+
if err != nil {
545+
t.Fatal(err)
546+
}
547+
548+
if validSince > after || validSince < before {
549+
t.Errorf("validSince = %d, expecting time between %d and %d", validSince, before, after)
540550
}
541551
}
542552

0 commit comments

Comments
 (0)