login in extern loginController, RequestController cleanup, login right after register
This commit is contained in:
parent
0f7d248d40
commit
a41889b2cb
|
@ -1,17 +1,7 @@
|
||||||
package org.hso.ecommerce.app;
|
package org.hso.ecommerce.app;
|
||||||
|
|
||||||
import org.hso.ecommerce.entities.user.User;
|
|
||||||
import org.hso.ecommerce.repos.user.UserRepository;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import javax.servlet.http.HttpSession;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO clean up this class
|
* TODO clean up this class
|
||||||
|
@ -19,70 +9,13 @@ import java.util.Optional;
|
||||||
@Controller
|
@Controller
|
||||||
public class RequestController {
|
public class RequestController {
|
||||||
|
|
||||||
@Autowired
|
@GetMapping("/intern/customerOrders/")
|
||||||
private final UserRepository userRepository = null;
|
public String internCustomerOrder() {
|
||||||
|
return "intern/customerOrders/index";
|
||||||
static int notSoRandom = 0;
|
}
|
||||||
|
|
||||||
@GetMapping("/login")
|
|
||||||
public String login() {
|
|
||||||
return "login";
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/login")
|
|
||||||
public String loginPost(
|
|
||||||
HttpServletRequest request,
|
|
||||||
HttpServletResponse response,
|
|
||||||
@RequestParam("username") String username,
|
|
||||||
@RequestParam("password") String password,
|
|
||||||
HttpSession session
|
|
||||||
) {
|
|
||||||
String gto = (String) session.getAttribute("afterLogin");
|
|
||||||
|
|
||||||
Optional<User> user = userRepository.findByEmail(username);
|
|
||||||
if (!user.isPresent()) {
|
|
||||||
request.setAttribute("error", "Email Adresse falsch.");
|
|
||||||
response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
|
|
||||||
return "login";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!user.get().validatePassword(password)) {
|
|
||||||
request.setAttribute("error", "Passwort falsch.");
|
|
||||||
response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
|
|
||||||
return "login";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!user.get().isActive) {
|
|
||||||
request.setAttribute("error", "User ist deaktiviert.");
|
|
||||||
response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
|
|
||||||
return "login";
|
|
||||||
}
|
|
||||||
|
|
||||||
session.setAttribute("userId", user.get().getId());
|
|
||||||
|
|
||||||
if (gto != null && gto.startsWith("/")) {
|
|
||||||
return "redirect:" + gto;
|
|
||||||
} else {
|
|
||||||
return "redirect:/";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/logout")
|
|
||||||
public String logoutPost(HttpServletResponse response,
|
|
||||||
HttpSession session
|
|
||||||
) {
|
|
||||||
session.removeAttribute("userId");
|
|
||||||
return "redirect:/";
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/intern/customerOrders/")
|
|
||||||
public String internCustomerOrder() {
|
|
||||||
return "intern/customerOrders/index";
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/intern/customerOrders/{id}")
|
|
||||||
public String internCustomerOrdersId() {
|
|
||||||
return "intern/customerOrders/id";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@GetMapping("/intern/customerOrders/{id}")
|
||||||
|
public String internCustomerOrdersId() {
|
||||||
|
return "intern/customerOrders/id";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,69 @@
|
||||||
package org.hso.ecommerce.controller;
|
package org.hso.ecommerce.controller;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
|
|
||||||
|
import org.hso.ecommerce.entities.user.User;
|
||||||
|
import org.hso.ecommerce.repos.user.UserRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
//@RequestMapping("...")
|
@RequestMapping("/")
|
||||||
public class LoginController {
|
public class LoginController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private final UserRepository userRepository = null;
|
||||||
|
|
||||||
|
@GetMapping("login")
|
||||||
|
public String login() {
|
||||||
|
return "login";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("login")
|
||||||
|
public String loginPost(HttpServletRequest request, HttpServletResponse response,
|
||||||
|
@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) {
|
||||||
|
|
||||||
|
String gto = (String) session.getAttribute("afterLogin");
|
||||||
|
|
||||||
|
Optional<User> user = userRepository.findByEmail(username);
|
||||||
|
if (!user.isPresent()) {
|
||||||
|
request.setAttribute("error", "Email Adresse falsch.");
|
||||||
|
response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
|
||||||
|
return "login";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!user.get().validatePassword(password)) {
|
||||||
|
request.setAttribute("error", "Passwort falsch.");
|
||||||
|
response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
|
||||||
|
return "login";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!user.get().isActive) {
|
||||||
|
request.setAttribute("error", "User ist deaktiviert.");
|
||||||
|
response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
|
||||||
|
return "login";
|
||||||
|
}
|
||||||
|
|
||||||
|
session.setAttribute("userId", user.get().getId());
|
||||||
|
|
||||||
|
if (gto != null && gto.startsWith("/")) {
|
||||||
|
return "redirect:" + gto;
|
||||||
|
} else {
|
||||||
|
return "redirect:/";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("logout")
|
||||||
|
public String logoutPost(HttpServletResponse response, HttpSession session) {
|
||||||
|
session.removeAttribute("userId");
|
||||||
|
return "redirect:/";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,66 +16,67 @@ import javax.servlet.http.HttpSession;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class RegisterController {
|
public class RegisterController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private final UserRepository userRepository = null;
|
private final UserRepository userRepository = null;
|
||||||
|
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
public String registerPost(
|
public String registerPost(HttpServletRequest request, HttpServletResponse response,
|
||||||
HttpServletRequest request,
|
@RequestParam("username") String username, @RequestParam("password") String password,
|
||||||
HttpServletResponse response,
|
@RequestParam("password2") String password2, @RequestParam("salutation") String salutation,
|
||||||
@RequestParam("username") String username,
|
@RequestParam("name") String name, @RequestParam("address") String address,
|
||||||
@RequestParam("password") String password,
|
@RequestParam("type") String type, // TODO store
|
||||||
@RequestParam("password2") String password2,
|
@RequestParam("ad") String ad, // TODO store
|
||||||
@RequestParam("salutation") String salutation,
|
HttpSession session) {
|
||||||
@RequestParam("name") String name,
|
Optional<User> user = userRepository.findByEmail(username);
|
||||||
@RequestParam("address") String address,
|
if (user.isPresent()) {
|
||||||
@RequestParam("type") String type, //TODO store
|
request.setAttribute("error", "Email Adresse existiert bereits!");
|
||||||
@RequestParam("ad") String ad, //TODO store
|
response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
|
||||||
HttpSession session
|
return "register";
|
||||||
)
|
}
|
||||||
{
|
|
||||||
Optional<User> user = userRepository.findByEmail(username);
|
|
||||||
if (user.isPresent()) {
|
|
||||||
request.setAttribute("error", "Email Adresse existiert bereits!");
|
|
||||||
response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
|
|
||||||
return "register";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!password.equals(password2)){
|
if (!password.equals(password2)) {
|
||||||
request.setAttribute("error", "Passwörter sind nicht gleich");
|
request.setAttribute("error", "Passwörter sind nicht gleich");
|
||||||
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
||||||
return "register";
|
return "register";
|
||||||
}
|
}
|
||||||
|
|
||||||
//set values for new user
|
// set values for new user
|
||||||
User newUser = new User();
|
User newUser = new User();
|
||||||
newUser.email = username;
|
newUser.email = username;
|
||||||
newUser.setPassword(password);
|
newUser.setPassword(password);
|
||||||
newUser.email = username;
|
newUser.email = username;
|
||||||
newUser.isEmployee = false;
|
newUser.isEmployee = false;
|
||||||
newUser.salutation = salutation;
|
newUser.salutation = salutation;
|
||||||
newUser.defaultPayment = PaymentMethod.fromCreditCardNumber("");
|
newUser.defaultPayment = PaymentMethod.fromCreditCardNumber("");
|
||||||
|
|
||||||
newUser.isActive = true;
|
newUser.isActive = true;
|
||||||
newUser.created = new java.sql.Timestamp(System.currentTimeMillis());
|
newUser.created = new java.sql.Timestamp(System.currentTimeMillis());
|
||||||
|
|
||||||
Address newAddress = new Address();
|
Address newAddress = new Address();
|
||||||
newAddress.name = name;
|
newAddress.name = name;
|
||||||
newAddress.addressString = address;
|
newAddress.addressString = address;
|
||||||
newUser.defaultDeliveryAddress = newAddress;
|
newUser.defaultDeliveryAddress = newAddress;
|
||||||
|
|
||||||
userRepository.save(newUser); // save newUser
|
userRepository.save(newUser); // save newUser
|
||||||
|
|
||||||
user = userRepository.findByEmail(username);
|
user = userRepository.findByEmail(username);
|
||||||
session.setAttribute("userId", user.get().getId());
|
session.setAttribute("userId", user.get().getId());
|
||||||
|
|
||||||
return "redirect:/";
|
String gto = (String) session.getAttribute("afterLogin");
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/register")
|
//login after register
|
||||||
public String register() {
|
if (gto != null && gto.startsWith("/")) {
|
||||||
return "register";
|
return "redirect:" + gto;
|
||||||
}
|
} else {
|
||||||
|
return "redirect:/";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/register")
|
||||||
|
public String register() {
|
||||||
|
return "register";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue