From 0468147bfa4a7b3a004154460c20ad4f66ee5530 Mon Sep 17 00:00:00 2001 From: "nghiann@zyyx.jp" Date: Mon, 17 Jul 2023 22:17:59 +0700 Subject: [PATCH 1/4] remove file ignore --- .gitignore | 1 + .idea/.idea.DATN.Web/.idea/workspace.xml | 394 ------------------ .idea/.idea.Dictionary.App/.idea/.gitignore | 13 - .idea/.idea.Dictionary.App/.idea/.name | 1 - .../.idea.Dictionary.App/.idea/encodings.xml | 4 - .../.idea/git_toolbox_prj.xml | 15 - .../.idea/indexLayout.xml | 8 - .../.idea/sqldialects.xml | 6 - .idea/.idea.Dictionary.App/.idea/vcs.xml | 6 - .../.idea/dataSources.local.xml | 19 - .../.idea/dataSources.xml | 12 - .../schema/information_schema.FNRwLQ.meta | 2 - .../storage_v2/_src_/schema/mysql.osA4Bg.meta | 2 - .../schema/performance_schema.kIw0nw.meta | 2 - .../.idea/sqldialects.xml | 6 - .../.idea/workspace.xml | 138 ------ 16 files changed, 1 insertion(+), 628 deletions(-) delete mode 100644 .idea/.idea.DATN.Web/.idea/workspace.xml delete mode 100644 .idea/.idea.Dictionary.App/.idea/.gitignore delete mode 100644 .idea/.idea.Dictionary.App/.idea/.name delete mode 100644 .idea/.idea.Dictionary.App/.idea/encodings.xml delete mode 100644 .idea/.idea.Dictionary.App/.idea/git_toolbox_prj.xml delete mode 100644 .idea/.idea.Dictionary.App/.idea/indexLayout.xml delete mode 100644 .idea/.idea.Dictionary.App/.idea/sqldialects.xml delete mode 100644 .idea/.idea.Dictionary.App/.idea/vcs.xml delete mode 100644 .idea/.idea.dictionary-service/.idea/dataSources.local.xml delete mode 100644 .idea/.idea.dictionary-service/.idea/dataSources.xml delete mode 100644 .idea/.idea.dictionary-service/.idea/dataSources/c52ef015-b6fc-4e6b-b8bc-3f8daa19a952/storage_v2/_src_/schema/information_schema.FNRwLQ.meta delete mode 100644 .idea/.idea.dictionary-service/.idea/dataSources/c52ef015-b6fc-4e6b-b8bc-3f8daa19a952/storage_v2/_src_/schema/mysql.osA4Bg.meta delete mode 100644 .idea/.idea.dictionary-service/.idea/dataSources/c52ef015-b6fc-4e6b-b8bc-3f8daa19a952/storage_v2/_src_/schema/performance_schema.kIw0nw.meta delete mode 100644 .idea/.idea.dictionary-service/.idea/sqldialects.xml delete mode 100644 .idea/.idea.dictionary-service/.idea/workspace.xml diff --git a/.gitignore b/.gitignore index 2d53cb3..ed31b08 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /Dictionary.Repo/obj/ /Dictionary.Service/bin/ /Dictionary.Service/obj/ +.idea \ No newline at end of file diff --git a/.idea/.idea.DATN.Web/.idea/workspace.xml b/.idea/.idea.DATN.Web/.idea/workspace.xml deleted file mode 100644 index 1950520..0000000 --- a/.idea/.idea.DATN.Web/.idea/workspace.xml +++ /dev/null @@ -1,394 +0,0 @@ - - - - DATN.Web.Api/DATN.Web.Api.csproj - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1684588731098 - - - - - - - - \ No newline at end of file diff --git a/.idea/.idea.Dictionary.App/.idea/.gitignore b/.idea/.idea.Dictionary.App/.idea/.gitignore deleted file mode 100644 index c24e2d0..0000000 --- a/.idea/.idea.Dictionary.App/.idea/.gitignore +++ /dev/null @@ -1,13 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Rider ignored files -/contentModel.xml -/projectSettingsUpdater.xml -/.idea.DATN.Web.iml -/modules.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/.idea.Dictionary.App/.idea/.name b/.idea/.idea.Dictionary.App/.idea/.name deleted file mode 100644 index dcc7ff4..0000000 --- a/.idea/.idea.Dictionary.App/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -Dictionary.App \ No newline at end of file diff --git a/.idea/.idea.Dictionary.App/.idea/encodings.xml b/.idea/.idea.Dictionary.App/.idea/encodings.xml deleted file mode 100644 index df87cf9..0000000 --- a/.idea/.idea.Dictionary.App/.idea/encodings.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/.idea.Dictionary.App/.idea/git_toolbox_prj.xml b/.idea/.idea.Dictionary.App/.idea/git_toolbox_prj.xml deleted file mode 100644 index 02b915b..0000000 --- a/.idea/.idea.Dictionary.App/.idea/git_toolbox_prj.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/.idea/.idea.Dictionary.App/.idea/indexLayout.xml b/.idea/.idea.Dictionary.App/.idea/indexLayout.xml deleted file mode 100644 index 7b08163..0000000 --- a/.idea/.idea.Dictionary.App/.idea/indexLayout.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/.idea.Dictionary.App/.idea/sqldialects.xml b/.idea/.idea.Dictionary.App/.idea/sqldialects.xml deleted file mode 100644 index 852e673..0000000 --- a/.idea/.idea.Dictionary.App/.idea/sqldialects.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/.idea.Dictionary.App/.idea/vcs.xml b/.idea/.idea.Dictionary.App/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/.idea.Dictionary.App/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/.idea.dictionary-service/.idea/dataSources.local.xml b/.idea/.idea.dictionary-service/.idea/dataSources.local.xml deleted file mode 100644 index afed74e..0000000 --- a/.idea/.idea.dictionary-service/.idea/dataSources.local.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - #@ - ` - - - master_key - root - - - - - - - - \ No newline at end of file diff --git a/.idea/.idea.dictionary-service/.idea/dataSources.xml b/.idea/.idea.dictionary-service/.idea/dataSources.xml deleted file mode 100644 index 3542ba5..0000000 --- a/.idea/.idea.dictionary-service/.idea/dataSources.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - mariadb - true - org.mariadb.jdbc.Driver - jdbc:mariadb://localhost:3306/dictionary - $ProjectFileDir$ - - - \ No newline at end of file diff --git a/.idea/.idea.dictionary-service/.idea/dataSources/c52ef015-b6fc-4e6b-b8bc-3f8daa19a952/storage_v2/_src_/schema/information_schema.FNRwLQ.meta b/.idea/.idea.dictionary-service/.idea/dataSources/c52ef015-b6fc-4e6b-b8bc-3f8daa19a952/storage_v2/_src_/schema/information_schema.FNRwLQ.meta deleted file mode 100644 index 1ff3db2..0000000 --- a/.idea/.idea.dictionary-service/.idea/dataSources/c52ef015-b6fc-4e6b-b8bc-3f8daa19a952/storage_v2/_src_/schema/information_schema.FNRwLQ.meta +++ /dev/null @@ -1,2 +0,0 @@ -#n:information_schema -! [null, 0, null, null, -2147483648, -2147483648] diff --git a/.idea/.idea.dictionary-service/.idea/dataSources/c52ef015-b6fc-4e6b-b8bc-3f8daa19a952/storage_v2/_src_/schema/mysql.osA4Bg.meta b/.idea/.idea.dictionary-service/.idea/dataSources/c52ef015-b6fc-4e6b-b8bc-3f8daa19a952/storage_v2/_src_/schema/mysql.osA4Bg.meta deleted file mode 100644 index 86a53f1..0000000 --- a/.idea/.idea.dictionary-service/.idea/dataSources/c52ef015-b6fc-4e6b-b8bc-3f8daa19a952/storage_v2/_src_/schema/mysql.osA4Bg.meta +++ /dev/null @@ -1,2 +0,0 @@ -#n:mysql -! [null, 0, null, null, -2147483648, -2147483648] diff --git a/.idea/.idea.dictionary-service/.idea/dataSources/c52ef015-b6fc-4e6b-b8bc-3f8daa19a952/storage_v2/_src_/schema/performance_schema.kIw0nw.meta b/.idea/.idea.dictionary-service/.idea/dataSources/c52ef015-b6fc-4e6b-b8bc-3f8daa19a952/storage_v2/_src_/schema/performance_schema.kIw0nw.meta deleted file mode 100644 index 9394db1..0000000 --- a/.idea/.idea.dictionary-service/.idea/dataSources/c52ef015-b6fc-4e6b-b8bc-3f8daa19a952/storage_v2/_src_/schema/performance_schema.kIw0nw.meta +++ /dev/null @@ -1,2 +0,0 @@ -#n:performance_schema -! [null, 0, null, null, -2147483648, -2147483648] diff --git a/.idea/.idea.dictionary-service/.idea/sqldialects.xml b/.idea/.idea.dictionary-service/.idea/sqldialects.xml deleted file mode 100644 index 852e673..0000000 --- a/.idea/.idea.dictionary-service/.idea/sqldialects.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/.idea.dictionary-service/.idea/workspace.xml b/.idea/.idea.dictionary-service/.idea/workspace.xml deleted file mode 100644 index 8fc7893..0000000 --- a/.idea/.idea.dictionary-service/.idea/workspace.xml +++ /dev/null @@ -1,138 +0,0 @@ - - - - DATN.Web.Api/DATN.Web.Api.csproj - Dictionary.Api/Dictionary.Api.csproj - - - - - - - - - - - - - - - - - - - - - - FILE - - - - - - - - - - - - - - - - - - - - 1684588731098 - - - - - - - - - - - - - file://$PROJECT_DIR$/Dictionary.Service/Service/UserService.cs - 36 - - - - - - - - - - - \ No newline at end of file From 50b4a63c414c7519b03adb971111ef01a03fe006 Mon Sep 17 00:00:00 2001 From: "nghiann@zyyx.jp" Date: Thu, 20 Jul 2023 23:29:31 +0700 Subject: [PATCH 2/4] feat: API refactor --- Dictionary.Api/Controllers/UserController.cs | 107 +++++++++--------- Dictionary.Repo/Repo/UserRepo.cs | 17 +-- .../DtoEdit/Authentication/ForgotModel.cs | 6 + .../DtoEdit/Authentication/ResetPassword.cs | 2 +- .../DtoEdit/Authentication/SignupModel.cs | 71 +----------- .../DtoEdit/Authentication/UpdateUser.cs | 36 ++---- .../Interfaces/Service/IUserService.cs | 6 +- .../AuthenContextHandleMiddleware.cs | 3 + Dictionary.Service/Service/UserService.cs | 104 +++++++++++------ 9 files changed, 159 insertions(+), 193 deletions(-) create mode 100644 Dictionary.Service/DtoEdit/Authentication/ForgotModel.cs diff --git a/Dictionary.Api/Controllers/UserController.cs b/Dictionary.Api/Controllers/UserController.cs index 0b84be2..d62f29e 100644 --- a/Dictionary.Api/Controllers/UserController.cs +++ b/Dictionary.Api/Controllers/UserController.cs @@ -86,7 +86,7 @@ public async Task Login([FromBody] LoginModel model) var res = await _userService.Login(model); if (res != null) { - var actionResult = new ServiceResult((int)ApiStatus.Success, Resources.getDataSuccess, "", res, null); + var actionResult = new ServiceResult((int)ApiStatus.Success, Resources.getDataSuccess, "", res, ""); return Ok(actionResult); } else @@ -98,9 +98,25 @@ public async Task Login([FromBody] LoginModel model) } catch (ValidateException exception) { - var actionResult = new ServiceResult((int)ApiStatus.Exception, exception.Message, "", 0, "400"); + var actionResult = new ServiceResult((int)ApiStatus.Exception, exception.Message, "", null, exception.resultCode.ToString()); + return Ok(actionResult); + } + catch (Exception exception) + { + var actionResult = new ServiceResult((int)ApiStatus.Exception, Resources.error, exception.Message, null, "500"); + Console.WriteLine(exception.StackTrace); return Ok(actionResult); } + } + + [HttpPost("account/forgot_password")] + public async Task ForgotPassword([FromBody] ForgotModel forgotModel) + { + try + { + var res = await _userService.ForgotPassword(forgotModel); + return Ok(res); + } catch (Exception exception) { var actionResult = new ServiceResult((int)ApiStatus.Exception, Resources.error, exception.Message, null, "500"); @@ -109,6 +125,7 @@ public async Task Login([FromBody] LoginModel model) } } + [HttpGet("token")] public async Task GetToken() { @@ -145,79 +162,41 @@ public async Task Signup([FromBody] SignupModel model) } catch (Exception exception) { - var actionResult = new ServiceResult((int)ApiStatus.Exception, Resources.error, exception.Message, null, "500"); + var actionResult = new ServiceResult(-1, Resources.error, exception.Message, null, "500"); return Ok(actionResult); } } - - [HttpPut("resetPassword")] - public async Task ResetPassword([FromBody] ResetPassword resetPassword) - { - try - { - var contextService = _contextService.Get(); - resetPassword.user_id = contextService.UserId; - await _userService.ResetPassword(resetPassword); - var actionResult = new ServiceResult((int)ApiStatus.Success, "Cập nhật mật khẩu thành công!", "", null, null); + [HttpPut("user/update_password")] - return Ok(actionResult); - } - catch (ValidateException exception) - { - var actionResult = new ServiceResult((int)ApiStatus.Exception, exception.Message, "", 0, "400"); - return Ok(actionResult); - } - catch (Exception exception) - { - var actionResult = new ServiceResult((int)ApiStatus.Exception, Resources.error, exception.Message, null, "500"); - return Ok(actionResult); - } - } - - [HttpPut("update")] - public async Task updateUser([FromBody] UpdateUser userUpdate) + public async Task UpdatePassword([FromBody] ResetPassword resetPassword) { try { - var contextService = _contextService.Get(); - userUpdate.user_id = contextService.UserId; - var res = await _userService.UpdateUser(userUpdate); - if (res != null) - { - var actionResult = new ServiceResult((int)ApiStatus.Success, "Cập nhật thông tin tài khoản thành công!", "", null, null); - - return Ok(actionResult); - } - else - { - var actionResult = new ServiceResult((int)ApiStatus.Fail, "Cập nhật thất bại!", "", - null, "204"); - return Ok(actionResult); - } + var res = await _userService.ResetPassword(resetPassword); + return Ok(res); } catch (Exception exception) { - var actionResult = new ServiceResult((int)ApiStatus.Exception, Resources.error, exception.Message, null, "500"); + var actionResult = new ServiceResult(-1, Resources.error, exception.Message, null, "500"); return Ok(actionResult); } } - [HttpPut("updateStatus")] - public async Task UpdateStatus([FromBody] UpdateUser userUpdate) + [HttpPut("/user/update_user_info")] + public async Task updateUser([FromBody] UpdateUser userUpdate) { try { - var res = await _userService.UpdateStatus(userUpdate.is_block, userUpdate.user_id); + var res = await _userService.UpdateUser(userUpdate); if (res != null) { - var actionResult = new ServiceResult((int)ApiStatus.Success, "Cập nhật thông tin khách hàng thành công!", "", null, null); - + var actionResult = new ServiceResult(1, "Cập nhật thông tin tài khoản thành công!", "", res, null); return Ok(actionResult); } else { - var actionResult = new ServiceResult((int)ApiStatus.Fail, "Cập nhật thất bại!", "", + var actionResult = new ServiceResult(2, "Cập nhật thất bại!", "", null, "204"); return Ok(actionResult); } @@ -228,4 +207,30 @@ public async Task UpdateStatus([FromBody] UpdateUser userUpdate) return Ok(actionResult); } } + + // [HttpPut("updateStatus")] + // public async Task UpdateStatus([FromBody] UpdateUser userUpdate) + // { + // try + // { + // var res = await _userService.UpdateStatus(userUpdate.is_block, userUpdate.user_id); + // if (res != null) + // { + // var actionResult = new ServiceResult((int)ApiStatus.Success, "Cập nhật thông tin khách hàng thành công!", "", null, null); + // + // return Ok(actionResult); + // } + // else + // { + // var actionResult = new ServiceResult((int)ApiStatus.Fail, "Cập nhật thất bại!", "", + // null, "204"); + // return Ok(actionResult); + // } + // } + // catch (Exception exception) + // { + // var actionResult = new ServiceResult((int)ApiStatus.Exception, Resources.error, exception.Message, null, "500"); + // return Ok(actionResult); + // } + // } } \ No newline at end of file diff --git a/Dictionary.Repo/Repo/UserRepo.cs b/Dictionary.Repo/Repo/UserRepo.cs index c2190ea..cb1668b 100644 --- a/Dictionary.Repo/Repo/UserRepo.cs +++ b/Dictionary.Repo/Repo/UserRepo.cs @@ -41,23 +41,24 @@ public async Task GetUserInfo(Guid id) public async Task Login(LoginModel model) { var sql = string.Format(@"SELECT * FROM {0} - WHERE (email=@email) + WHERE (user_name=@user_name) LIMIT 1;", GetTableName(typeof(UserEntity))); var param = new Dictionary { - { "email", model.username } + { "user_name", model.username } }; var result = await Provider.QueryAsync(sql, param); var res = result?.FirstOrDefault(); if (res == null) - throw new ValidateException("Tài khoản không tồn tại, vui lòng kiểm tra lại", model, - int.Parse(ResultCode.WrongAccount)); + throw new ValidateException("Email hoặc mật khẩu không chính xác!", model, + 1000); - // var verified = BCrypt.Net.BCrypt.Verify(model.password, res.password); - // if (!verified) - // throw new ValidateException("Mật khẩu không chính xác, vui lòng kiểm tra lại", model, - // int.Parse(ResultCode.WrongPassword)); + var verified = (model.password == res.password); + + if (!verified) + throw new ValidateException("Email hoặc mật khẩu không chính xác!", model, + 1000); return result.FirstOrDefault(); } diff --git a/Dictionary.Service/DtoEdit/Authentication/ForgotModel.cs b/Dictionary.Service/DtoEdit/Authentication/ForgotModel.cs new file mode 100644 index 0000000..469cdf5 --- /dev/null +++ b/Dictionary.Service/DtoEdit/Authentication/ForgotModel.cs @@ -0,0 +1,6 @@ +namespace Dictionary.Service.DtoEdit.Authentication; + +public class ForgotModel +{ + public string email { get; set; } +} \ No newline at end of file diff --git a/Dictionary.Service/DtoEdit/Authentication/ResetPassword.cs b/Dictionary.Service/DtoEdit/Authentication/ResetPassword.cs index 3fe5ad1..83a8d36 100644 --- a/Dictionary.Service/DtoEdit/Authentication/ResetPassword.cs +++ b/Dictionary.Service/DtoEdit/Authentication/ResetPassword.cs @@ -12,7 +12,7 @@ public class ResetPassword /// /// password hiện tại /// - public string password { get; set; } + public string old_password { get; set; } /// /// new_password diff --git a/Dictionary.Service/DtoEdit/Authentication/SignupModel.cs b/Dictionary.Service/DtoEdit/Authentication/SignupModel.cs index 77e4a74..531d58c 100644 --- a/Dictionary.Service/DtoEdit/Authentication/SignupModel.cs +++ b/Dictionary.Service/DtoEdit/Authentication/SignupModel.cs @@ -1,74 +1,7 @@ namespace Dictionary.Service.DtoEdit.Authentication; public class SignupModel -{ - /// - /// Họ người dùng - /// - public string first_name { get; set; } - - /// - /// Tên người dùng - /// - public string last_name { get; set; } - - /// - /// Địa chỉ email của người dùng - /// - public string email { get; set; } - - /// - /// Mật khẩu đăng nhập - /// +{ + public string username { get; set; } public string password { get; set; } - - /// - /// Số điện thoại người dùng - /// - public string phone { get; set; } - - /// - /// Đường dẫn ảnh đại diện - /// - public string avatar { get; set; } - - /// - /// Tỉnh/ thành phố - /// - public string province { get; set; } - - /// - /// Mã tỉnh / thành phố - /// - public int province_code { get; set; } - - /// - /// quận/ huyện - /// - public string district { get; set; } - - /// - /// Mã quận huyện - /// - public int district_code { get; set; } - - /// - /// xã/ phường - /// - public string commune { get; set; } - - /// - /// Mã xã phường - /// - public int commune_code { get; set; } - - /// - /// Địa chỉ chi tiết - /// - public string address_detail { get; set; } - - /// - /// Giới tính - /// - public int? gender { get; set; } } \ No newline at end of file diff --git a/Dictionary.Service/DtoEdit/Authentication/UpdateUser.cs b/Dictionary.Service/DtoEdit/Authentication/UpdateUser.cs index c76e961..fcffe5a 100644 --- a/Dictionary.Service/DtoEdit/Authentication/UpdateUser.cs +++ b/Dictionary.Service/DtoEdit/Authentication/UpdateUser.cs @@ -9,35 +9,13 @@ public class UpdateUser /// public Guid user_id { get; set; } - /// - /// Họ người dùng - /// - public string first_name { get; set; } - - /// - /// Tên người dùng - /// - public string last_name { get; set; } - - /// - /// Số điện thoại người dùng - /// - public string phone { get; set; } - - /// - /// Giới tính - /// - public int? gender { get; set; } - - /// - /// Ngày sinh - /// - public DateTime? date_of_birth { get; set; } + public string displayName { get; set; } - /// - /// user_id - /// - public string email { get; set; } + public string fullName { get; set; } - public bool is_block { get; set; } + public string birthday { get; set; } + + public string position { get; set; } + + public string avatar { get; set; } } \ No newline at end of file diff --git a/Dictionary.Service/Interfaces/Service/IUserService.cs b/Dictionary.Service/Interfaces/Service/IUserService.cs index c3a53c4..011b80c 100644 --- a/Dictionary.Service/Interfaces/Service/IUserService.cs +++ b/Dictionary.Service/Interfaces/Service/IUserService.cs @@ -27,14 +27,14 @@ public interface IUserService : IBaseService /// Reset Password /// /// - Task ResetPassword(ResetPassword resetPassword); + Task ResetPassword(ResetPassword resetPassword); /// /// update user info /// /// /// - Task UpdateUser(UpdateUser updateUser); + Task> UpdateUser(UpdateUser updateUser); /// /// Lấy token @@ -45,4 +45,6 @@ public interface IUserService : IBaseService /// chập nhật trạng thái /// Task UpdateStatus(bool status, Guid userId); + + Task ForgotPassword(ForgotModel forgotModel); } \ No newline at end of file diff --git a/Dictionary.Service/Middlewares/AuthenContextHandleMiddleware.cs b/Dictionary.Service/Middlewares/AuthenContextHandleMiddleware.cs index 8fcb7fb..ccbe213 100644 --- a/Dictionary.Service/Middlewares/AuthenContextHandleMiddleware.cs +++ b/Dictionary.Service/Middlewares/AuthenContextHandleMiddleware.cs @@ -126,6 +126,9 @@ private bool NoRequestAuthentication(string path) "/api/Users/signup", //test "/api/Dictionaries", + "api/Users/account/forgot_password", + "api/Users/user/update_password", + "user/update_user_info" }; if (lstPath.Any(x => path.Contains(x))) return true; return false; diff --git a/Dictionary.Service/Service/UserService.cs b/Dictionary.Service/Service/UserService.cs index 62a9406..7b10642 100644 --- a/Dictionary.Service/Service/UserService.cs +++ b/Dictionary.Service/Service/UserService.cs @@ -67,27 +67,47 @@ public async Task> Login(LoginModel model) /// /// Reset Password /// - public async Task ResetPassword(ResetPassword resetPassword) + public async Task ResetPassword(ResetPassword resetPassword) { - var field = "user_id"; - object value = resetPassword.user_id; - - var existedUser = await _userRepo.GetAsync(field, value); + var existedUser = await _userRepo.GetAsync("user_id", resetPassword.user_id); var res = existedUser.FirstOrDefault(); - if (res == null) throw new ValidateException("User doesn't exist", "", 400); - var verified = BCrypt.Net.BCrypt.Verify(resetPassword.password, res.password); + if (res == null) + { + return new ServiceResult( + 2, + "Người dùng không tồn tại", + "", + null, + "400" + ); + } + var verified = resetPassword.old_password == res.password; if (!verified) - throw new ValidateException("Mật khẩu không chính xác, vui lòng kiểm tra lại", 0, - int.Parse(ResultCode.WrongPassword)); - var encodedData = BCrypt.Net.BCrypt.HashPassword(resetPassword.new_password); - res.password = encodedData; + { + return new ServiceResult( + 2, + "Mật khẩu cũ không chính xaác!", + "", + null, + "1000" + ); + } + + res.password = resetPassword.new_password; await _userRepo.UpdateAsync(res, "password"); + + return new ServiceResult( + 1, + "Cập nhật mật khẩu thành công!", + "", + null, + "" + ); } - - - public async Task UpdateUser(UpdateUser updateUser) + + public async Task> UpdateUser(UpdateUser updateUser) { var field = "user_id"; object value = updateUser.user_id; @@ -95,29 +115,39 @@ public async Task UpdateUser(UpdateUser updateUser) var existedUser = await _userRepo.GetAsync(field, value); var res = existedUser.FirstOrDefault(); - if (res == null) throw new ValidateException("User doesn't exist", null, 400); - res.email = updateUser.email; - res.birthday = updateUser.date_of_birth; + if (res == null) return null; + res.birthday = DateTime.Parse(updateUser.birthday); + res.avatar = updateUser.avatar; + res.full_name = updateUser.fullName; + res.position = updateUser.position; + res.display_name = updateUser.displayName; await _userRepo.UpdateAsync(res); - var data = await _userRepo.GetUserInfo(updateUser.user_id); - - return data; + return new Dictionary + { + {"displayName", res.display_name}, + {"avatar", res.avatar} + }; } public async Task Signup(SignupModel model) { var newUser = new UserEntity(); newUser.user_id = Guid.NewGuid(); - newUser.email = model.email; - newUser.password = BCrypt.Net.BCrypt.HashPassword(model.password); - // newUser.avatar = Common.SaveImage(_httpContextAccessor.HttpContext.Request.Host.Value, model.avatar); + newUser.email = model.username; + newUser.password = model.password; + // Check tồn tại Email var existUserEmail = (await _userRepo.GetAsync("email", newUser.email))?.FirstOrDefault(); - // if (existUserEmail != null) - // return new ServiceResult(int.Parse(ResultCode.ExistEmail), Resources.msgExistEmail, "", newUser); - // Check tồn tại Số điện thoại + if (existUserEmail != null) + return new ServiceResult( + 2, + Resources.msgExistEmail, + "", + null, + "1001"); + var user = await _userRepo.InsertAsync(newUser); var context = new ContextData(); @@ -132,8 +162,7 @@ public async Task Signup(SignupModel model) .GetConnectionString("JwtTokenConfig")); var data = await GetContextReturn(context, jwtTokenConfig); - // return new ServiceResult(200, Resources.signupSuccess, "", data); - return null; + return new ServiceResult(1, Resources.signupSuccess, "", data, ""); } public async Task> GetToken(Guid userId) @@ -165,11 +194,20 @@ public async Task UpdateStatus(bool status, Guid userId) return user; } - /// - /// Lấy thông tin trả về client - /// - /// - /// Thời gian hết hạn + public async Task ForgotPassword(ForgotModel forgotModel) + { + var user = await _userRepo.GetAsync("email", forgotModel.email); + + var foundUser = user.FirstOrDefault(); + + if (foundUser == null) + { + return new ServiceResult(2, "Không tồn tại email người dùng!" , "", null, "1002"); + } + + return new ServiceResult(1, "Send verify token successfully!", "", null, ""); + } + private async Task> GetContextReturn(ContextData context, JwtTokenConfig jwtTokenConfig) { From 95e790261cba244262640f2d45c90cb16ced3f44 Mon Sep 17 00:00:00 2001 From: "nghiann@zyyx.jp" Date: Fri, 21 Jul 2023 09:54:12 +0700 Subject: [PATCH 3/4] fix: nghiann refactor --- Dictionary.Api/Controllers/UserController.cs | 100 +++++++++++++----- .../DtoEdit/Authentication/ResetPassword.cs | 4 - .../Authentication/ResetPasswordByToken.cs | 8 ++ .../DtoEdit/Authentication/SendActiveEmail.cs | 8 ++ .../Interfaces/Service/IUserService.cs | 6 ++ .../AuthenContextHandleMiddleware.cs | 5 +- Dictionary.Service/Model/UserEntity.cs | 2 + Dictionary.Service/Service/UserService.cs | 90 +++++++++++++++- 8 files changed, 191 insertions(+), 32 deletions(-) create mode 100644 Dictionary.Service/DtoEdit/Authentication/ResetPasswordByToken.cs create mode 100644 Dictionary.Service/DtoEdit/Authentication/SendActiveEmail.cs diff --git a/Dictionary.Api/Controllers/UserController.cs b/Dictionary.Api/Controllers/UserController.cs index d62f29e..ed7cafb 100644 --- a/Dictionary.Api/Controllers/UserController.cs +++ b/Dictionary.Api/Controllers/UserController.cs @@ -182,9 +182,67 @@ public async Task UpdatePassword([FromBody] ResetPassword resetPa return Ok(actionResult); } } + + [HttpPut("/user/reset_password")] + public async Task ResetPassword([FromBody] ResetPasswordByToken resetPasswordByToken) + { + try + { + var res = await _userService.ResetPasswordByVerifyToken(resetPasswordByToken); + return Ok(res); + } + catch (Exception exception) + { + var actionResult = new ServiceResult(-1, Resources.error, exception.Message, null, "500"); + return Ok(actionResult); + } + } + + [HttpPost("/account/send_activate_email")] + + public async Task SendActiveEmail([FromBody] SendActiveEmail sendActiveEmail) + { + { + try + { + var res = await _userService.SendActiveEmail(sendActiveEmail); + return Ok(res); + } + catch (Exception exception) + { + var actionResult = new ServiceResult(-1, Resources.error, exception.Message, null, "500"); + return Ok(actionResult); + } + } + } + + [HttpGet("/account/activate_account")] + public async Task ActiveAccount() + { + try + { + var res = await _userService.ActiveAccount(); + if (res != null) + { + var actionResult = new ServiceResult(1, "Cập nhật thông tin tài khoản thành công!", "", res, null); + return Ok(actionResult); + } + else + { + var actionResult = new ServiceResult(2, "Cập nhật thất bại!", "", + null, "204"); + return Ok(actionResult); + } + } + catch (Exception exception) + { + var actionResult = new ServiceResult((int)ApiStatus.Exception, Resources.error, exception.Message, null, "500"); + return Ok(actionResult); + } + } [HttpPut("/user/update_user_info")] - public async Task updateUser([FromBody] UpdateUser userUpdate) + public async Task UpdateUser([FromBody] UpdateUser userUpdate) { try { @@ -208,29 +266,19 @@ public async Task updateUser([FromBody] UpdateUser userUpdate) } } - // [HttpPut("updateStatus")] - // public async Task UpdateStatus([FromBody] UpdateUser userUpdate) - // { - // try - // { - // var res = await _userService.UpdateStatus(userUpdate.is_block, userUpdate.user_id); - // if (res != null) - // { - // var actionResult = new ServiceResult((int)ApiStatus.Success, "Cập nhật thông tin khách hàng thành công!", "", null, null); - // - // return Ok(actionResult); - // } - // else - // { - // var actionResult = new ServiceResult((int)ApiStatus.Fail, "Cập nhật thất bại!", "", - // null, "204"); - // return Ok(actionResult); - // } - // } - // catch (Exception exception) - // { - // var actionResult = new ServiceResult((int)ApiStatus.Exception, Resources.error, exception.Message, null, "500"); - // return Ok(actionResult); - // } - // } + [HttpGet("/user/logout")] + + public async Task Logout() + { + try + { + var res = new ServiceResult(1, "Logout User!", "", null, ""); + return Ok(res); + } + catch (Exception exception) + { + var actionResult = new ServiceResult((int)ApiStatus.Exception, Resources.error, exception.Message, null, "500"); + return Ok(actionResult); + } + } } \ No newline at end of file diff --git a/Dictionary.Service/DtoEdit/Authentication/ResetPassword.cs b/Dictionary.Service/DtoEdit/Authentication/ResetPassword.cs index 83a8d36..2f046f2 100644 --- a/Dictionary.Service/DtoEdit/Authentication/ResetPassword.cs +++ b/Dictionary.Service/DtoEdit/Authentication/ResetPassword.cs @@ -4,10 +4,6 @@ namespace Dictionary.Service.DtoEdit.Authentication; public class ResetPassword { - /// - /// user_id - /// - public Guid user_id { get; set; } /// /// password hiện tại diff --git a/Dictionary.Service/DtoEdit/Authentication/ResetPasswordByToken.cs b/Dictionary.Service/DtoEdit/Authentication/ResetPasswordByToken.cs new file mode 100644 index 0000000..a6f665a --- /dev/null +++ b/Dictionary.Service/DtoEdit/Authentication/ResetPasswordByToken.cs @@ -0,0 +1,8 @@ +namespace Dictionary.Service.DtoEdit.Authentication; + +public class ResetPasswordByToken +{ + public string verifyToken { get; set; } + + public string newPassword { get; set; } +} \ No newline at end of file diff --git a/Dictionary.Service/DtoEdit/Authentication/SendActiveEmail.cs b/Dictionary.Service/DtoEdit/Authentication/SendActiveEmail.cs new file mode 100644 index 0000000..d63b723 --- /dev/null +++ b/Dictionary.Service/DtoEdit/Authentication/SendActiveEmail.cs @@ -0,0 +1,8 @@ +namespace Dictionary.Service.DtoEdit.Authentication; + +public class SendActiveEmail +{ + public string username { get; set; } + + public string password { get; set; } +} \ No newline at end of file diff --git a/Dictionary.Service/Interfaces/Service/IUserService.cs b/Dictionary.Service/Interfaces/Service/IUserService.cs index 011b80c..a0f9c11 100644 --- a/Dictionary.Service/Interfaces/Service/IUserService.cs +++ b/Dictionary.Service/Interfaces/Service/IUserService.cs @@ -47,4 +47,10 @@ public interface IUserService : IBaseService Task UpdateStatus(bool status, Guid userId); Task ForgotPassword(ForgotModel forgotModel); + + Task ActiveAccount(); + + Task ResetPasswordByVerifyToken(ResetPasswordByToken resetPasswordByToken); + + Task SendActiveEmail(SendActiveEmail sendActiveEmail); } \ No newline at end of file diff --git a/Dictionary.Service/Middlewares/AuthenContextHandleMiddleware.cs b/Dictionary.Service/Middlewares/AuthenContextHandleMiddleware.cs index ccbe213..31d86ee 100644 --- a/Dictionary.Service/Middlewares/AuthenContextHandleMiddleware.cs +++ b/Dictionary.Service/Middlewares/AuthenContextHandleMiddleware.cs @@ -128,7 +128,10 @@ private bool NoRequestAuthentication(string path) "/api/Dictionaries", "api/Users/account/forgot_password", "api/Users/user/update_password", - "user/update_user_info" + "user/update_user_info", + "/user/logout", + "/user/reset_password", + "/account/send_activate_email" }; if (lstPath.Any(x => path.Contains(x))) return true; return false; diff --git a/Dictionary.Service/Model/UserEntity.cs b/Dictionary.Service/Model/UserEntity.cs index ed51e37..42a7c6e 100644 --- a/Dictionary.Service/Model/UserEntity.cs +++ b/Dictionary.Service/Model/UserEntity.cs @@ -35,4 +35,6 @@ public class UserEntity public DateTime created_at { get; set; } public DateTime modified_at { get; set; } + + public string verify_token { get; set; } } \ No newline at end of file diff --git a/Dictionary.Service/Service/UserService.cs b/Dictionary.Service/Service/UserService.cs index 7b10642..997bf4b 100644 --- a/Dictionary.Service/Service/UserService.cs +++ b/Dictionary.Service/Service/UserService.cs @@ -69,7 +69,8 @@ public async Task> Login(LoginModel model) /// public async Task ResetPassword(ResetPassword resetPassword) { - var existedUser = await _userRepo.GetAsync("user_id", resetPassword.user_id); + Guid user_id = new Guid("b0da65f9-dc39-11ed-a1e6-a44cc8756a37"); + var existedUser = await _userRepo.GetAsync("user_id", user_id); var res = existedUser.FirstOrDefault(); if (res == null) @@ -208,6 +209,93 @@ public async Task ForgotPassword(ForgotModel forgotModel) return new ServiceResult(1, "Send verify token successfully!", "", null, ""); } + public async Task ActiveAccount() + { + string verifyToken = "verify_token_example"; + Guid user_id = new Guid("b0da65f9-dc39-11ed-a1e6-a44cc8756a37"); + + var query_user = await _userRepo.GetAsync("user_id", user_id); + + var user = query_user.FirstOrDefault(); + + if (user == null) + { + return new ServiceResult(2, "User not exist!", "", null, "1001"); + } + + if (user.verify_token != verifyToken) + { + return new ServiceResult(2, "Token không hợp lệ!", "", null, "1003"); + } + + if (user.status == 1) + { + return new ServiceResult(2, "Người dùng đã kích hoạt tài khoản!", "", null, "9000"); + } + + user.status = 1; + user.verify_token = ""; + + await UpdateAsync(user); + + return new ServiceResult(1, "Kích hoạt tài khoản thành công!", "", null, ""); + } + + public async Task ResetPasswordByVerifyToken(ResetPasswordByToken resetPasswordByToken) + { + string verifyToken = resetPasswordByToken.verifyToken; + Guid user_id = new Guid("b0da65f9-dc39-11ed-a1e6-a44cc8756a37"); + + var query_user = await _userRepo.GetAsync("user_id", user_id); + + var user = query_user.FirstOrDefault(); + + if (user == null) + { + return new ServiceResult(2, "User not exist!", "", null, "1001"); + } + + if (user.verify_token != verifyToken) + { + return new ServiceResult(2, "Token không hợp lệ!", "", null, "1003"); + } + + user.password = resetPasswordByToken.newPassword; + user.verify_token = ""; + + await _userRepo.UpdateAsync(user); + + return new ServiceResult(1, "Cập nhật mật khẩu thành công!", "", null, ""); + } + + public async Task SendActiveEmail(SendActiveEmail sendActiveEmail) + { + var query_user = await _userRepo.GetAsync("email", sendActiveEmail.username); + + var user = query_user.FirstOrDefault(); + + if (user == null) + { + return new ServiceResult(2, "Email chưa được đăng ký!", "", null, "1003"); + } + + if (user.status == 1) + { + return new ServiceResult(2, "Tài khoản đã được kích hoạt!", "", null, "1003"); + } + + if (user.password != sendActiveEmail.password) + { + return new ServiceResult(2, "Tài khoản không tồn tại!", "", null, "1003"); + } + + user.verify_token = "verify_token"; + + await _userRepo.UpdateAsync(user); + + return new ServiceResult(1, "Gửi token kích hoạt tài khoản thành công!", "", null, ""); + } + private async Task> GetContextReturn(ContextData context, JwtTokenConfig jwtTokenConfig) { From c1f55844d1ff391642d071f7bab0a16c6060e350 Mon Sep 17 00:00:00 2001 From: "nghiann@zyyx.jp" Date: Fri, 21 Jul 2023 09:56:32 +0700 Subject: [PATCH 4/4] remove idea --- .gitignore | 2 +- Dictionary.Api/appsettings.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index ed31b08..cede3ab 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ /Dictionary.Repo/obj/ /Dictionary.Service/bin/ /Dictionary.Service/obj/ -.idea \ No newline at end of file +.idea/ \ No newline at end of file diff --git a/Dictionary.Api/appsettings.json b/Dictionary.Api/appsettings.json index ebf17e9..71ec737 100644 --- a/Dictionary.Api/appsettings.json +++ b/Dictionary.Api/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "DefaultConnection": "Host= 127.0.0.1; Port= 3306; Database= dictionary; User Id= root; Password = Hoangkm13@", + "DefaultConnection": "Host= 127.0.0.1; Port= 3306; Database= dictionary; User Id= root; Password =", "JwtTokenConfig": "{\"SecretKey\":\"DATN_WEB_2022_dat.dq173003\",\"ExpiredSeconds\":86400}" }, "Logging": {