Пример #1
0
        public async Task <IActionResult> ForgotPassword([FromBody] ForgotPasswordBindingModel bm)
        {
            if (bm == null)
            {
                return(BadRequest("The payload must not be null."));
            }

            if (string.IsNullOrWhiteSpace(bm.Email))
            {
                return(BadRequest("An email address is required."));
            }

            var user = await _userService.GetUserByEmail(bm.Email);

            if (user == null)
            {
                return(NotFound("A user with that email address doesn't exist."));
            }

            var token = Helpers.GenerateToken("email", bm.Email, 12);

            var email = EmailTemplates.GetForgotPasswordEmail(
                $"{Config.FrontEndUrl}/auth/reset-password?token={token}");
            await _emailService.SendAsync(bm.Email, "Forgot Password", email);

            _logger.LogInformation("Forgot password email sent successfully.");

            return(Ok("Your password reset email has been sent."));
        }
Пример #2
0
        public async Task <IActionResult> ForgotPassword([FromBody] ForgotPasswordBindingModel bm)
        {
            if (bm == null)
            {
                return(BadRequest("The payload must not be null."));
            }

            if (string.IsNullOrWhiteSpace(bm.Email))
            {
                return(BadRequest("An email address is required."));
            }

            var user = await _userService.GetUserByEmail(bm.Email);

            if (user == null)
            {
                return(NotFound("A user with that email address doesn't exist."));
            }

            var token = Helpers.GetToken(user, 12, TokenType.Reset);

            var email = EmailTemplates.GetForgotPasswordEmail(
                $"{Config.FrontEndUrl}/auth/reset-password?token={token}");
            var response = await _emailService.SendAsync(bm.Email, "Forgot Password", email);

            if (response.IsSuccessful)
            {
                _logger.LogInformation("Forgot password email sent successfully.");
                return(Ok(new GenericViewModel
                {
                    Message = "Your password reset email has been sent."
                }));
            }

            _logger.LogError("The email was not sent successfully.");
            _logger.LogError(response.ErrorException, response.ErrorMessage);
            return(StatusCode((int)HttpStatusCode.InternalServerError, new GenericViewModel
            {
                Message = "An error occurred when sending the recovery email."
            }));
        }