my post is going to get. : learnprogramming

my repository url: https://github.com/jonathanIckovich/realtyKing.git

im using spring boot at for my login page im trying to get it so the post mapping login is entered after a post request form. from login.html. for some reason it keeps going to the get mapping for a post request how do i fix this?

using spring boot thymleaf

my html page login.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="utf-8">
        <title>Log in with your account</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">

    </head>

    <body>
    <div class="container">
        <form class="form-signin" method="POST" action="/login" th:action="@{/login}" th:object="${sighnUp}">
            <h2 class="form-heading">Log in</h2>

            <div class="form-group">

                <span th:text="${message}"></span>
                <input  type="text" th:field="*{username}" th:placeholder="Username" autofocus="true" class="form-control"/>
                <span class="has-error" th:if="${#fields.hasErrors('username')}" th:errors="*{username}"></span>

                <input type="password" th:field="*{password}" th:placeholder="password" class="form-control"/>
                <span class="has-error" th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></span>

                <button class="btn btn-lg btn-primary btn-block" type="submit">Log In</button>


                </div>
            </form>
        <h4 class="text-center"><a href="/userSighnup">Create an account</a></h4>
        </div>


    </body>
</html>

my controller

import com.example.realtyKing.Model.SighnUp;
import com.example.realtyKing.Model.User;
import com.example.realtyKing.Model.UserAuthentification;
import com.example.realtyKing.Service.SecurityService;
import com.example.realtyKing.Service.UserAuthentificationService;
import com.example.realtyKing.Service.UserService;
import com.example.realtyKing.Utility.MiscleaniousServices;
import com.example.realtyKing.Utility.UserCreationValidator;
import com.example.realtyKing.Utility.UserLoginValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

//fix the id problem.

@Controller
public class UserController {
//find a way for cookie to check if cookie is right instead of just not null.
    @Autowired
    private UserService userService;
    @Autowired
    private UserAuthentificationService userAuthentificationService;

    @Autowired
    private UserLoginValidator userAuthenticatorValidator;

    @Autowired
    private UserCreationValidator userCreationValidator;

    @Autowired
    private SecurityService securityService;

    @Autowired
    MiscleaniousServices miscleaniousServices;

    @Autowired
    UserLoginValidator userLoginValidator;

    @GetMapping("/userSighnup")
    public String userAuthentificationForm(Model model)
    {
        //if authentification has been succesful
        if (securityService.isAuthenticated()) {
            return "redirect:/home";
        }
        model.addAttribute("User", miscleaniousServices.user());
        model.addAttribute("UserAuthentification", miscleaniousServices.userAuthentification());
        model.addAttribute("errors"," ");
        return "new_user";
    }
    //finsihed user sighnup
    @PostMapping("/userSighnup")
    public String userAuthentificationForm(@ModelAttribute("User") User user,
                                           @ModelAttribute("UserAuthentification") UserAuthentification userAuthentification
            , BindingResult bindingResult,Model model) {
        userAuthentification.setUser(user);
        user.setUserAuthentification(userAuthentification);
        userCreationValidator.validate(user,userAuthentification, bindingResult);

                if (bindingResult.hasErrors()) {
        //            return "registration";
                    model.addAttribute("User", new User());
                    model.addAttribute("UserAuthentification", new UserAuthentification());
                    return "new_user";
                }

                userService.saveUser(user);
                userAuthentification.setUser(user);
                userAuthentificationService.saveUserAuthentification(userAuthentification);
        //      userService.save(userForm);

                return "redirect:/login";
    }
//fix sighnup authentification

    //fixed get login.
    @GetMapping("/login")
    public String login(  @ModelAttribute("sighnUp") SighnUp sighnUp, Model model,
                          @CookieValue(name = "user-name", defaultValue = "null") String userName,
                        @CookieValue(name = "user-password", defaultValue = "null") String userPassword)
    {

        //if authentification has been succesful
        if (securityService.isAuthenticated()&&userName!=null) {
            UserAuthentification userAuthentification =userAuthentificationService.findByUsername(userName);
            User user=userAuthentification.getUser();
            model.addAttribute("userAuthentification", userAuthentification);
            model.addAttribute("user", user);
            return "redirect:/home";
        }
        if( sighnUp.getUsername() != null )
        {

            ResponseCookie springCookieUser = ResponseCookie.from("user-name",userName)
                    .httpOnly(true)
                    .secure(true)
                    .path("/login")
                    .maxAge(60*60*24*124)
                    .domain("realtyKing.com")
                    .build();

            ResponseCookie springCookiePassword = ResponseCookie.from("user-password",userPassword)
                    .httpOnly(true)
                    .secure(true)
                    .path("/login")
                    .maxAge(60*60*24*124)
                    .domain("realtyKing.com")
                    .build();

            ResponseEntity
                    .ok()
                    .header(HttpHeaders.SET_COOKIE, springCookieUser.toString())
                    .build();

            ResponseEntity
                    .ok()
                    .header(HttpHeaders.SET_COOKIE, springCookiePassword.toString())
                    .build();

            UserAuthentification userAuthentification =userAuthentificationService.findByUsername(userName);
            User user=userAuthentification.getUser();
            model.addAttribute("userAuthentification", userAuthentification);
            model.addAttribute("user", user);

            //authenticate
            securityService.autoLogin(sighnUp.getUsername(), sighnUp.getPassword());

            return "index";
        }
        //if cookie has not been initialised yet.

        model.addAttribute("sighnUp", miscleaniousServices.sighnUp());

        if(userName!=null && userPassword!=null&&!userName.equals("null")&&!userPassword.equals("null"))
        {
            model.addAttribute("username",userName);
            model.addAttribute("password",userPassword);
        }
        else
        {
            model.addAttribute("username"," ");
            model.addAttribute("password"," ");
        }
        //
        return "login";
    }
@PostMapping("/login")
public String postLogin(Model model){
        return "login";
}
        // display list of users
    @GetMapping("/home")
    public String viewHomePage(Model model, HttpSession session,@CookieValue(name = "user-name") String userName) {


        if (!securityService.isAuthenticated()) {
            return "redirect:/login";
        }
        UserAuthentification userAuthentification=userAuthentificationService.findByUsername(userName);
        User user=userAuthentification.getUser();
        model.addAttribute("User",user);
        return "index";
    }

    @GetMapping("/showFormForUpdate")
    public String showFormForUpdate( Model model, @CookieValue(name = "user-name") String userName,HttpSession session) {



        if (!securityService.isAuthenticated()) {
            return "redirect:/login";
        }
        UserAuthentification userAuthentification=userAuthentificationService.findByUsername(userName);
        User user=userAuthentification.getUser();
        model.addAttribute("User",user);


        // set user as a model attribute to pre-populate the form

        model.addAttribute("UserAuthentification", userAuthentification);
        return "update_user";
    }

    @GetMapping("/deleteUser")
    public String deleteUser(@CookieValue(name = "user-name") String userName,HttpSession session) {

        if (!securityService.isAuthenticated()) {
            return "redirect:/login";
        }
        UserAuthentification userAuthentification=userAuthentificationService.findByUsername(userName);
        User user=userAuthentification.getUser();
        // call delete user method
        this.userService.deleteUserById(user.getUserId());
        this.userAuthentificationService.deleteUserAuthentificationByUserId(userAuthentification.getAuthId());
        return "redirect:/login";
    }
}

Leave a Comment