public async Task <IActionResult> ResetPasswordAsync([FromBody] PasswordRecoveryInfo model, CancellationToken cancellationToken = default) { try { var result = await _userService.ResetPasswordAsync(model, cancellationToken); return(result.IsError ? throw new InvalidOperationException(result.Message) : result.IsSuccess ? StatusCode(StatusCodes.Status206PartialContent, result.Message.CollectProblemDetailsPartialContent(HttpContext)) : StatusCode(StatusCodes.Status206PartialContent, result.Message.CollectProblemDetailsPartialContent(HttpContext))); } catch (InvalidOperationException ex) { Log.Error(ex, ex.Message); return(StatusCode(StatusCodes.Status500InternalServerError, new CustumResult() { Status = StatusCodes.Status500InternalServerError, Message = ex.Message })); } }
public async Task <Result> ResetPasswordAsync(PasswordRecoveryInfo userInfo, CancellationToken cancellationToken = default) { var user = await _userManager.FindByIdAsync(userInfo.UserId); if (user == null) { return(Result.Fail(ExceptionConstants.USER_WAS_NOT_FOUND)); } var decodedTokenBytes = Convert.FromBase64String(userInfo.Token); string decodedTokenString = Encoding.UTF8.GetString(decodedTokenBytes); var result = await _userManager.ResetPasswordAsync(user, decodedTokenString, userInfo.Password); if (result.Succeeded) { return(await _emailBuilder.SendEmailWithLinkAsync(user.Email, EmailConstants.PASSWORD_RESET_SUBJECT, EmailConstants.PASSWORD_RESET_LINK, EmailConstants.PASSWORD_RESET_MESSAGE, userInfo.CallBackUrl, cancellationToken)); } else { return(Result.Fail(result.Errors.Select(x => x.Description).Join("\n"))); } }