Skip to content

Commit ff9e1df

Browse files
committed
Renamed UserArgs to UserRecordArgs; Added CreateUserAsync API
1 parent 15d8c43 commit ff9e1df

File tree

5 files changed

+355
-121
lines changed

5 files changed

+355
-121
lines changed

FirebaseAdmin/FirebaseAdmin.Tests/Auth/FirebaseUserManagerTest.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public async Task UpdateUser()
309309
{ "package", "gold" },
310310
};
311311

312-
await userManager.UpdateUserAsync(new UserArgs()
312+
await userManager.UpdateUserAsync(new UserRecordArgs()
313313
{
314314
Uid = "user1",
315315
CustomClaims = customClaims,
@@ -336,7 +336,7 @@ public async Task LargeClaimsUnderLimit()
336336
{ "testClaim", new string('a', 950) },
337337
};
338338

339-
await userManager.UpdateUserAsync(new UserArgs()
339+
await userManager.UpdateUserAsync(new UserRecordArgs()
340340
{
341341
Uid = "user1",
342342
CustomClaims = customClaims,
@@ -352,7 +352,7 @@ public async Task EmptyClaims()
352352
};
353353
var userManager = this.CreateFirebaseUserManager(handler);
354354

355-
await userManager.UpdateUserAsync(new UserArgs()
355+
await userManager.UpdateUserAsync(new UserRecordArgs()
356356
{
357357
Uid = "user1",
358358
CustomClaims = new Dictionary<string, object>(),
@@ -372,7 +372,7 @@ public async Task NullClaims()
372372
};
373373
var userManager = this.CreateFirebaseUserManager(handler);
374374

375-
await userManager.UpdateUserAsync(new UserArgs()
375+
await userManager.UpdateUserAsync(new UserRecordArgs()
376376
{
377377
Uid = "user1",
378378
CustomClaims = null,
@@ -399,7 +399,7 @@ public void ReservedClaims()
399399
{ key, "value" },
400400
};
401401

402-
var args = new UserArgs()
402+
var args = new UserRecordArgs()
403403
{
404404
Uid = "user1",
405405
CustomClaims = customClaims,
@@ -421,7 +421,7 @@ public void UpdateUserNoUid()
421421
{ "key", "value" },
422422
};
423423

424-
var args = new UserArgs()
424+
var args = new UserRecordArgs()
425425
{
426426
CustomClaims = customClaims,
427427
};
@@ -441,7 +441,7 @@ public void UpdateUserInvalidUid()
441441
{ "key", "value" },
442442
};
443443

444-
var args = new UserArgs()
444+
var args = new UserRecordArgs()
445445
{
446446
Uid = new string('a', 129),
447447
CustomClaims = customClaims,
@@ -462,7 +462,7 @@ public void EmptyNameClaims()
462462
{ string.Empty, "value" },
463463
};
464464

465-
var args = new UserArgs()
465+
var args = new UserRecordArgs()
466466
{
467467
Uid = "user1",
468468
CustomClaims = emptyClaims,
@@ -483,7 +483,7 @@ public void LargeClaimsOverLimit()
483483
{ "testClaim", new string('a', 1001) },
484484
};
485485

486-
var args = new UserArgs()
486+
var args = new UserRecordArgs()
487487
{
488488
Uid = "user1",
489489
CustomClaims = largeClaims,
@@ -504,7 +504,7 @@ public async Task UpdateUserIncorrectResponseObject()
504504
{ "admin", true },
505505
};
506506

507-
var args = new UserArgs()
507+
var args = new UserRecordArgs()
508508
{
509509
Uid = "user1",
510510
CustomClaims = customClaims,
@@ -525,7 +525,7 @@ public async Task UpdateUserIncorrectResponseUid()
525525
{ "admin", true },
526526
};
527527

528-
var args = new UserArgs()
528+
var args = new UserRecordArgs()
529529
{
530530
Uid = "user1",
531531
CustomClaims = customClaims,
@@ -545,7 +545,7 @@ public async Task UpdateUserHttpError()
545545
{
546546
{ "admin", true },
547547
};
548-
var args = new UserArgs()
548+
var args = new UserRecordArgs()
549549
{
550550
Uid = "user1",
551551
CustomClaims = customClaims,

FirebaseAdmin/FirebaseAdmin/Auth/FirebaseAuth.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,41 @@ public async Task<FirebaseToken> VerifyIdTokenAsync(
263263
.ConfigureAwait(false);
264264
}
265265

266+
/// <summary>
267+
/// Creates a new user account with the attributes contained in the specified <see cref="UserRecordArgs"/>.
268+
/// </summary>
269+
/// <param name="args">Attributes that will be added to the new user account.</param>
270+
/// <returns>A task that completes with a <see cref="UserRecord"/> representing
271+
/// the newly created user account.</returns>
272+
/// <exception cref="ArgumentNullException">If <paramref name="args"/> is null.</exception>
273+
/// <exception cref="ArgumentException">If any of the values in <paramref name="args"/> are invalid.</exception>
274+
/// <exception cref="FirebaseException">If an error occurs while creating rhe user account.</exception>
275+
public async Task<UserRecord> CreateUserAsync(UserRecordArgs args)
276+
{
277+
return await this.CreateUserAsync(args, default(CancellationToken))
278+
.ConfigureAwait(false);
279+
}
280+
281+
/// <summary>
282+
/// Creates a new user account with the attributes contained in the specified <see cref="UserRecordArgs"/>.
283+
/// </summary>
284+
/// <param name="args">Attributes that will be added to the new user account.</param>
285+
/// <param name="cancellationToken">A cancellation token to monitor the asynchronous
286+
/// operation.</param>
287+
/// <returns>A task that completes with a <see cref="UserRecord"/> representing
288+
/// the newly created user account.</returns>
289+
/// <exception cref="ArgumentNullException">If <paramref name="args"/> is null.</exception>
290+
/// <exception cref="ArgumentException">If any of the values in <paramref name="args"/> are invalid.</exception>
291+
/// <exception cref="FirebaseException">If an error occurs while creating rhe user account.</exception>
292+
public async Task<UserRecord> CreateUserAsync(
293+
UserRecordArgs args, CancellationToken cancellationToken)
294+
{
295+
var userManager = this.IfNotDeleted(() => this.userManager.Value);
296+
var uid = await userManager.CreateUserAsync(args, cancellationToken)
297+
.ConfigureAwait(false);
298+
return await userManager.GetUserById(uid, cancellationToken).ConfigureAwait(false);
299+
}
300+
266301
/// <summary>
267302
/// Gets a <see cref="UserRecord"/> object containing information about the user who's
268303
/// user ID was specified in <paramref name="uid"/>.
@@ -432,7 +467,7 @@ public async Task SetCustomUserClaimsAsync(
432467
string uid, IReadOnlyDictionary<string, object> claims, CancellationToken cancellationToken)
433468
{
434469
var userManager = this.IfNotDeleted(() => this.userManager.Value);
435-
var user = new UserArgs()
470+
var user = new UserRecordArgs()
436471
{
437472
Uid = uid,
438473
CustomClaims = claims,

FirebaseAdmin/FirebaseAdmin/Auth/FirebaseUserManager.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Google.Apis.Auth.OAuth2;
2121
using Google.Apis.Http;
2222
using Google.Apis.Json;
23+
using Google.Apis.Util;
2324
using Newtonsoft.Json.Linq;
2425

2526
namespace FirebaseAdmin.Auth
@@ -137,6 +138,29 @@ internal async Task<UserRecord> GetUserByPhoneNumber(
137138
return await this.GetUserAsync(query, cancellationToken);
138139
}
139140

141+
/// <summary>
142+
/// Create a new user account.
143+
/// </summary>
144+
/// <exception cref="FirebaseException">If an error occurs while creating the user account.</exception>
145+
/// <param name="args">The data to create the user account with.</param>
146+
/// <param name="cancellationToken">A cancellation token to monitor the asynchronous
147+
/// operation.</param>
148+
/// <returns>The unique uid assigned to the newly created user account.</returns>
149+
internal async Task<string> CreateUserAsync(
150+
UserRecordArgs args, CancellationToken cancellationToken = default(CancellationToken))
151+
{
152+
var payload = args.ThrowIfNull(nameof(args)).ToCreateUserRequest();
153+
var response = await this.PostAndDeserializeAsync<JObject>(
154+
"accounts", payload, cancellationToken).ConfigureAwait(false);
155+
var uid = response["localId"];
156+
if (uid == null)
157+
{
158+
throw new FirebaseException("Failed to create new user.");
159+
}
160+
161+
return uid.Value<string>();
162+
}
163+
140164
/// <summary>
141165
/// Update an existing user.
142166
/// </summary>
@@ -145,7 +169,7 @@ internal async Task<UserRecord> GetUserByPhoneNumber(
145169
/// <param name="cancellationToken">A cancellation token to monitor the asynchronous
146170
/// operation.</param>
147171
internal async Task UpdateUserAsync(
148-
UserArgs args, CancellationToken cancellationToken = default(CancellationToken))
172+
UserRecordArgs args, CancellationToken cancellationToken = default(CancellationToken))
149173
{
150174
var payload = args.ToUpdateUserRequest();
151175
var response = await this.PostAndDeserializeAsync<JObject>(

FirebaseAdmin/FirebaseAdmin/Auth/UserArgs.cs

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)