Add update password API

This commit is contained in:
AnonymousDotNet 2024-09-17 18:31:38 +08:00
parent 8cb97af629
commit b89d88ffb2
3 changed files with 33 additions and 0 deletions

View file

@ -16,4 +16,5 @@ public interface IUserService
Task<bool> ResetUserPassword(User user);
Task<bool> ModifyUserEmail(string email);
Task<bool> ModifyUserPhone(string phone);
Task<bool> UpdatePassword(string newPassword, string verificationCode);
}

View file

@ -83,6 +83,28 @@ public class UserService : IUserService
return record;
}
public async Task<bool> UpdatePassword(string password, string verificationCode)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var record = db.GetUserByUserName(_user.UserName);
if (record == null)
{
return false;
}
if (record.VerificationCode != verificationCode)
{
return false;
}
var salt = Guid.NewGuid().ToString("N");
record.Password = Utilities.HashTextMd5($"{password}{salt}");
db.UpdateUserPassword(record.Id, password);
return true;
}
public async Task<Token?> GetToken(string authorization)
{
var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization));

View file

@ -121,6 +121,16 @@ public class UserController : ControllerBase
return await _userService.ResetUserPassword(user.ToUser());
}
[HttpPost("/user/updatepassword")]
public async Task<bool> UpdatePassword([FromBody] User user)
{
if (string.IsNullOrWhiteSpace(user.Password) || string.IsNullOrWhiteSpace(user.VerificationCode))
{
return false;
}
return await _userService.UpdatePassword(user.Password, user.VerificationCode);
}
[HttpPost("/user/email/modify")]
public async Task<bool> ModifyUserEmail([FromQuery] string email)
{