Пример #1
0
        public async Task <IActionResult> Register([FromBody] RegisterModel body)
        {
            if (_emailService.GetCaptchaNotPassed(body.Captcha).Result)
            {
                return(ErrorResponse($"Prvok Re-Captcha je zlý skúste znova."));
            }

            body.Email = body.Email.ToLower();
            User user      = new User(body.Email, body.Name, body.Surname);
            var  addResult = await _userService.AddUserAsync(user, body.Password);

            if (!addResult.Succeeded)
            {
                var existingUser = await _userService.GetUserByEmailAsync(body.Email);

                // user hasn't confirmed email
                if (existingUser != null && !existingUser.EmailConfirmed)
                {
                    Console.WriteLine("existingUser: "******"confirmEmail/{existingUser.Id}/{newToken}").ToString();

                    if (!_emailService.SendConfirmationEmail(body.Email, newCallbackUrl, "RegistrationEmail"))
                    {
                        _logger.LogError($"Error when sending confirmation email to user {body.Email}. Errors: {addResult.Errors} URI: {newCallbackUrl}");
                        return(BadRequest());
                    }
                    _logger.LogInformation($"Confirmation email to user {user.Email} sent.");
                    return(Ok());
                }
                else
                {
                    _logger.LogInformation(ControllerExtensions.IdentityErrorBuilder($"Error when creating user {body.Email}. Identity errors: ", addResult.Errors));
                    Dictionary <string, string[]> identityErrors = ControllerExtensions.IdentityErrorsToDictionary(addResult.Errors);
                    return(ValidationError(identityErrors));
                }
            }

            _logger.LogInformation($"User {body.Email} created.");
            string token = await _userService.GenerateEmailConfirmationTokenAsync(user);

            user = await _userService.GetUserByEmailAsync(body.Email);

            string callbackUrl = new Uri(_baseUrl, $@"confirmEmail/{user.Id}/{token}").ToString();

            if (!_emailService.SendConfirmationEmail(body.Email, callbackUrl, "RegistrationEmail"))
            {
                _logger.LogError($"Error when sending confirmation email to user {body.Email}.");
                var deleteResult = await _userService.DeleteUserAsyc(user);

                if (deleteResult.Succeeded)
                {
                    _logger.LogInformation($"User {body.Email} deleted.");
                }
                return(BadRequest());
            }
            _logger.LogInformation($"Confirmation email to user {user.Email} sent.");
            return(Ok());
        }
Пример #2
0
        public async Task <IActionResult> ConfirmEmail([FromBody] ConfirmEmailModel body)
        {
            var user = await _userService.GetUserByIdAsync(body.UserId);

            if (user == null)
            {
                _logger.LogWarning($"Invalid email confirmation attempt. User with id {body.UserId} doesn't exist.");
                return(BadRequest());
            }

            if (user.EmailConfirmed)
            {
                return(Ok());
            }

            var result = await _userService.ConfirmEmailAsync(user, body.Token);

            if (result.Succeeded)
            {
                _logger.LogInformation($"User {user.Email} confirmed email address.");
                return(Ok());
            }
            _logger.LogInformation(ControllerExtensions.IdentityErrorBuilder($"Confirmation of email address {body.UserId} failed. Errors: ", result.Errors));
            return(BadRequest());
        }
Пример #3
0
        public async Task <IActionResult> SetNewPassword([FromBody] SetNewPasswordModel body)
        {
            var user = await _userService.GetUserByIdAsync(body.UserId);

            if (user == null)
            {
                _logger.LogError($"Invalid password reset attemp. User with id {body.UserId} doesn't exist.");
                return(BadRequest());
            }
            var result = await _userService.ResetPasswordAsync(user, body.Token, body.Password);

            if (!result.Succeeded)
            {
                _logger.LogInformation(ControllerExtensions.IdentityErrorBuilder($"Error when resetting password for user {user.Email}. Identity errors: ", result.Errors));
                Dictionary <string, string[]> identityErrors = ControllerExtensions.IdentityErrorsToDictionary(result.Errors);
                return(ValidationError(identityErrors));
            }
            return(Ok());
        }