Skip to content
Snippets Groups Projects
Commit fc110e81 authored by Gabriel Copat's avatar Gabriel Copat
Browse files

Merge branch...

Merge branch '70-as-a-repeat-trail-visitor-i-want-to-be-able-to-create-an-account-so-i-can-save-and-review-my' into 77-as-a-user-i-want-to-be-able-to-use-the-application-on-any-device-e-g-iphone-ipad-laptop

# Conflicts:
#	src/main/java/Team5/SmartTowns/users/UserController.java
#	src/main/resources/templates/users/login.html
parents 87ad0f8a 9564ad0d
No related branches found
No related tags found
1 merge request!36Resolve "As a user I want to be able to use the application on any device e.g. iPhone, iPad, Laptop"
...@@ -6,7 +6,7 @@ import Team5.SmartTowns.rewards.RewardsRepository; ...@@ -6,7 +6,7 @@ import Team5.SmartTowns.rewards.RewardsRepository;
import Team5.SmartTowns.rewards.Sticker; import Team5.SmartTowns.rewards.Sticker;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner; import org.springframework.dao.DataAccessException;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
...@@ -31,41 +31,58 @@ public class UserController { ...@@ -31,41 +31,58 @@ public class UserController {
public ModelAndView getLoginPage() { public ModelAndView getLoginPage() {
ModelAndView mav = new ModelAndView("users/login"); ModelAndView mav = new ModelAndView("users/login");
mav.addObject("user", new NewUser( "", "", "")); mav.addObject("user", new NewUser( "", "", ""));
mav.addObject("error", "");
mav.addObject("status", "");
System.out.println(userRepository.findUserByName("Admin").getName()); System.out.println(userRepository.findUserByName("Admin").getName());
return mav; return mav;
} }
@GetMapping("/logout") // @GetMapping("/logout")
public ModelAndView getLogOutPage(){ // public ModelAndView getLogOutPage(){
ModelAndView mav = new ModelAndView("users/logout"); // ModelAndView mav = new ModelAndView("users/logout");
return mav; // return mav;
} // }
@PostMapping("/login/register") @PostMapping("/login/register")
public ModelAndView registerUser(@Valid @ModelAttribute("user") NewUser user, BindingResult bindingResult, Model model) { public ModelAndView registerUser(@Valid @ModelAttribute("user") NewUser user, BindingResult bindingResult, Model model) {
ModelAndView mav = new ModelAndView("users/login", model.asMap()); ModelAndView mav = new ModelAndView("users/login", model.asMap());
// TODO VALIDATE EMAIL INPUT // TODO VALIDATE EMAIL INPUT
mav.addObject("status", "active");
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
ModelAndView modelAndView = new ModelAndView("users/login"); ModelAndView modelAndView = new ModelAndView("users/login");
modelAndView.addObject("errors", bindingResult); modelAndView.addObject("errors", bindingResult);
return modelAndView; return modelAndView;
} }
System.out.println(user.getName());
System.out.println(user.getPassword());
if ( userRepository.doesUserExist(user.getEmail()) ) { if ( userRepository.doesUserExist(user.getEmail()) ) {
//TODO return modelandview for user already exists mav.addObject("errors", "Email already in use");
System.out.println(user.getEmail() + " already exists");
return mav; return mav;
} else { }
try {
userRepository.addUser(user.name, user.email, user.password); userRepository.addUser(user.name, user.email, user.password);
System.out.println(user.getEmail() + " created"); mav.addObject("error", "");
//TODO return user creation success
return mav; return mav;
} catch (DataAccessException e) {
mav.addObject("error", "User exists");
} }
return mav;
} }
@GetMapping("/userProfile")
public ModelAndView userProfile(){
ModelAndView mav = new ModelAndView("users/userProfile");
List<Pack> allPacks = rewardsRepository.getAllPacks();
mav.addObject("packs", allPacks);
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
mav.addObject("user", userRepository.findUserByName("Admin"));
mav.addAllObjects(getPackInfo("Admin", 1).getModelMap());
return mav;
}
/* USER MAPPING & FUNCTIONS */ /* USER MAPPING & FUNCTIONS */
@GetMapping("/profile/{username}") @GetMapping("/profile/{username}")
public ModelAndView getUserPage(@PathVariable String username) { public ModelAndView getUserPage(@PathVariable String username) {
......
//Implements the users repository using JDBC //Implements the users repository using JDBC
package Team5.SmartTowns.users; package Team5.SmartTowns.users;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.object.SqlQuery; import org.springframework.jdbc.object.SqlQuery;
...@@ -59,7 +60,7 @@ public class UserRepositoryJDBC implements UserRepository{ ...@@ -59,7 +60,7 @@ public class UserRepositoryJDBC implements UserRepository{
} }
@Override @Override
public boolean addUser(String username, String email, String password){ public boolean addUser(String username, String email, String password) throws DataAccessException{
/* Adds new user to the database */ /* Adds new user to the database */
String query = "INSERT INTO users (username, email, password) VALUES (?, ?, ?);"; String query = "INSERT INTO users (username, email, password) VALUES (?, ?, ?);";
String query2= "INSERT INTO authorities (username, authority) VALUES (?,?);"; String query2= "INSERT INTO authorities (username, authority) VALUES (?,?);";
......
...@@ -16,10 +16,12 @@ ...@@ -16,10 +16,12 @@
<main> <main>
<!--CODE MODIFIED FROM: https://github.com/AsmrProg-YT/Modern-Login --> <!--CODE MODIFIED FROM: https://github.com/AsmrProg-YT/Modern-Login -->
<div class="container" id="container"> <div class="container" th:classappend="${status}" id="container">
<div class="form-container sign-up"> <div class="form-container sign-up">
<h1 class="form-title">Create Account</h1> <h1 class="form-title">Create Account</h1>
<form th:object="${user}" action="#" th:action="@{/login/register}" th:method="POST" onsubmit="return registerFormValidation()"> <form th:object="${user}" action="#" th:action="@{/login/register}" th:method="POST" onsubmit="return registerFormValidation()">
<h1>Create Account</h1>
<div th:if="${error.equals('User exists')}" class="alert alert-error">User already exist</div>
<label> <label>
<input class="input" th:field="*{name}" id="register-username" type="text" placeholder="Name"> <input class="input" th:field="*{name}" id="register-username" type="text" placeholder="Name">
</label> </label>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment