internal async Task <TUser> FindByIdAsync(string userId, CancellationToken cancellationToken) { var snapshot = await UsersSet.Document(userId).GetSnapshotAsync(cancellationToken).ConfigureAwait(false); if (snapshot.Exists) { return(snapshot.ToDictionary().ToObject <TUser>()); } return(default);
internal async Task <IdentityResult> CreateAsync(TUser user, CancellationToken cancellationToken = default) { // create a new document reference and use the generated Id var doc = UsersSet.Document(); user.Id = doc.Id; // save the user document await doc.CreateAsync(user.ToDictionary(), cancellationToken : cancellationToken).ConfigureAwait(false); return(IdentityResult.Success); }
internal Task <IdentityResult> UpdateAsync(TUser user, CancellationToken cancellationToken = default) { // get the stored user document reference var doc = UsersSet.Document(user.Id); return(_db.RunTransactionAsync(async transaction => { var snapshot = await transaction.GetSnapshotAsync(doc, cancellationToken).ConfigureAwait(false); if (!snapshot.Exists || snapshot.GetValue <string>("ConcurrencyStamp") != user.ConcurrencyStamp) { return IdentityResult.Failed(_errorDescriber.ConcurrencyFailure()); } transaction.Update(doc, user.ToDictionary()); return IdentityResult.Success; }, cancellationToken: cancellationToken)); }
internal Task <IdentityResult> DeleteAsync(TUser user, CancellationToken cancellationToken = default) { // get the stored user document reference var doc = UsersSet.Document(user.Id); return(_db.RunTransactionAsync(async transaction => { var snapshot = await transaction.GetSnapshotAsync(doc, cancellationToken).ConfigureAwait(false); // check if the user was deleted or updated while running the transaction if (!snapshot.Exists || snapshot.GetValue <string>("ConcurrencyStamp") != user.ConcurrencyStamp) { return IdentityResult.Failed(_errorDescriber.ConcurrencyFailure()); } // delete the user logins var loginSnapshots = await UserLogins.WhereEqualTo("UserId", user.Id).GetSnapshotAsync(cancellationToken).ConfigureAwait(false); if (loginSnapshots.Count > 0) { foreach (var login in loginSnapshots) { transaction.Delete(login.Reference); } } // delete the user tokens var tokenSnapshots = await UserTokens.WhereEqualTo("UserId", user.Id).GetSnapshotAsync(cancellationToken).ConfigureAwait(false); if (tokenSnapshots.Count > 0) { foreach (var token in tokenSnapshots) { transaction.Delete(token.Reference); } } // delete the user claims transaction.Delete(UserClaims.Document(user.Id)); transaction.Delete(doc); return IdentityResult.Success; }, cancellationToken: cancellationToken)); }