login : login
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package hello.login.domain.login;
|
||||
|
||||
import hello.login.domain.member.Member;
|
||||
import hello.login.domain.member.MemberRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class LoginService {
|
||||
|
||||
private final MemberRepository memberRepository;
|
||||
|
||||
public Member login(String loginId, String password) {
|
||||
return memberRepository.findByLoginId(loginId)
|
||||
.filter(m -> m.getPassword().equals(password))
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package hello.login.web.login;
|
||||
|
||||
import hello.login.domain.login.LoginService;
|
||||
import hello.login.domain.member.Member;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
@Controller
|
||||
@RequiredArgsConstructor
|
||||
public class LoginController {
|
||||
|
||||
private final LoginService loginService;
|
||||
|
||||
@GetMapping("/login")
|
||||
public String loginForm(@ModelAttribute("loginForm") LoginForm form) {
|
||||
return "login/loginForm";
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public String login(@Valid @ModelAttribute LoginForm form, BindingResult bindingResult) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
return "login/loginForm";
|
||||
}
|
||||
|
||||
Member loginMember = loginService.login(form.getLoginId(), form.getPassword());
|
||||
|
||||
if (loginMember == null) {
|
||||
bindingResult.reject("loginFail", "아이디 또는 비밀번호가 맞지 않습니다.");
|
||||
return "login/loginForm";
|
||||
}
|
||||
|
||||
// 로그인 성공 처리 TODO
|
||||
|
||||
return "redirect:/";
|
||||
}
|
||||
}
|
||||
16
login/src/main/java/hello/login/web/login/LoginForm.java
Normal file
16
login/src/main/java/hello/login/web/login/LoginForm.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package hello.login.web.login;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
@Data
|
||||
public class LoginForm {
|
||||
|
||||
@NotEmpty
|
||||
private String loginId;
|
||||
|
||||
@NotBlank
|
||||
private String password;
|
||||
}
|
||||
55
login/src/main/resources/templates/login/loginForm.html
Normal file
55
login/src/main/resources/templates/login/loginForm.html
Normal file
@@ -0,0 +1,55 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<link th:href="@{/css/bootstrap.min.css}"
|
||||
href="../css/bootstrap.min.css" rel="stylesheet">
|
||||
<style>
|
||||
.container {
|
||||
max-width: 560px;
|
||||
}
|
||||
.field-error {
|
||||
border-color: #dc3545;
|
||||
color: #dc3545;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="py-5 text-center">
|
||||
<h2>로그인</h2>
|
||||
</div>
|
||||
<form action="item.html" th:action th:object="${loginForm}" method="post">
|
||||
<div th:if="${#fields.hasGlobalErrors()}">
|
||||
<p class="field-error" th:each="err : ${#fields.globalErrors()}"
|
||||
th:text="${err}">전체 오류 메시지</p>
|
||||
</div>
|
||||
<div>
|
||||
<label for="loginId">로그인 ID</label>
|
||||
<input type="text" id="loginId" th:field="*{loginId}" class="form-control"
|
||||
th:errorclass="field-error">
|
||||
<div class="field-error" th:errors="*{loginId}" />
|
||||
</div>
|
||||
<div>
|
||||
<label for="password">비밀번호</label>
|
||||
<input type="password" id="password" th:field="*{password}" class="form-control"
|
||||
th:errorclass="field-error">
|
||||
<div class="field-error" th:errors="*{password}" />
|
||||
</div>
|
||||
<hr class="my-4">
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<button class="w-100 btn btn-primary btn-lg" type="submit">
|
||||
로그인</button>
|
||||
</div>
|
||||
<div class="col">
|
||||
<button class="w-100 btn btn-secondary btn-lg"
|
||||
onclick="location.href='items.html'"
|
||||
th:onclick="|location.href='@{/}'|"
|
||||
type="button">취소</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div> <!-- /container -->
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user