Skip to content

Commit 1bbcf2c

Browse files
committed
Snippets for new auth APIs
1 parent 356a8c1 commit 1bbcf2c

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseAuthSnippets.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,56 @@ 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 privilege 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+
// [END verify_custom_claims]
104+
105+
// [START read_custom_user_claims]
106+
// Lookup the user associated with the specified uid.
107+
UserRecord user = await FirebaseAuth.DefaultInstance.GetUserAsync(uid);
108+
Console.WriteLine(user.CustomClaims["admin"]);
109+
// [END read_custom_user_claims]
110+
}
60111
}
61112
}

0 commit comments

Comments
 (0)