[ADD]パスワード再設定の作成
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package com.example.demo.app.domain.service;
|
||||
|
||||
import com.example.demo.app.presentation.form.PasswordResetForm;
|
||||
|
||||
public interface PasswordResetService {
|
||||
|
||||
boolean resetPassword(Long userId, PasswordResetForm passwordResetForm);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.example.demo.app.domain.service.impl;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.example.demo.app.domain.service.PasswordResetService;
|
||||
import com.example.demo.app.infra.entity.UserEntity;
|
||||
import com.example.demo.app.infra.repository.UserRepository;
|
||||
import com.example.demo.app.presentation.form.PasswordResetForm;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class PasswordResetServiceImpl implements PasswordResetService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Override
|
||||
public boolean resetPassword(
|
||||
Long userId,
|
||||
PasswordResetForm passwordResetForm) {
|
||||
|
||||
if (!passwordResetForm.getPassword()
|
||||
.equals(passwordResetForm.getPasswordConfirm())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
UserEntity userEntity =
|
||||
userRepository.findById(userId).orElse(null);
|
||||
|
||||
if (userEntity == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
userEntity.setPassword(passwordResetForm.getPassword());
|
||||
|
||||
userRepository.save(userEntity);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.example.demo.app.presentation.controller;
|
||||
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
||||
import com.example.demo.app.domain.service.PasswordResetService;
|
||||
import com.example.demo.app.presentation.form.PasswordResetForm;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Controller
|
||||
@RequiredArgsConstructor
|
||||
public class PasswordResetController {
|
||||
|
||||
private final PasswordResetService passwordResetService;
|
||||
|
||||
@GetMapping("/password_reset")
|
||||
public String showPasswordReset(Model model) {
|
||||
|
||||
model.addAttribute(
|
||||
"passwordResetForm",
|
||||
new PasswordResetForm());
|
||||
|
||||
return "password_reset";
|
||||
}
|
||||
|
||||
@PostMapping("/password_reset")
|
||||
public String resetPassword(
|
||||
PasswordResetForm passwordResetForm,
|
||||
Model model,
|
||||
HttpSession session) {
|
||||
|
||||
Long userId =
|
||||
(Long) session.getAttribute("passwordResetUserId");
|
||||
|
||||
if (userId == null) {
|
||||
return "redirect:/login";
|
||||
}
|
||||
|
||||
boolean isReset =
|
||||
passwordResetService.resetPassword(
|
||||
userId,
|
||||
passwordResetForm);
|
||||
|
||||
if (!isReset) {
|
||||
model.addAttribute(
|
||||
"errorMessage",
|
||||
"パスワードが一致していません。");
|
||||
|
||||
return "password_reset";
|
||||
}
|
||||
|
||||
session.removeAttribute("passwordResetUserId");
|
||||
|
||||
return "redirect:/login";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.demo.app.presentation.form;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class PasswordResetForm {
|
||||
private String password;
|
||||
private String passwordConfirm;
|
||||
}
|
||||
42
src/main/resources/templates/password_reset.html
Normal file
42
src/main/resources/templates/password_reset.html
Normal file
@@ -0,0 +1,42 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ja"xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>パスワード再設定</title>
|
||||
</head>
|
||||
<body>
|
||||
<header></header>
|
||||
|
||||
<main>
|
||||
<h1>パスワード再設定</h1>
|
||||
|
||||
<form th:action="@{/password_reset}" th:obiect="${passwordResteForm"} method="post">
|
||||
<div>
|
||||
<label for="password">新しいパスワード</label>
|
||||
<input type="password"
|
||||
id="password"
|
||||
th:field="*{password}"
|
||||
minlength="8"
|
||||
maxlength="255"
|
||||
required>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="passwordConfirm">パスワード確認</label>
|
||||
<input type="password"
|
||||
id="passwordConfirm"
|
||||
th:field="*{passwordConfirm}"
|
||||
minlength="8"
|
||||
maxlength="255"
|
||||
required>
|
||||
</div>
|
||||
|
||||
<button type="submit">再設定</button>
|
||||
</form>
|
||||
|
||||
<div th:if="${errorMessage}">
|
||||
<p th:text="${errorMessage}"></p>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,5 +1,5 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<html lang="ja"xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>新規会員登録確認</title>
|
||||
|
||||
Reference in New Issue
Block a user