From 6200ad1b4e30f8e6ed3487b79169862fe4ed0572 Mon Sep 17 00:00:00 2001 From: TAMARA JERINIC Date: Thu, 8 Dec 2022 14:24:23 +0100 Subject: Dodata funkcija za promenu lozinke na back-u. Povezana promena lozinke na front-u. --- Backend/Api/Api/Controllers/UserController.cs | 6 ++++++ Backend/Api/Api/Interfaces/IUserService.cs | 2 ++ Backend/Api/Api/Services/UserService.cs | 24 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+) (limited to 'Backend/Api') diff --git a/Backend/Api/Api/Controllers/UserController.cs b/Backend/Api/Api/Controllers/UserController.cs index 73d32ea..30beac4 100644 --- a/Backend/Api/Api/Controllers/UserController.cs +++ b/Backend/Api/Api/Controllers/UserController.cs @@ -162,6 +162,12 @@ namespace Api.Controllers return Ok(await _userService.ChangeMyProfileName(newName)); } + [HttpPost("changePass")] + [Authorize(Roles = "User")] + public async Task> ChangePass(string currentPass, string newPass) + { + return Ok(await _userService.ChangePass(currentPass,newPass)); + } } diff --git a/Backend/Api/Api/Interfaces/IUserService.cs b/Backend/Api/Api/Interfaces/IUserService.cs index f4954e0..855272f 100644 --- a/Backend/Api/Api/Interfaces/IUserService.cs +++ b/Backend/Api/Api/Interfaces/IUserService.cs @@ -40,6 +40,8 @@ namespace Api.Interfaces Task ChangeMyProfileUsername(String newUsername); Task ChangeMyProfileName(String newUsername); + Task ChangePass(string currentPass, string newPass); + } } diff --git a/Backend/Api/Api/Services/UserService.cs b/Backend/Api/Api/Services/UserService.cs index 2b3f0b8..74c0894 100644 --- a/Backend/Api/Api/Services/UserService.cs +++ b/Backend/Api/Api/Services/UserService.cs @@ -733,6 +733,30 @@ namespace Api.Services return false; } + public async Task ChangePass(string currentPass,string newPass) + { + + string myId = null; + if (_httpContext.HttpContext.User.FindFirstValue("id") != null) + { + myId = _httpContext.HttpContext.User.FindFirstValue("id").ToString(); + } + + User u = await _users.Find(user => user._id == myId).FirstOrDefaultAsync(); + + if (u != null) + { + if (checkPassword(currentPass, u.password)) + { + u.password = hashPassword(newPass); + await _users.ReplaceOneAsync(x => x._id == u._id, u); + return 1; + } + return -1; + } + return -2; + } + } -- cgit v1.2.3