From 1249d21c433a9aa4761b03475e1270f7445e9fe5 Mon Sep 17 00:00:00 2001 From: Maina Wycliffe Date: Mon, 10 Feb 2020 02:14:56 +0300 Subject: [PATCH] feat: add function to get firebase users --- firebase/auth/auth.go | 5 ----- firebase/auth/listUsers.go | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 firebase/auth/listUsers.go diff --git a/firebase/auth/auth.go b/firebase/auth/auth.go index b323eb0..9c02dd1 100644 --- a/firebase/auth/auth.go +++ b/firebase/auth/auth.go @@ -84,8 +84,3 @@ func DeleteFirebaseUser(ctx context.Context, uid string) error { } return nil } - -// ListAllFirebaseUsers get all users in firebase auth -func ListAllFirebaseUsers(ctx context.Context, maxResults uint32, nextPageToken string) error { - panic("not implemented") -} diff --git a/firebase/auth/listUsers.go b/firebase/auth/listUsers.go new file mode 100644 index 0000000..b036104 --- /dev/null +++ b/firebase/auth/listUsers.go @@ -0,0 +1,40 @@ +package auth + +import ( + "context" + "fmt" + + "firebase.google.com/go/auth" + "github.com/mainawycliffe/kamanda/firebase" + "google.golang.org/api/iterator" +) + +type ListUsersResponse struct { + Users []*auth.ExportedUserRecord + nextPageToken string +} + +// ListUsers get all users in firebase auth +func ListUsers(ctx context.Context, maxSize int, nextPageToken string) (ListUsersResponse, error) { + client, err := firebase.Auth(ctx, "") + if err != nil { + return ListUsersResponse{}, fmt.Errorf("Error authenticating firebase account: %w", err) + } + usersIterator := client.Users(ctx, nextPageToken) + users := make([]*auth.ExportedUserRecord, 0) + for { + user, err := usersIterator.Next() + if err == iterator.Done { + break + } + if err != nil { + return ListUsersResponse{}, err + } + users = append(users, user) + } + response := ListUsersResponse{ + Users: users, + nextPageToken: usersIterator.PageInfo().Token, + } + return response, nil +}