|
| 1 | +package org.baeldung.event.listener; |
| 2 | + |
| 3 | +import java.util.UUID; |
| 4 | + |
| 5 | +import org.baeldung.event.OnRegistrationCompleteEvent; |
| 6 | +import org.baeldung.persistence.model.User; |
| 7 | +import org.baeldung.persistence.service.IUserService; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.context.ApplicationListener; |
| 10 | +import org.springframework.context.MessageSource; |
| 11 | +import org.springframework.mail.SimpleMailMessage; |
| 12 | +import org.springframework.mail.javamail.JavaMailSender; |
| 13 | +import org.springframework.stereotype.Component; |
| 14 | + |
| 15 | +@Component |
| 16 | +public class RegistrationListener implements ApplicationListener<OnRegistrationCompleteEvent> { |
| 17 | + @Autowired |
| 18 | + private IUserService service; |
| 19 | + |
| 20 | + @Autowired |
| 21 | + private MessageSource messages; |
| 22 | + |
| 23 | + @Autowired |
| 24 | + private JavaMailSender mailSender; |
| 25 | + |
| 26 | + @Override |
| 27 | + public void onApplicationEvent(OnRegistrationCompleteEvent event) { |
| 28 | + this.confirmRegistration(event); |
| 29 | + } |
| 30 | + |
| 31 | + private void confirmRegistration(OnRegistrationCompleteEvent event) { |
| 32 | + User user = event.getUser(); |
| 33 | + String token = UUID.randomUUID().toString(); |
| 34 | + service.createVerificationTokenForUser(user, token); |
| 35 | + |
| 36 | + String recipientAddress = user.getEmail(); |
| 37 | + String subject = "Registration Confirmation"; |
| 38 | + String confirmationUrl = event.getAppUrl() + "/regitrationConfirm.html?token=" + token; |
| 39 | + String message = messages.getMessage("message.regSucc", null, event.getLocale()); |
| 40 | + SimpleMailMessage email = new SimpleMailMessage(); |
| 41 | + email.setTo(recipientAddress); |
| 42 | + email.setSubject(subject); |
| 43 | + email.setText(message + " \r\n" + "http://localhost:8080" + confirmationUrl); |
| 44 | + mailSender.send(email); |
| 45 | + } |
| 46 | +} |
0 commit comments