Merge pull request #18 from Qtoss-AI/hdongDev

hdong: Add send verify code API with none login and login when reset …
This commit is contained in:
Haiping 2024-10-07 17:15:00 -05:00 committed by GitHub
commit f760f07edd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 54 additions and 21 deletions

View file

@ -13,7 +13,8 @@ public interface IUserService
Task<User> GetMyProfile();
Task<bool> VerifyUserNameExisting(string userName);
Task<bool> VerifyEmailExisting(string email);
Task<bool> SendVerificationCodeResetPassword(User user);
Task<bool> SendVerificationCodeResetPasswordNoLogin(User user);
Task<bool> SendVerificationCodeResetPasswordLogin();
Task<bool> ResetUserPassword(User user);
Task<bool> ModifyUserEmail(string email);
Task<bool> ModifyUserPhone(string phone);

View file

@ -412,7 +412,48 @@ public class UserService : IUserService
return false;
}
public async Task<bool> SendVerificationCodeResetPassword(User user)
public async Task<bool> SendVerificationCodeResetPasswordNoLogin(User user)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
User? record = null;
if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone))
{
return false;
}
if (!string.IsNullOrEmpty(user.Phone))
{
record = db.GetUserByPhone(user.Phone);
}
if (!string.IsNullOrEmpty(user.Email))
{
record = db.GetUserByEmail(user.Email);
}
if (record == null)
{
return false;
}
record.VerificationCode = Nanoid.Generate(alphabet: "0123456789", size: 6);
//update current verification code.
db.UpdateUserVerificationCode(record.Id, record.VerificationCode);
//send code to user Email.
var hooks = _services.GetServices<IAuthenticationHook>();
foreach (var hook in hooks)
{
hook.VerificationCodeResetPassword(record);
}
return true;
}
public async Task<bool> SendVerificationCodeResetPasswordLogin()
{
var db = _services.GetRequiredService<IBotSharpRepository>();
@ -422,23 +463,6 @@ public class UserService : IUserService
{
record = db.GetUserById(_user.Id);
}
else
{
if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone))
{
return false;
}
if (!string.IsNullOrEmpty(user.Email))
{
record = db.GetUserByEmail(user.Email);
}
if (!string.IsNullOrEmpty(user.Phone))
{
record = db.GetUserByPhone(user.Phone);
}
}
if (record == null)
{

View file

@ -108,12 +108,20 @@ public class UserController : ControllerBase
{
return await _userService.VerifyEmailExisting(email);
}
[AllowAnonymous]
[HttpPost("/user/verifycode")]
[HttpPost("/user/verifycode-out")]
public async Task<bool> SendVerificationCodeResetPassword([FromBody] UserCreationModel user)
{
return await _userService.SendVerificationCodeResetPassword(user.ToUser());
return await _userService.SendVerificationCodeResetPasswordNoLogin(user.ToUser());
}
[HttpPost("/user/verifycode-in")]
public async Task<bool> SendVerificationCodeResetPasswordLogined()
{
return await _userService.SendVerificationCodeResetPasswordLogin();
}
[AllowAnonymous]
[HttpPost("/user/resetpassword")]
public async Task<bool> ResetUserPassword([FromBody] UserResetPasswordModel user)