diff --git a/src/main/java/com/gcc_it_cell_services/springboot/controller/HomeController.java b/src/main/java/com/gcc_it_cell_services/springboot/controller/HomeController.java index 72e82a6..e9e4e0c 100644 --- a/src/main/java/com/gcc_it_cell_services/springboot/controller/HomeController.java +++ b/src/main/java/com/gcc_it_cell_services/springboot/controller/HomeController.java @@ -24,12 +24,18 @@ public class HomeController { private UrlDataService urlDataSer; @GetMapping({ "/", "" }) - public String home(Model model, Authentication auth) { + public String home(Model model, Authentication auth, javax.servlet.http.HttpServletRequest req) { List urls = urlDataSer.getAllUrls(); // Add role information to the model for conditional navbar rendering boolean isAdmin = auth != null && auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN")); + + javax.servlet.http.HttpSession session = req.getSession(false); + boolean isOtpVerified = session != null && session.getAttribute("otpVerified") != null && (Boolean) session.getAttribute("otpVerified"); + boolean isLoggedIn = (auth != null && auth.isAuthenticated() && !auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ANONYMOUS"))) || isOtpVerified; + model.addAttribute("isAdmin", isAdmin); + model.addAttribute("isLoggedIn", isLoggedIn); model.addAttribute("curPage", "home"); model.addAttribute("urls", urls); return "home/home"; @@ -45,7 +51,13 @@ public class HomeController { // Add role information to the model for conditional navbar rendering boolean isAdmin = auth != null && auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN")); + + if (!isAdmin) { + return "redirect:/"; // Redirect to home if not admin + } + model.addAttribute("isAdmin", isAdmin); + model.addAttribute("isLoggedIn", true); List urls = urlDataSer.getAllUrlsWithUsrNameAndPass(); model.addAttribute("urls", urls); model.addAttribute("curPage", "view-login-det"); diff --git a/src/main/java/com/gcc_it_cell_services/springboot/controller/UrlDataController.java b/src/main/java/com/gcc_it_cell_services/springboot/controller/UrlDataController.java index a7a6b10..5346eac 100644 --- a/src/main/java/com/gcc_it_cell_services/springboot/controller/UrlDataController.java +++ b/src/main/java/com/gcc_it_cell_services/springboot/controller/UrlDataController.java @@ -27,12 +27,13 @@ public class UrlDataController { boolean isAdmin = auth != null && auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN")); model.addAttribute("isAdmin", isAdmin); + model.addAttribute("isLoggedIn", isAdmin); model.addAttribute("curPage", "add-url"); return "urls/add-url-data"; } @GetMapping("/get-{urlCatName}-urls") - public String getUrlsByCategory(@PathVariable("urlCatName") String urlCatName, Model model, Authentication auth) { + public String getUrlsByCategory(@PathVariable("urlCatName") String urlCatName, Model model, Authentication auth, javax.servlet.http.HttpServletRequest req) { // Add role information to the model for conditional navbar rendering urlCatName = urlCatName.replace("-", " "); List urls = urlDataSer.getUrlsByUrlCatName(urlCatName); @@ -49,7 +50,21 @@ public class UrlDataController { boolean isAdmin = auth != null && auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN")); + + // Check if user is logged in (either via Spring Security authentication or session OTP verification) + javax.servlet.http.HttpSession session = req.getSession(false); + boolean isOtpVerified = session != null && session.getAttribute("otpVerified") != null && (Boolean) session.getAttribute("otpVerified"); + boolean isLoggedIn = (auth != null && auth.isAuthenticated() && !auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ANONYMOUS"))) || isOtpVerified; + + // Filter urls that actually have a username and password + List credentialUrls = urls.stream() + .filter(dto -> dto.getUrlDataUsrName() != null && !dto.getUrlDataUsrName().trim().equals("-") + && dto.getUrlDataUsrPass() != null && !dto.getUrlDataUsrPass().trim().equals("-")) + .collect(java.util.stream.Collectors.toList()); + model.addAttribute("isAdmin", isAdmin); + model.addAttribute("isLoggedIn", isLoggedIn); + model.addAttribute("credentialUrls", credentialUrls); model.addAttribute("urlCatName", resolvedCatName); model.addAttribute("groupedUrls", groupedUrls); model.addAttribute("urls", urls); @@ -64,6 +79,7 @@ public class UrlDataController { boolean isAdmin = auth != null && auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN")); model.addAttribute("isAdmin", isAdmin); + model.addAttribute("isLoggedIn", isAdmin); model.addAttribute("curPage", "upd-url"); model.addAttribute("stats", stats); model.addAttribute("urls", urls); diff --git a/src/main/java/com/gcc_it_cell_services/springboot/rest_controller/HomeRestController.java b/src/main/java/com/gcc_it_cell_services/springboot/rest_controller/HomeRestController.java index 5718e21..c9e8b89 100644 --- a/src/main/java/com/gcc_it_cell_services/springboot/rest_controller/HomeRestController.java +++ b/src/main/java/com/gcc_it_cell_services/springboot/rest_controller/HomeRestController.java @@ -66,11 +66,12 @@ public class HomeRestController { HttpSession ses = req.getSession(false); ServiceResponseDTO serResDTO = homeSer.verifyOtp(verifyOtpReqDTO, ses); if (serResDTO.isResStat()) { - // OTP verified successfully → cleanup session + // OTP verified successfully → cleanup session and set otpVerified = true ses.removeAttribute("otp"); ses.removeAttribute("otpTimestamp"); ses.removeAttribute("mobNum"); ses.removeAttribute("otpLogId"); + ses.setAttribute("otpVerified", true); return ResponseEntity.ok(serResDTO); } else { // OTP failed → remove old OTP only diff --git a/src/main/resources/static/assets/js/home-script.js b/src/main/resources/static/assets/js/home-script.js index 56de5a5..e0c1023 100644 --- a/src/main/resources/static/assets/js/home-script.js +++ b/src/main/resources/static/assets/js/home-script.js @@ -271,12 +271,11 @@ document.addEventListener("DOMContentLoaded", () => { await reloadMobileDropdown(); - // Close modal, then redirect to view-login-det + // Close modal, then reload the page setTimeout(() => { closeLoginModal(); - window.open(`${contextPath}view-login-det`, '_blank'); + window.location.reload(); }, 500); - } else { // OTP verification failed or session expired messageEl.textContent = data.res || "OTP verification failed. Please request a new one."; @@ -397,6 +396,10 @@ document.addEventListener("DOMContentLoaded", function () { btn.style.display = "none"; } }); + + if (typeof window.updateDropdownCredentials === "function") { + window.updateDropdownCredentials(); + } } function showState2(subcatName) { @@ -431,6 +434,10 @@ document.addEventListener("DOMContentLoaded", function () { btn.style.display = "none"; } }); + + if (typeof window.updateDropdownCredentials === "function") { + window.updateDropdownCredentials(); + } } /* === FILTER BUTTONS === */ @@ -480,6 +487,10 @@ document.addEventListener("DOMContentLoaded", function () { clearBtn.style.display = "none"; populateAlphabetDropdown(false); } + + if (typeof window.updateDropdownCredentials === "function") { + window.updateDropdownCredentials(); + } } /* === CLICK HANDLERS === */ diff --git a/src/main/resources/templates/fragments/navbar.html b/src/main/resources/templates/fragments/navbar.html index d69bf91..496b51e 100644 --- a/src/main/resources/templates/fragments/navbar.html +++ b/src/main/resources/templates/fragments/navbar.html @@ -1,4 +1,4 @@ - + diff --git a/src/main/resources/templates/home/home.html b/src/main/resources/templates/home/home.html index 15e3b09..33d27cc 100644 --- a/src/main/resources/templates/home/home.html +++ b/src/main/resources/templates/home/home.html @@ -21,7 +21,7 @@ - +
@@ -52,7 +52,7 @@ ? 'window.location.href=this.dataset.link' : 'window.open(this.dataset.link, \'_blank\')'" class="custom-button"> - [[${url.urlDataNum}]] + [[${url.urlDataName}]]
diff --git a/src/main/resources/templates/urls/add-url-data.html b/src/main/resources/templates/urls/add-url-data.html index aa480b8..5872154 100644 --- a/src/main/resources/templates/urls/add-url-data.html +++ b/src/main/resources/templates/urls/add-url-data.html @@ -21,7 +21,7 @@ - +
diff --git a/src/main/resources/templates/urls/get-urls-by-url-category.html b/src/main/resources/templates/urls/get-urls-by-url-category.html index a528c6b..d6af0da 100644 --- a/src/main/resources/templates/urls/get-urls-by-url-category.html +++ b/src/main/resources/templates/urls/get-urls-by-url-category.html @@ -20,20 +20,66 @@ - +
-
+
+ + +
-