-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update notification service and add signup confirmation process
- Loading branch information
1 parent
70be4c0
commit f232253
Showing
12 changed files
with
206 additions
and
61 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
68 changes: 68 additions & 0 deletions
68
backend/src/main/java/com/morris/quizly/controllers/SubscriptionController.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.morris.quizly.controllers; | ||
|
||
import com.morris.quizly.models.security.UserDetails; | ||
import com.morris.quizly.services.QuizlyUserDetailsService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/subscriptions") | ||
public class SubscriptionController { | ||
|
||
private final QuizlyUserDetailsService userService; | ||
|
||
@Autowired | ||
public SubscriptionController(QuizlyUserDetailsService userService) { | ||
this.userService = userService; | ||
} | ||
|
||
@GetMapping("/confirm-signup") | ||
public ResponseEntity<String> confirmSignup(@RequestParam("token") String token) { | ||
UserDetails user = userService.findBySignupToken(token); | ||
if (null == user) { | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid token"); | ||
} | ||
userService.updateEnabledStatus(user, true); | ||
|
||
String htmlResponse = "<html>" + | ||
"<head>" + | ||
"<style>" + | ||
"body { font-family: Arial, sans-serif; text-align: center; padding: 20px; }" + | ||
"h2 { color: #333; }" + | ||
".logo { margin: 20px auto; }" + | ||
".container { max-width: 600px; margin: 0 auto; text-align: center; }" + | ||
".button { background-color: #000; color: #fff; padding: 10px 20px; text-decoration: none; display: inline-block; margin-top: 20px; }" + | ||
"</style>" + | ||
"</head>" + | ||
"<body>" + | ||
"<div class='container'>" + | ||
"<img class='logo' src='/images/quizly-logo.png' alt='Quizly Logo'>" + | ||
"<h2>Thanks for confirming signup with Quizly, " + user.getFirstName() + "!</h2>" + | ||
"</div>" + | ||
"<script>" + | ||
"window.onload = function() {" + | ||
" var newTab = window.open('', '_blank');" + | ||
" newTab.document.write(\"<html><head><style>" + | ||
"body { font-family: Arial, sans-serif; text-align: center; padding: 20px; }" + | ||
"h1 { color: #333; }" + | ||
".logo { margin: 20px auto; }" + | ||
".container { max-width: 600px; margin: 0 auto; text-align: center; }" + | ||
"</style></head><body><div class='container'>" + | ||
"<h1>Subscription confirmed successfully!</h1>" + | ||
"</div></body></html>\");" + | ||
"};" + | ||
"</script>" + | ||
"</body>" + | ||
"</html>"; | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.TEXT_HTML); | ||
return ResponseEntity.ok().headers(headers).body(htmlResponse); | ||
} | ||
} |
This file contains 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
This file contains 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
8 changes: 8 additions & 0 deletions
8
backend/src/main/java/com/morris/quizly/services/EmailService.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
package com.morris.quizly.services; | ||
|
||
public interface EmailService { | ||
|
||
/** | ||
* Sends email. | ||
* | ||
* @param to {@link String} email recipient | ||
* @param subject {@link String} Subject | ||
* @param body {@link String} email content | ||
*/ | ||
void sendEmail(String to, String subject, String body); | ||
} |
This file contains 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
This file contains 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
77 changes: 45 additions & 32 deletions
77
backend/src/main/java/com/morris/quizly/services/impl/EmailServiceImpl.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,63 @@ | ||
package com.morris.quizly.services.impl; | ||
|
||
import com.morris.quizly.services.EmailService; | ||
import com.sendgrid.Method; | ||
import com.sendgrid.Request; | ||
import com.sendgrid.Response; | ||
import com.sendgrid.SendGrid; | ||
import com.sendgrid.helpers.mail.Mail; | ||
import com.sendgrid.helpers.mail.objects.ClickTrackingSetting; | ||
import com.sendgrid.helpers.mail.objects.Content; | ||
import com.sendgrid.helpers.mail.objects.Email; | ||
import com.sendgrid.helpers.mail.objects.TrackingSettings; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.mail.*; | ||
import javax.mail.internet.InternetAddress; | ||
import javax.mail.internet.MimeMessage; | ||
import java.util.Properties; | ||
import java.io.IOException; | ||
|
||
@Service | ||
public class EmailServiceImpl implements EmailService { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(EmailServiceImpl.class); | ||
|
||
@Value("$gmail.username") | ||
private String admin; | ||
|
||
@Value("${gmail.password}") | ||
private String password; | ||
@Value("${sendgrid.signup.confirmation.api}") | ||
private String key; | ||
|
||
@Override | ||
public void sendEmail(String to, String subject, String body) { | ||
Properties props = new Properties(); | ||
props.put("mail.smtp.auth", "true"); | ||
props.put("mail.smtp.starttls.enable", "true"); | ||
props.put("mail.smtp.host", "smtp.gmail.com"); | ||
props.put("mail.smtp.port", "587"); | ||
|
||
Session session = Session.getInstance(props, | ||
new javax.mail.Authenticator() { | ||
protected PasswordAuthentication getPasswordAuthentication() { | ||
return new PasswordAuthentication(admin, password); | ||
} | ||
}); | ||
Mail mail = getMail(to, subject, body); | ||
|
||
SendGrid sg = new SendGrid(key); | ||
Request request = new Request(); | ||
try { | ||
Message message = new MimeMessage(session); | ||
message.setFrom(new InternetAddress(admin)); | ||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); | ||
message.setSubject(subject); | ||
message.setText(body); | ||
|
||
Transport.send(message); | ||
|
||
System.out.println("Email sent successfully"); | ||
} catch (MessagingException e) { | ||
throw new RuntimeException(e); | ||
request.setMethod(Method.POST); | ||
request.setEndpoint("mail/send"); | ||
request.setBody(mail.build()); | ||
Response response = sg.api(request); | ||
System.out.println(response.getStatusCode()); | ||
System.out.println(response.getBody()); | ||
System.out.println(response.getHeaders()); | ||
} catch (IOException e) { | ||
LOGGER.error("Error sending email: {}", e.getMessage()); | ||
} | ||
} | ||
|
||
@NotNull | ||
private Mail getMail(String to, String subject, String body) { | ||
Email from = new Email("ai.quizly@gmail.com"); | ||
Email toEmail = new Email(to); | ||
Content content = new Content("text/plain", body); | ||
Mail mail = new Mail(from, subject, toEmail, content); | ||
|
||
// Disable click tracking | ||
TrackingSettings trackingSettings = new TrackingSettings(); | ||
ClickTrackingSetting clickTrackingSetting = new ClickTrackingSetting(); | ||
clickTrackingSetting.setEnable(false); | ||
clickTrackingSetting.setEnableText(false); | ||
trackingSettings.setClickTrackingSetting(clickTrackingSetting); | ||
mail.setTrackingSettings(trackingSettings); | ||
return mail; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.