public async Task <Result <bool> > RegisterAsync(InputRegisterModel model)
        {
            if (model is null)
            {
                return(EMPTY_FORM);
            }

            var user = new ApplicationUser {
                Email = model.Email, UserName = model.Username
            };
            var result = await this.userManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return(Result <bool> .Failure(result.Errors.Select(e => e.Description)));
            }

            var messageData = new UserRegisteredMessage
            {
                Email  = user.Email,
                Name   = user.UserName,
                UserId = user.Id
            };

            var message = new Message(messageData);

            await this.messageService.SaveAsync(message);

            await this.publisher.Publish(messageData);

            await this.messageService.MarkMessageAsPublishedAsync(message.Id);

            return(Result <bool> .SuccessWith(true));
        }
Exemplo n.º 2
0
        public async Task <RegisterViewModel> RegisterAsync(RegisterBindingModel model)
        {
            var salt           = this.GenerateSalt();
            var hashedPassword = this.HashPassword(model.Password, salt);

            var user = new Models.User
            {
                Username = model.Username,
                Password = hashedPassword,
                Salt     = salt
            };

            await this.userDatabase.Users.AddAsync(user);

            await this.userDatabase.SaveChangesAsync();

            var queueName   = "users";
            var messageData = new UserRegisteredMessage
            {
                UserId   = user.Id,
                Username = user.Username
            };

            var message = new Message.DataAccess.Models.Message(queueName, messageData);

            await this.messageDatabase.Messages.AddAsync(message);

            await this.messageDatabase.SaveChangesAsync();

            this.queueSender.PostQueueMessage(queueName, messageData);

            message.MarkAsPublished();
            await this.messageDatabase.SaveChangesAsync();

            var token = this.GenerateJwtToken(user.Id.ToString(), user.Username, false);

            return(new RegisterViewModel
            {
                Username = user.Username,
                Token = token,
                IsAdmin = false,
                ExpirationDate = DateTime.UtcNow.AddDays(Constants.Common.TokenExpiration),
                Message = string.Format(Constants.Common.Success, nameof(User), user.Username, "Registered")
            });
        }