Seeking assistance with determining the success of a login using a SpringBoot Controller. Encountering an issue where unsuccessful logins result in a page displaying HTML -> false, indicating that JavaScript may not be running properly (e.g., failure: function()...etc
). Attempted utilizing async: false
without any changes.
The Auth class within the controller package is as follows. Even tried mapping the doLogin method with @ResponseBody, but it only displays a response on a new page encoded in JSON format. If @ResponseBody tag is removed, an error occurs stating that Boolean is unsupported as a response.
@Controller
@CrossOrigin("http://localhost:4200")
public class Auth {
@GetMapping("/login")
public String login(){
return "loginPage.html";
}
@PostMapping("/login")
public Boolean doLogin(HttpServletRequest httpRequest, HttpServletResponse httpResponse, @RequestParam String nickname, @RequestParam String password){
System.out.println(nickname);
System.out.println(password);
User user = DBManager.getInstance().getUserDao().findByPrimaryKey(nickname);
if(user != null) {
httpRequest.getSession().setAttribute("User", user);
try {
httpResponse.sendRedirect("http://localhost:4200/");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return false;
}
@GetMapping("/register")
public String register() {
return "registerPage.html";
}
@PostMapping("/register")
public void doRegister(@RequestParam String nickname){
System.out.println(nickname );
}
}
This is my JavaScript code linked to the Login Button.
window.onload = function(){
const button = document.getElementById("submitButton");
const nickname = document.getElementById('nicknameInput');
const password = document.getElementById('passwordInput');
button.addEventListener("click", function(event){
$.ajax({
method: 'POST',
url: 'http://localhost:8080/doLogin',
data: {'nickname': nickname.nodeValue, 'password': password.nodeValue},
success: function(response){
alert(response);
},
});
event.preventDefault();
});
}
Attempting to:
- Upon successful login, redirect to Angular homepage possibly through JavaScript
- In case of failed login, display an alert/label indicating an error
Your advice and input are highly appreciated.