This commit is contained in:
kkazuya
2026-09-17 16:02:58 +09:00
6 changed files with 166 additions and 1 deletions

View File

@@ -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);
}

View File

@@ -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;
}
}

View File

@@ -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";
}
}

View File

@@ -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;
}

View 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>

View File

@@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html lang="ja"xmlns:th="http://www.thymeleaf.org">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>新規会員登録確認</title> <title>新規会員登録確認</title>