Skip to content

Commit 7e51e2c

Browse files
authored
Merge pull request #60 from firebase/hkj-user-metadata
Use nullable types for UserMetadata fields
2 parents d2f019e + 150d1d5 commit 7e51e2c

File tree

5 files changed

+64
-13
lines changed

5 files changed

+64
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Unreleased
22

3-
- [added] Implemented the `GetUserById()` API in the `FirebaseUserManager` class.
4-
5-
-
3+
- [added] Implemented the `GetUserAsync()` API in the `FirebaseAuth` class.
4+
- [added] Implemented the `DeleteUserAsync()` API in the `FirebaseAuth` class.
65

76
# v1.4.0
87

FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseAuthSnippets.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,57 @@ internal static async Task VeridyIdTokenAsync(string idToken)
5757
// [END verify_id_token]
5858
Console.WriteLine("Decoded ID token from user: {0}", uid);
5959
}
60+
61+
internal static async Task GetUserAsync(string uid)
62+
{
63+
// [START get_user_by_id]
64+
UserRecord userRecord = await FirebaseAuth.DefaultInstance.GetUserAsync(uid);
65+
// See the UserRecord reference doc for the contents of userRecord.
66+
Console.WriteLine("Successfully fetched user data: {0}", userRecord.Uid);
67+
// [END get_user_by_id]
68+
}
69+
70+
internal static async Task DeleteUserAsync(string uid)
71+
{
72+
// [START delete_user]
73+
await FirebaseAuth.DefaultInstance.DeleteUserAsync(uid);
74+
Console.WriteLine("Successfully deleted user.");
75+
// [END delete_user]
76+
}
77+
78+
internal static async Task SetCustomUserClaimsAsync(string uid)
79+
{
80+
// [START set_custom_user_claims]
81+
// Set admin privileges on the user corresponding to uid.
82+
var claims = new Dictionary<string, object>()
83+
{
84+
{ "admin", true },
85+
};
86+
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(uid, claims);
87+
// The new custom claims will propagate to the user's ID token the
88+
// next time a new one is issued.
89+
// [END set_custom_user_claims]
90+
91+
var idToken = "id_token";
92+
// [START verify_custom_claims]
93+
// Verify the ID token first.
94+
FirebaseToken decoded = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken);
95+
object isAdmin;
96+
if (decoded.Claims.TryGetValue("admin", out isAdmin))
97+
{
98+
if ((bool)isAdmin)
99+
{
100+
// Allow access to requested admin resource.
101+
}
102+
}
103+
104+
// [END verify_custom_claims]
105+
106+
// [START read_custom_user_claims]
107+
// Lookup the user associated with the specified uid.
108+
UserRecord user = await FirebaseAuth.DefaultInstance.GetUserAsync(uid);
109+
Console.WriteLine(user.CustomClaims["admin"]);
110+
// [END read_custom_user_claims]
111+
}
60112
}
61113
}

FirebaseAdmin/FirebaseAdmin.Tests/Auth/FirebaseUserManagerTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public async Task GetUserById()
5858
Assert.Equal(UserRecord.UnixEpoch, userRecord.TokensValidAfterTimestamp);
5959
Assert.Empty(userRecord.CustomClaims);
6060
Assert.Empty(userRecord.ProviderData);
61-
Assert.Equal(DateTime.MinValue, userRecord.UserMetaData.CreationTimestamp);
62-
Assert.Equal(DateTime.MinValue, userRecord.UserMetaData.LastSignInTimestamp);
61+
Assert.Null(userRecord.UserMetaData.CreationTimestamp);
62+
Assert.Null(userRecord.UserMetaData.LastSignInTimestamp);
6363
}
6464

6565
[Fact]

FirebaseAdmin/FirebaseAdmin.Tests/Auth/UserRecordTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public void UidOnly()
6464
Assert.Empty(user.CustomClaims);
6565
Assert.Empty(user.ProviderData);
6666
Assert.NotNull(user.UserMetaData);
67-
Assert.Equal(DateTime.MinValue, user.UserMetaData.CreationTimestamp);
68-
Assert.Equal(DateTime.MinValue, user.UserMetaData.LastSignInTimestamp);
67+
Assert.Null(user.UserMetaData.CreationTimestamp);
68+
Assert.Null(user.UserMetaData.LastSignInTimestamp);
6969
}
7070

7171
[Fact]

FirebaseAdmin/FirebaseAdmin/Auth/UserMetadata.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,27 @@ internal UserMetadata(long creationTimestamp, long lastSignInTimestamp)
2626

2727
/// <summary>
2828
/// Gets a timestamp representing the date and time that the account was created.
29-
/// If not available this property is <c>DateTime.MinValue</c>.
29+
/// If not available this property is <c>null</c>.
3030
/// </summary>
31-
public DateTime CreationTimestamp
31+
public DateTime? CreationTimestamp
3232
{
3333
get => this.ToDateTime(this.creationTimestampMillis);
3434
}
3535

3636
/// <summary>
3737
/// Gets a timestamp representing the last time that the user has signed in. If the user
38-
/// has never signed in this property is <c>DateTime.MinValue</c>.
38+
/// has never signed in this property is <c>null</c>.
3939
/// </summary>
40-
public DateTime LastSignInTimestamp
40+
public DateTime? LastSignInTimestamp
4141
{
4242
get => this.ToDateTime(this.lastSignInTimestampMillis);
4343
}
4444

45-
private DateTime ToDateTime(long millisFromEpoch)
45+
private DateTime? ToDateTime(long millisFromEpoch)
4646
{
4747
if (millisFromEpoch == 0)
4848
{
49-
return DateTime.MinValue;
49+
return null;
5050
}
5151

5252
return UserRecord.UnixEpoch.AddMilliseconds(millisFromEpoch);

0 commit comments

Comments
 (0)