Skip to content

Commit bfd8fa9

Browse files
authored
Merge pull request #81 from firebase/hkj-auth-snippets
Added snippets for new user management APIs
2 parents 31b9a13 + 534e838 commit bfd8fa9

File tree

3 files changed

+130
-10
lines changed

3 files changed

+130
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22

3+
- [added] Implemented the `ListUsersAsync()` API.
34
- [added] Implemented the `UpdateUserAsync()` API.
45
- [added] Implemented the `CreateUserAsync()` and `UserRecordArgs` APIs.
56

FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseAuthSnippets.cs

Lines changed: 121 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal static async Task GetUserAsync(string uid)
6363
// [START get_user_by_id]
6464
UserRecord userRecord = await FirebaseAuth.DefaultInstance.GetUserAsync(uid);
6565
// See the UserRecord reference doc for the contents of userRecord.
66-
Console.WriteLine("Successfully fetched user data: {0}", userRecord.Uid);
66+
Console.WriteLine($"Successfully fetched user data: {userRecord.Uid}");
6767
// [END get_user_by_id]
6868
}
6969

@@ -72,7 +72,7 @@ internal static async Task GetUserByEmailAsync(string email)
7272
// [START get_user_by_email]
7373
UserRecord userRecord = await FirebaseAuth.DefaultInstance.GetUserByEmailAsync(email);
7474
// See the UserRecord reference doc for the contents of userRecord.
75-
Console.WriteLine("Successfully fetched user data: {0}", userRecord.Uid);
75+
Console.WriteLine($"Successfully fetched user data: {userRecord.Uid}");
7676
// [END get_user_by_email]
7777
}
7878

@@ -81,10 +81,64 @@ internal static async Task GetUserByPhoneNumberAsync(string phoneNumber)
8181
// [START get_user_by_phone]
8282
UserRecord userRecord = await FirebaseAuth.DefaultInstance.GetUserByPhoneNumberAsync(phoneNumber);
8383
// See the UserRecord reference doc for the contents of userRecord.
84-
Console.WriteLine("Successfully fetched user data: {0}", userRecord.Uid);
84+
Console.WriteLine($"Successfully fetched user data: {userRecord.Uid}");
8585
// [END get_user_by_phone]
8686
}
8787

88+
internal static async Task CreateUserAsync()
89+
{
90+
// [START create_user]
91+
UserRecordArgs args = new UserRecordArgs()
92+
{
93+
Email = "[email protected]",
94+
EmailVerified = false,
95+
PhoneNumber = "+11234567890",
96+
Password = "secretPassword",
97+
DisplayName = "John Doe",
98+
PhotoUrl = "http://www.example.com/12345678/photo.png",
99+
Disabled = false,
100+
};
101+
UserRecord userRecord = await FirebaseAuth.DefaultInstance.CreateUserAsync(args);
102+
// See the UserRecord reference doc for the contents of userRecord.
103+
Console.WriteLine($"Successfully created new user: {userRecord.Uid}");
104+
// [END create_user]
105+
}
106+
107+
internal static async Task CreateUserWithUidAsync()
108+
{
109+
// [START create_user_with_uid]
110+
UserRecordArgs args = new UserRecordArgs()
111+
{
112+
Uid = "some-uid",
113+
Email = "[email protected]",
114+
PhoneNumber = "+11234567890",
115+
};
116+
UserRecord userRecord = await FirebaseAuth.DefaultInstance.CreateUserAsync(args);
117+
// See the UserRecord reference doc for the contents of userRecord.
118+
Console.WriteLine($"Successfully created new user: {userRecord.Uid}");
119+
// [END create_user_with_uid]
120+
}
121+
122+
internal static async Task UpdateUserAsync(string uid)
123+
{
124+
// [START update_user]
125+
UserRecordArgs args = new UserRecordArgs()
126+
{
127+
Uid = uid,
128+
Email = "[email protected]",
129+
PhoneNumber = "+11234567890",
130+
EmailVerified = true,
131+
Password = "newPassword",
132+
DisplayName = "Jane Doe",
133+
PhotoUrl = "http://www.example.com/12345678/photo.png",
134+
Disabled = true,
135+
};
136+
UserRecord userRecord = await FirebaseAuth.DefaultInstance.UpdateUserAsync(args);
137+
// See the UserRecord reference doc for the contents of userRecord.
138+
Console.WriteLine($"Successfully updated user: {userRecord.Uid}");
139+
// [END update_user]
140+
}
141+
88142
internal static async Task DeleteUserAsync(string uid)
89143
{
90144
// [START delete_user]
@@ -93,6 +147,33 @@ internal static async Task DeleteUserAsync(string uid)
93147
// [END delete_user]
94148
}
95149

150+
internal static async Task ListAllUsersAsync()
151+
{
152+
// [START list_all_users]
153+
// Start listing users from the beginning, 1000 at a time.
154+
var pagedEnumerable = FirebaseAuth.DefaultInstance.ListUsersAsync(null);
155+
var responses = pagedEnumerable.AsRawResponses().GetEnumerator();
156+
while (await responses.MoveNext())
157+
{
158+
ExportedUserRecords response = responses.Current;
159+
foreach (ExportedUserRecord user in response.Users)
160+
{
161+
Console.WriteLine($"User: {user.Uid}");
162+
}
163+
}
164+
165+
// Iterate through all users. This will still retrieve users in batches,
166+
// buffering no more than 1000 users in memory at a time.
167+
var enumerator = FirebaseAuth.DefaultInstance.ListUsersAsync(null).GetEnumerator();
168+
while (await enumerator.MoveNext())
169+
{
170+
ExportedUserRecord user = enumerator.Current;
171+
Console.WriteLine($"User: {user.Uid}");
172+
}
173+
174+
// [END list_all_users]
175+
}
176+
96177
internal static async Task SetCustomUserClaimsAsync(string uid)
97178
{
98179
// [START set_custom_user_claims]
@@ -127,5 +208,42 @@ internal static async Task SetCustomUserClaimsAsync(string uid)
127208
Console.WriteLine(user.CustomClaims["admin"]);
128209
// [END read_custom_user_claims]
129210
}
211+
212+
internal static async Task SetCustomUserClaimsScriptAsync()
213+
{
214+
// [START set_custom_user_claims_script]
215+
UserRecord user = await FirebaseAuth.DefaultInstance
216+
.GetUserByEmailAsync("[email protected]");
217+
// Confirm user is verified.
218+
if (user.EmailVerified)
219+
{
220+
var claims = new Dictionary<string, object>()
221+
{
222+
{ "admin", true },
223+
};
224+
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(user.Uid, claims);
225+
}
226+
227+
// [END set_custom_user_claims_script]
228+
}
229+
230+
internal static async Task SetCustomUserClaimsIncrementalAsync()
231+
{
232+
// [START set_custom_user_claims_incremental]
233+
UserRecord user = await FirebaseAuth.DefaultInstance
234+
.GetUserByEmailAsync("[email protected]");
235+
// Add incremental custom claims without overwriting the existing claims.
236+
object isAdmin;
237+
if (user.CustomClaims.TryGetValue("admin", out isAdmin) && (bool)isAdmin)
238+
{
239+
var claims = new Dictionary<string, object>(user.CustomClaims);
240+
// Add level.
241+
claims["level"] = 10;
242+
// Add custom claims for additional privileges.
243+
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(user.Uid, claims);
244+
}
245+
246+
// [END set_custom_user_claims_incremental]
247+
}
130248
}
131249
}

FirebaseAdmin/FirebaseAdmin/Auth/FirebaseAuth.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,12 @@ public async Task<FirebaseToken> VerifyIdTokenAsync(
264264
/// <summary>
265265
/// Creates a new user account with the attributes contained in the specified <see cref="UserRecordArgs"/>.
266266
/// </summary>
267-
/// <param name="args">Attributes that will be added to the new user account.</param>
267+
/// <param name="args">Attributes to add to the new user account.</param>
268268
/// <returns>A task that completes with a <see cref="UserRecord"/> representing
269269
/// the newly created user account.</returns>
270270
/// <exception cref="ArgumentNullException">If <paramref name="args"/> is null.</exception>
271271
/// <exception cref="ArgumentException">If any of the values in <paramref name="args"/> are invalid.</exception>
272-
/// <exception cref="FirebaseException">If an error occurs while creating rhe user account.</exception>
272+
/// <exception cref="FirebaseException">If an error occurs while creating the user account.</exception>
273273
public async Task<UserRecord> CreateUserAsync(UserRecordArgs args)
274274
{
275275
return await this.CreateUserAsync(args, default(CancellationToken))
@@ -279,14 +279,14 @@ public async Task<UserRecord> CreateUserAsync(UserRecordArgs args)
279279
/// <summary>
280280
/// Creates a new user account with the attributes contained in the specified <see cref="UserRecordArgs"/>.
281281
/// </summary>
282-
/// <param name="args">Attributes that will be added to the new user account.</param>
282+
/// <param name="args">Attributes to add to the new user account.</param>
283283
/// <param name="cancellationToken">A cancellation token to monitor the asynchronous
284284
/// operation.</param>
285285
/// <returns>A task that completes with a <see cref="UserRecord"/> representing
286286
/// the newly created user account.</returns>
287287
/// <exception cref="ArgumentNullException">If <paramref name="args"/> is null.</exception>
288288
/// <exception cref="ArgumentException">If any of the values in <paramref name="args"/> are invalid.</exception>
289-
/// <exception cref="FirebaseException">If an error occurs while creating rhe user account.</exception>
289+
/// <exception cref="FirebaseException">If an error occurs while creating the user account.</exception>
290290
public async Task<UserRecord> CreateUserAsync(
291291
UserRecordArgs args, CancellationToken cancellationToken)
292292
{
@@ -405,7 +405,7 @@ public async Task<UserRecord> GetUserByPhoneNumberAsync(
405405
/// Updates an existing user account with the attributes contained in the specified <see cref="UserRecordArgs"/>.
406406
/// The <see cref="UserRecordArgs.Uid"/> property must be specified.
407407
/// </summary>
408-
/// <param name="args">Attributes that will be updated.</param>
408+
/// <param name="args">The attributes to update.</param>
409409
/// <returns>A task that completes with a <see cref="UserRecord"/> representing
410410
/// the updated user account.</returns>
411411
/// <exception cref="ArgumentNullException">If <paramref name="args"/> is null.</exception>
@@ -421,7 +421,7 @@ public async Task<UserRecord> UpdateUserAsync(UserRecordArgs args)
421421
/// Updates an existing user account with the attributes contained in the specified <see cref="UserRecordArgs"/>.
422422
/// The <see cref="UserRecordArgs.Uid"/> property must be specified.
423423
/// </summary>
424-
/// <param name="args">Attributes that will be updated.</param>
424+
/// <param name="args">The attributes to update.</param>
425425
/// <param name="cancellationToken">A cancellation token to monitor the asynchronous
426426
/// operation.</param>
427427
/// <returns>A task that completes with a <see cref="UserRecord"/> representing
@@ -527,7 +527,8 @@ public async Task SetCustomUserClaimsAsync(
527527
/// page. See <a href="https://googleapis.github.io/google-cloud-dotnet/docs/guides/page-streaming.html">
528528
/// Page Streaming</a> for more details on how to use this API.
529529
/// </summary>
530-
/// <param name="options">The options to control the starting point and page size.</param>
530+
/// <param name="options">The options to control the starting point and page size. Pass null
531+
/// to list from the beginning with default settings.</param>
531532
/// <returns>A <see cref="PagedAsyncEnumerable{ExportedUserRecords, ExportedUserRecord}"/> instance.</returns>
532533
public PagedAsyncEnumerable<ExportedUserRecords, ExportedUserRecord> ListUsersAsync(
533534
ListUsersOptions options)

0 commit comments

Comments
 (0)