-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathFirebaseAuthSnippets.cs
More file actions
292 lines (265 loc) · 11.8 KB
/
FirebaseAuthSnippets.cs
File metadata and controls
292 lines (265 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright 2018, Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FirebaseAdmin.Auth;
namespace FirebaseAdmin.Snippets
{
internal class FirebaseAuthSnippets
{
internal static async Task CreateCustomTokenAsync()
{
// [START custom_token]
var uid = "some-uid";
string customToken = await FirebaseAuth.DefaultInstance.CreateCustomTokenAsync(uid);
// Send token back to client
// [END custom_token]
Console.WriteLine("Created custom token: {0}", customToken);
}
internal static async Task CreateCustomTokenWithClaimsAsync()
{
// [START custom_token_with_claims]
var uid = "some-uid";
var additionalClaims = new Dictionary<string, object>()
{
{ "premiumAccount", true },
};
string customToken = await FirebaseAuth.DefaultInstance
.CreateCustomTokenAsync(uid, additionalClaims);
// Send token back to client
// [END custom_token_with_claims]
Console.WriteLine("Created custom token: {0}", customToken);
}
internal static async Task VeridyIdTokenAsync(string idToken)
{
// [START verify_id_token]
FirebaseToken decodedToken = await FirebaseAuth.DefaultInstance
.VerifyIdTokenAsync(idToken);
string uid = decodedToken.Uid;
// [END verify_id_token]
Console.WriteLine("Decoded ID token from user: {0}", uid);
}
internal static async Task GetUserAsync(string uid)
{
// [START get_user_by_id]
UserRecord userRecord = await FirebaseAuth.DefaultInstance.GetUserAsync(uid);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully fetched user data: {userRecord.Uid}");
// [END get_user_by_id]
}
internal static async Task GetUserByEmailAsync(string email)
{
// [START get_user_by_email]
UserRecord userRecord = await FirebaseAuth.DefaultInstance.GetUserByEmailAsync(email);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully fetched user data: {userRecord.Uid}");
// [END get_user_by_email]
}
internal static async Task GetUserByPhoneNumberAsync(string phoneNumber)
{
// [START get_user_by_phone]
UserRecord userRecord = await FirebaseAuth.DefaultInstance.GetUserByPhoneNumberAsync(phoneNumber);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully fetched user data: {userRecord.Uid}");
// [END get_user_by_phone]
}
internal static async Task GetUserByProviderUidAsync()
{
// [START get_user_by_federated_id]
UserRecord userRecord = await FirebaseAuth.DefaultInstance.GetUserByProviderUidAsync("google.com", "google_uid1234");
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully fetched user data: {userRecord.Uid}");
// [END get_user_by_federated_id]
}
internal static async Task CreateUserAsync()
{
// [START create_user]
UserRecordArgs args = new UserRecordArgs()
{
Email = "user@example.com",
EmailVerified = false,
PhoneNumber = "+11234567890",
Password = "secretPassword",
DisplayName = "John Doe",
PhotoUrl = "http://www.example.com/12345678/photo.png",
Disabled = false,
};
UserRecord userRecord = await FirebaseAuth.DefaultInstance.CreateUserAsync(args);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully created new user: {userRecord.Uid}");
// [END create_user]
}
internal static async Task CreateUserWithUidAsync()
{
// [START create_user_with_uid]
UserRecordArgs args = new UserRecordArgs()
{
Uid = "some-uid",
Email = "user@example.com",
PhoneNumber = "+11234567890",
};
UserRecord userRecord = await FirebaseAuth.DefaultInstance.CreateUserAsync(args);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully created new user: {userRecord.Uid}");
// [END create_user_with_uid]
}
internal static async Task UpdateUserAsync(string uid)
{
// [START update_user]
UserRecordArgs args = new UserRecordArgs()
{
Uid = uid,
Email = "modifiedUser@example.com",
PhoneNumber = "+11234567890",
EmailVerified = true,
Password = "newPassword",
DisplayName = "Jane Doe",
PhotoUrl = "http://www.example.com/12345678/photo.png",
Disabled = true,
};
UserRecord userRecord = await FirebaseAuth.DefaultInstance.UpdateUserAsync(args);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully updated user: {userRecord.Uid}");
// [END update_user]
}
internal static async Task UpdateUserFederatedAsync(string uid)
{
// [START update_user_link_federated]
// Link the user with a federated identity provider (like Google).
UserRecordArgs args = new UserRecordArgs()
{
Uid = uid,
ProviderToLink = new ProviderUserInfo()
{
ProviderId = "google.com",
Uid = "google_uid1234",
},
};
UserRecord userRecord = await FirebaseAuth.DefaultInstance.UpdateUserAsync(args);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully updated user: {userRecord.Uid}");
// [END update_user_link_federated]
// [START update_user_unlink_federated]
// Unlink the user from a federated identity provider (like Google).
UserRecordArgs args = new UserRecordArgs()
{
Uid = uid,
ProvidersToDelete = new List<string>()
{
"google.com",
},
};
UserRecord userRecord = await FirebaseAuth.DefaultInstance.UpdateUserAsync(args);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully updated user: {userRecord.Uid}");
// [END update_user_unlink_federated]
}
internal static async Task DeleteUserAsync(string uid)
{
// [START delete_user]
await FirebaseAuth.DefaultInstance.DeleteUserAsync(uid);
Console.WriteLine("Successfully deleted user.");
// [END delete_user]
}
internal static async Task ListAllUsersAsync()
{
// [START list_all_users]
// Start listing users from the beginning, 1000 at a time.
var pagedEnumerable = FirebaseAuth.DefaultInstance.ListUsersAsync(null);
var responses = pagedEnumerable.AsRawResponses().GetEnumerator();
while (await responses.MoveNext())
{
ExportedUserRecords response = responses.Current;
foreach (ExportedUserRecord user in response.Users)
{
Console.WriteLine($"User: {user.Uid}");
}
}
// Iterate through all users. This will still retrieve users in batches,
// buffering no more than 1000 users in memory at a time.
var enumerator = FirebaseAuth.DefaultInstance.ListUsersAsync(null).GetEnumerator();
while (await enumerator.MoveNext())
{
ExportedUserRecord user = enumerator.Current;
Console.WriteLine($"User: {user.Uid}");
}
// [END list_all_users]
}
internal static async Task SetCustomUserClaimsAsync(string uid)
{
// [START set_custom_user_claims]
// Set admin privileges on the user corresponding to uid.
var claims = new Dictionary<string, object>()
{
{ "admin", true },
};
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(uid, claims);
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.
// [END set_custom_user_claims]
var idToken = "id_token";
// [START verify_custom_claims]
// Verify the ID token first.
FirebaseToken decoded = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken);
object isAdmin;
if (decoded.Claims.TryGetValue("admin", out isAdmin))
{
if ((bool)isAdmin)
{
// Allow access to requested admin resource.
}
}
// [END verify_custom_claims]
// [START read_custom_user_claims]
// Lookup the user associated with the specified uid.
UserRecord user = await FirebaseAuth.DefaultInstance.GetUserAsync(uid);
Console.WriteLine(user.CustomClaims["admin"]);
// [END read_custom_user_claims]
}
internal static async Task SetCustomUserClaimsScriptAsync()
{
// [START set_custom_user_claims_script]
UserRecord user = await FirebaseAuth.DefaultInstance
.GetUserByEmailAsync("user@admin.example.com");
// Confirm user is verified.
if (user.EmailVerified)
{
var claims = new Dictionary<string, object>()
{
{ "admin", true },
};
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(user.Uid, claims);
}
// [END set_custom_user_claims_script]
}
internal static async Task SetCustomUserClaimsIncrementalAsync()
{
// [START set_custom_user_claims_incremental]
UserRecord user = await FirebaseAuth.DefaultInstance
.GetUserByEmailAsync("user@admin.example.com");
// Add incremental custom claims without overwriting the existing claims.
object isAdmin;
if (user.CustomClaims.TryGetValue("admin", out isAdmin) && (bool)isAdmin)
{
var claims = new Dictionary<string, object>(user.CustomClaims);
// Add level.
claims["level"] = 10;
// Add custom claims for additional privileges.
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(user.Uid, claims);
}
// [END set_custom_user_claims_incremental]
}
}
}