@@ -39,8 +39,8 @@ private FirebaseAuth(FirebaseApp app)
3939 ( ) => FirebaseTokenFactory . Create ( this . app ) , true ) ;
4040 this . idTokenVerifier = new Lazy < FirebaseTokenVerifier > (
4141 ( ) => FirebaseTokenVerifier . CreateIDTokenVerifier ( this . app ) , true ) ;
42- this . userManager = new Lazy < FirebaseUserManager > ( ( ) =>
43- FirebaseUserManager . Create ( this . app ) ) ;
42+ this . userManager = new Lazy < FirebaseUserManager > (
43+ ( ) => FirebaseUserManager . Create ( this . app ) , true ) ;
4444 }
4545
4646 /// <summary>
@@ -211,17 +211,7 @@ public async Task<string> CreateCustomTokenAsync(
211211 IDictionary < string , object > developerClaims ,
212212 CancellationToken cancellationToken )
213213 {
214- FirebaseTokenFactory tokenFactory ;
215- lock ( this . authLock )
216- {
217- if ( this . deleted )
218- {
219- throw new InvalidOperationException ( "Cannot invoke after deleting the app." ) ;
220- }
221-
222- tokenFactory = this . tokenFactory . Value ;
223- }
224-
214+ var tokenFactory = this . IfNotDeleted ( ( ) => this . tokenFactory . Value ) ;
225215 return await tokenFactory . CreateCustomTokenAsync (
226216 uid , developerClaims , cancellationToken ) . ConfigureAwait ( false ) ;
227217 }
@@ -268,15 +258,8 @@ public async Task<FirebaseToken> VerifyIdTokenAsync(string idToken)
268258 public async Task < FirebaseToken > VerifyIdTokenAsync (
269259 string idToken , CancellationToken cancellationToken )
270260 {
271- lock ( this . authLock )
272- {
273- if ( this . deleted )
274- {
275- throw new InvalidOperationException ( "Cannot invoke after deleting the app." ) ;
276- }
277- }
278-
279- return await this . idTokenVerifier . Value . VerifyTokenAsync ( idToken , cancellationToken )
261+ var idTokenVerifier = this . IfNotDeleted ( ( ) => this . idTokenVerifier . Value ) ;
262+ return await idTokenVerifier . VerifyTokenAsync ( idToken , cancellationToken )
280263 . ConfigureAwait ( false ) ;
281264 }
282265
@@ -295,22 +278,39 @@ public async Task<FirebaseToken> VerifyIdTokenAsync(
295278 /// <param name="claims">The claims to be stored on the user account, and made
296279 /// available to Firebase security rules. These must be serializable to JSON, and after
297280 /// serialization it should not be larger than 1000 characters.</param>
298- public async Task SetCustomUserClaimsAsync ( string uid , IReadOnlyDictionary < string , object > claims )
281+ public async Task SetCustomUserClaimsAsync (
282+ string uid , IReadOnlyDictionary < string , object > claims )
299283 {
300- lock ( this . authLock )
301- {
302- if ( this . deleted )
303- {
304- throw new InvalidOperationException ( "Cannot invoke after deleting the app." ) ;
305- }
306- }
284+ await this . SetCustomUserClaimsAsync ( uid , claims , default ( CancellationToken ) ) ;
285+ }
307286
287+ /// <summary>
288+ /// Sets the specified custom claims on an existing user account. A null claims value
289+ /// removes any claims currently set on the user account. The claims should serialize into
290+ /// a valid JSON string. The serialized claims must not be larger than 1000 characters.
291+ /// </summary>
292+ /// <returns>A task that completes when the claims have been set.</returns>
293+ /// <exception cref="ArgumentException">If <paramref name="uid"/> is null, empty or longer
294+ /// than 128 characters. Or, if the serialized <paramref name="claims"/> is larger than 1000
295+ /// characters.</exception>
296+ /// <param name="uid">The user ID string for the custom claims will be set. Must not be null
297+ /// or longer than 128 characters.
298+ /// </param>
299+ /// <param name="claims">The claims to be stored on the user account, and made
300+ /// available to Firebase security rules. These must be serializable to JSON, and after
301+ /// serialization it should not be larger than 1000 characters.</param>
302+ /// <param name="cancellationToken">A cancellation token to monitor the asynchronous
303+ /// operation.</param>
304+ public async Task SetCustomUserClaimsAsync (
305+ string uid , IReadOnlyDictionary < string , object > claims , CancellationToken cancellationToken )
306+ {
307+ var userManager = this . IfNotDeleted ( ( ) => this . userManager . Value ) ;
308308 var user = new UserRecord ( uid )
309309 {
310310 CustomClaims = claims ,
311311 } ;
312312
313- await this . userManager . Value . UpdateUserAsync ( user ) ;
313+ await userManager . UpdateUserAsync ( user , cancellationToken ) . ConfigureAwait ( false ) ;
314314 }
315315
316316 /// <summary>
@@ -332,5 +332,18 @@ void IFirebaseService.Delete()
332332 }
333333 }
334334 }
335+
336+ private TResult IfNotDeleted < TResult > ( Func < TResult > func )
337+ {
338+ lock ( this . authLock )
339+ {
340+ if ( this . deleted )
341+ {
342+ throw new InvalidOperationException ( "Cannot invoke after deleting the app." ) ;
343+ }
344+
345+ return func ( ) ;
346+ }
347+ }
335348 }
336349}
0 commit comments