Created
April 9, 2022 14:58
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
@RequiredArgsConstructor | |
public class ExampleController { | |
private final ExampleService exampleService; | |
private final AccountRepository accountRepository; | |
@PostMapping("/email-login") | |
public String sendEmailLoginLink(String email | |
, Model model | |
, RedirectAttributes attributes) { | |
Account account = accountRepository.findByEmail(email); | |
if (account == null) { | |
model.addAttribute("error", "유효한 이메일 주소가 아닙니다."); | |
return "account/email-login"; | |
} | |
if (!account.canSendConfirmEmail()) { | |
model.addAttribute("error", "이메일 로그인은 1시간 뒤에 사용할 수 있습니다."); | |
return "account/email-login"; | |
} | |
exampleService.sendLoginLink(account); | |
attributes.addFlashAttribute("message", "이메일 인증 메일을 발송했습니다."); | |
return "redirect:/email-login"; | |
} | |
} | |
@Service | |
@Slf4j | |
@Transactional | |
@RequiredArgsConstructor | |
public class ExampleService { | |
private static final String EXAMPLE_LINK_TEMPLATE = "템플릿 경로"; | |
private final TemplateEngine templateEngine; | |
private final EmailService emailService; | |
public void sendLoginLink(Account account) { | |
account.generateEmailCheckToken(); | |
Context context = getContext(account); | |
String message = templateEngine.process(EXAMPLE_LINK_TEMPLATE, context); | |
EmailMessage emailMessage = EmailMessage.builder() | |
.to(account.getEmail()) | |
.subject("이메일 제목") | |
.message(message) | |
.build(); | |
emailService.send(emailMessage); | |
} | |
private Context getContext(Account account) { | |
Context context = new Context(); | |
context.setVariable("name", account.getName()); | |
context.setVariable("message", "메일 메시지"); | |
return context; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment