Compare commits

..
Author SHA1 Message Date
Abdullah Atta 768384011d api: re-enable password change 2026-08-10 12:08:35 +05:00
Abdullah Atta a8e73069c2 common: avoid long lived mail client 2026-08-05 22:16:56 +05:00
2 changed files with 50 additions and 54 deletions
+28 -29
View File
@@ -93,39 +93,38 @@ namespace Notesnook.API.Controllers
[HttpPatch("password/{type}")] [HttpPatch("password/{type}")]
public async Task<IActionResult> ChangePassword([FromRoute] string type, [FromBody] ChangePasswordForm form) public async Task<IActionResult> ChangePassword([FromRoute] string type, [FromBody] ChangePasswordForm form)
{ {
return BadRequest(new { error = "Password change is currently disabled." }); var userId = User.GetUserId();
// var userId = User.GetUserId(); var clientId = User.FindFirstValue("client_id");
// var clientId = User.FindFirstValue("client_id"); var jti = User.FindFirstValue("jti");
// var jti = User.FindFirstValue("jti"); var isPasswordReset = type == "reset";
// var isPasswordReset = type == "reset"; try
// try {
// { var result = isPasswordReset ? await serviceAccessor.UserAccountService.ResetPasswordAsync(userId, form.NewPassword) : await serviceAccessor.UserAccountService.ChangePasswordAsync(userId, form.OldPassword, form.NewPassword);
// var result = isPasswordReset ? await serviceAccessor.UserAccountService.ResetPasswordAsync(userId, form.NewPassword) : await serviceAccessor.UserAccountService.ChangePasswordAsync(userId, form.OldPassword, form.NewPassword); if (!result)
// if (!result) return BadRequest("Failed to change password.");
// return BadRequest("Failed to change password.");
// await UserService.SetUserKeysAsync(userId, form.UserKeys); await UserService.SetUserKeysAsync(userId, form.UserKeys);
// await serviceAccessor.UserAccountService.ClearSessionsAsync(userId, clientId, all: false, jti, null); await serviceAccessor.UserAccountService.ClearSessionsAsync(userId, clientId, all: false, jti, null);
// await WampServers.MessengerServer.PublishMessageAsync(MessengerServerTopics.SendSSETopic, new SendSSEMessage await WampServers.MessengerServer.PublishMessageAsync(MessengerServerTopics.SendSSETopic, new SendSSEMessage
// { {
// UserId = userId, UserId = userId,
// OriginTokenId = jti, OriginTokenId = jti,
// Message = new Message Message = new Message
// { {
// Type = "logout", Type = "logout",
// Data = JsonSerializer.Serialize(new { reason = "Password changed." }) Data = JsonSerializer.Serialize(new { reason = "Password changed." })
// } }
// }); });
// return Ok(); return Ok();
// } }
// catch (Exception ex) catch (Exception ex)
// { {
// logger.LogError(ex, "Failed to change password"); logger.LogError(ex, "Failed to change password");
// return BadRequest(new { error = ex.Message }); return BadRequest(new { error = ex.Message });
// } }
} }
[HttpPost("reset")] [HttpPost("reset")]
+22 -25
View File
@@ -14,9 +14,8 @@ using Streetwriters.Common.Models;
namespace Streetwriters.Common.Services namespace Streetwriters.Common.Services
{ {
public class EmailSender : IEmailSender, IAsyncDisposable public class EmailSender : IEmailSender
{ {
private readonly SmtpClient mailClient = new();
private readonly ILogger<EmailSender> logger; private readonly ILogger<EmailSender> logger;
public EmailSender(ILogger<EmailSender> logger) public EmailSender(ILogger<EmailSender> logger)
@@ -32,27 +31,25 @@ namespace Streetwriters.Common.Services
Dictionary<string, byte[]>? attachments = null Dictionary<string, byte[]>? attachments = null
) )
{ {
if (!mailClient.IsConnected) using var mailClient = new SmtpClient();
if (int.TryParse(Common.Constants.SMTP_PORT, out int port))
{ {
if (int.TryParse(Common.Constants.SMTP_PORT, out int port)) await mailClient.ConnectAsync(
{ Common.Constants.SMTP_HOST,
await mailClient.ConnectAsync( port,
Common.Constants.SMTP_HOST, MailKit.Security.SecureSocketOptions.Auto
port, );
MailKit.Security.SecureSocketOptions.Auto }
); else
} {
else throw new InvalidDataException("SMTP_PORT is not a valid integer value.");
{
throw new InvalidDataException("SMTP_PORT is not a valid integer value.");
}
} }
if (!mailClient.IsAuthenticated) await mailClient.AuthenticateAsync(
await mailClient.AuthenticateAsync( Common.Constants.SMTP_USERNAME,
Common.Constants.SMTP_USERNAME, Common.Constants.SMTP_PASSWORD
Common.Constants.SMTP_PASSWORD );
);
var message = new MimeMessage(); var message = new MimeMessage();
message.From.Add(new MailboxAddress(from.DisplayName, from.Address)); message.From.Add(new MailboxAddress(from.DisplayName, from.Address));
@@ -70,6 +67,11 @@ namespace Streetwriters.Common.Services
); );
await mailClient.SendAsync(message); await mailClient.SendAsync(message);
if (mailClient.IsConnected)
{
await mailClient.DisconnectAsync(true);
}
} }
private async Task<MimeEntity> GetEmailBodyAsync( private async Task<MimeEntity> GetEmailBodyAsync(
@@ -129,10 +131,5 @@ namespace Streetwriters.Common.Services
} }
} }
async ValueTask IAsyncDisposable.DisposeAsync()
{
await mailClient.DisconnectAsync(true);
mailClient.Dispose();
}
} }
} }