Пример #1
0
        public async Task <IActionResult> Register(UserRegisterInput input)
        {
            var existingUser = await Mongo.GetUserByEmail(input.Email);

            if (existingUser != null)
            {
                return(this.ProblemParameter("Supplied email address is already registered"));
            }

            if (!CheckPassword(input.Password))
            {
                return(this.ProblemParameter("Password is not secure"));
            }

            var verificationToken = new Random().GenerateReadableCode(8);

            Logger.LogDebug("Registering new user for {0} with verification token {1}", input.Email, verificationToken);

            try {
                var user = new User {
                    Email             = input.Email,
                    PasswordHash      = BCrypt.Net.BCrypt.HashPassword(input.Password),
                    Name              = input.Name,
                    Surname           = input.Surname,
                    VerificationToken = verificationToken,
                    RegisteredOn      = DateTime.UtcNow
                };
                await Mongo.CreateUser(user);

                _composer.SendVerificationMail(user);

                return(CreatedAtAction(
                           nameof(GetInformation),
                           new {
                    id = user.Id.ToString()
                },
                           new UserOutput {
                    Id = user.Id.ToString(),
                    Email = user.Email,
                    Name = user.Name,
                    Surname = user.Surname
                }
                           ));
            }
            catch (Exception ex) {
                Logger.LogError(ex, "Failed to register new user with email {0}", input.Email);
                throw;
            }
        }