Exemplo n.º 1
0
        public async Task DeleteAsync(string uid)
        {
            // Get User
            try
            {
                await FirebaseAuthAdmin.DeleteUserAsync(uid);

                var person = await GetPersonByUserIdAsync(uid);

                if (person.Value != null)
                {
                    await FirebaseClient
                    .Child(Table)
                    .Child(person.Key)
                    .DeleteAsync();
                }
            }
            catch (FirebaseAdmin.Auth.FirebaseAuthException)
            {
                throw new KeyNotFoundException($"{nameof(User)} {uid} not found");
            }
            catch (Firebase.Database.FirebaseException ex)
            {
                if (ex.InnerException?.InnerException?.GetType() == typeof(SocketException))
                {
                    throw new HttpRequestException("Cannot join the server. Please check your internet connexion.");
                }
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
        public async Task <ListDto <UserDto> > GetUsersAsync(string pageToken = null, int pageSize = 50)
        {
            var list = new ListDto <UserDto>();

            list.PageSize = pageSize;
            //try
            //{
            var options = new FirebaseAdmin.Auth.ListUsersOptions
            {
                PageSize = 50
            };

            if (!string.IsNullOrEmpty(pageToken))
            {
                options.PageToken = pageToken;
            }

            var pagedEnumerable = FirebaseAuthAdmin.ListUsersAsync(options);
            var responses       = pagedEnumerable.AsRawResponses().GetAsyncEnumerator();

            while (await responses.MoveNextAsync())
            {
                FirebaseAdmin.Auth.ExportedUserRecords response = responses.Current;
                list.PageToken = response.NextPageToken;
                foreach (FirebaseAdmin.Auth.ExportedUserRecord user in response.Users)
                {
                    var person = await GetPersonByUserIdAsync(user.Uid);

                    list.Items.Add
                    (
                        GetUserDto
                        (
                            person.Key,
                            new User
                            (
                                user.Uid,
                                person.Value?.FullName,
                                person.Value?.PhoneCountryCode,
                                person.Value?.PhoneNumber ?? 0,
                                person.Value?.CreatedAt ?? DateTime.MinValue,
                                person.Value?.Role ?? RoleOptions.Other,
                                person.Value?.IsEmailVerified ?? false,
                                person.Value?.Status ?? false,
                                user.Email,
                                person.Value?.RegistrationNumber,
                                null
                            )
                        )
                    );
                }
            }
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}

            list.Items = list.Items.OrderByDescending(x => x.CreatedAt).ToList();
            return(list);
        }
Exemplo n.º 3
0
        internal async Task <KeyValuePair <string, User> > GetAsync(string id)
        {
            try
            {
                if (string.IsNullOrEmpty(id))
                {
                    return(new KeyValuePair <string, User>(string.Empty, null));
                }

                var user = await FirebaseAuthAdmin.GetUserAsync(id);

                var person = await GetPersonByUserIdAsync(user.Uid);

                return(new KeyValuePair <string, User>
                       (
                           person.Key,
                           new User
                           (
                               user.Uid,
                               person.Value?.FullName,
                               person.Value?.PhoneCountryCode,
                               person.Value?.PhoneNumber ?? 0,
                               person.Value?.CreatedAt ?? DateTime.MinValue,
                               person.Value?.Role ?? RoleOptions.Other,
                               person.Value?.IsEmailVerified ?? false,
                               person.Value?.Status ?? false,
                               user.Email,
                               person.Value?.RegistrationNumber,
                               null
                           )
                       ));
            }
            catch (FirebaseAdmin.Auth.FirebaseAuthException)
            {
                return(new KeyValuePair <string, User>(string.Empty, null));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 4
0
        private async Task <KeyValuePair <string, User> > GetByEmailAsync(string email)
        {
            // Get User
            try
            {
                var provider = await FirebaseAuthAdmin.GetUserByEmailAsync(email);

                var person = await GetPersonByUserIdAsync(provider.Uid);

                return(new KeyValuePair <string, User>
                       (
                           person.Key,
                           new User
                           (
                               provider.Uid,
                               person.Value?.FullName,
                               person.Value?.PhoneCountryCode,
                               person.Value?.PhoneNumber ?? 0,
                               person.Value?.CreatedAt ?? DateTime.UtcNow.AddHours(1),
                               person.Value?.Role ?? RoleOptions.Other,
                               person.Value?.IsEmailVerified ?? false,
                               person.Value?.Status ?? false,
                               provider.Email,
                               person.Value?.RegistrationNumber,
                               null
                           )
                       ));
            }
            catch (FirebaseAdmin.Auth.FirebaseAuthException)
            {
                return(new KeyValuePair <string, User>(string.Empty, null));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 5
0
        public async Task EditAsync(string id, RegisterCommand command)
        {
            try
            {
                var user1 = await GetAsync(id);

                if (user1.Value == null)
                {
                    throw new KeyNotFoundException($"{nameof(User)} {command.Email} not found");
                }

                var user2 = await GetByEmailAsync(command.Email);

                if (user2.Value != null && user2.Value.UserId != id)
                {
                    throw new DuplicateWaitObjectException($"{nameof(command.Email)} {command.Email} already exists !");
                }

                // Create User
                var userRecord = await FirebaseAuthAdmin.UpdateUserAsync
                                 (
                    new FirebaseAdmin.Auth.UserRecordArgs
                {
                    Uid         = id,
                    Email       = command.Email,
                    Password    = command.Password,
                    DisplayName = command.Name
                }
                                 );

                var person = await GetPersonByUserIdAsync(id);

                if (person.Value == null)
                {
                    var newPerson = new Person
                                    (
                        userRecord.Uid,
                        command.Name,
                        command.RegistrationNumber,
                        command.PhoneCountryCode,
                        command.PhoneNumber,
                        command.CreatedAt,
                        command.Role,
                        userRecord.EmailVerified,
                        command.Status
                                    );

                    var result = await FirebaseClient
                                 .Child(Table)
                                 .PostAsync(JsonConvert.SerializeObject(person));

                    person = new KeyValuePair <string, Person>(result.Key, newPerson);
                }
                else
                {
                    var newPerson = new Person
                                    (
                        userRecord.Uid,
                        command.RegistrationNumber,
                        command.Name,
                        command.PhoneCountryCode,
                        command.PhoneNumber,
                        person.Value.CreatedAt,
                        command.Role,
                        userRecord.EmailVerified,
                        command.Status
                                    );

                    await FirebaseClient
                    .Child(Table)
                    .Child(person.Key)
                    .PutAsync(JsonConvert.SerializeObject(newPerson));
                }
            }
            catch (Firebase.Database.FirebaseException ex)
            {
                if (ex.InnerException?.InnerException?.GetType() == typeof(SocketException))
                {
                    throw new HttpRequestException("Cannot join the server. Please check your internet connexion.");
                }
                throw ex;
            }
            catch (KeyNotFoundException ex)
            {
                throw ex;
            }
            catch (DuplicateWaitObjectException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }