Skip to content

Commit 45271e7

Browse files
committed
Implemented Email verification
1 parent 2f00f3a commit 45271e7

11 files changed

+202
-53
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ repositories {
88

99
dependencies {
1010
implementation 'javax.mail:javax.mail-api:1.6.2'
11+
implementation 'com.sun.mail:javax.mail:1.6.2'
1112
implementation 'junit:junit:4.13.1'
1213
testImplementation('org.junit.jupiter:junit-jupiter:5.6.0')
1314
}

src/main/java/Controllers/UserRegistrationUseCase.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/main/java/Controllers/IRetrieveList.java renamed to src/main/java/ControllersPresentersGateways/IRetrieveList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package Controllers;
1+
package ControllersPresentersGateways;
22
import Entities.User;
33
import java.util.List;
44

src/main/java/Controllers/UserExists.java renamed to src/main/java/ControllersPresentersGateways/UserExists.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package Controllers;
1+
package ControllersPresentersGateways;
22

33
public interface UserExists {
44
boolean UserExists(String username, String password);

src/main/java/UI/UserRegistrationController.java renamed to src/main/java/ControllersPresentersGateways/UserRegistrationController.java

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
1-
package UI;
1+
package ControllersPresentersGateways;
22

3-
import Controllers.UserRegistrationUseCase;
3+
import UseCase.UserCreator;
44
import UseCase.UserVerifier;
55
import UseCase.verificationMethodFactory;
66

77
import javax.swing.*;
88
import java.awt.event.ActionEvent;
99
import java.awt.event.ActionListener;
1010
import java.util.Random;
11-
public class UserRegistrationController implements UserVerifier, ActionListener, UserRegistrationUseCase {
11+
public class UserRegistrationController implements UserVerifier, ActionListener, UserRegistrator {
1212
private final String username;
1313
private final String password;
1414
private final String email;
15-
private UserDatabase database;
15+
private String preference;
16+
private boolean userExists = false;
17+
private UserCreator database;
1618
Random random;
1719
private final int code;
1820
private JTextField verificationCodeText;
1921
private JLabel success;
2022

21-
public UserRegistrationController(int code, String Username, String Password, String email, UserDatabase database){
23+
/*public UserRegistrationController(int code, String Username, String Password, String email, UserDatabase database){
2224
this.code = code;
2325
this.username = Username;
2426
this.password = Password;
2527
this.email = email;
2628
this.database = database;
29+
}*/
30+
31+
public UserRegistrationController(UserRegistrationGateway properties){
32+
this.username = properties.getUsername();
33+
this.password = properties.getPassword();
34+
this.email = properties.getEmail();
35+
this.userExists = properties.isUserExists();
36+
this.code = properties.getCode();
37+
this.database = properties.getDatabase();
2738
}
2839

2940
//Asks for the verification code from the user, and matches it with this.code to potentially verify the user
3041
public void verify(String email){
3142
JFrame verificationFrame = new JFrame();
3243
verificationFrame.setSize(400, 200);
33-
verificationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
44+
verificationFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
3445
JPanel verificationPanel = new JPanel();
3546
verificationFrame.add(verificationPanel);
3647

@@ -59,18 +70,39 @@ public void verify(String email){
5970
}
6071

6172
public void registerUser() {
62-
if(database.UserExists(this.username, this.email)){
63-
System.out.println("A user with this username or email already exists");
73+
if(this.userExists){
74+
System.out.println("An account with this username or email already exists");
75+
accountExistsMessage();
6476
}else{
6577
this.verify(email);
6678
}
6779
}
6880

69-
//For testing purposes
70-
/*public static void main(String[] args){
71-
UI.UserVerificationUI ver = new UI.UserVerificationUI(389);
72-
ver.verify("abc");
73-
}*/
81+
public static void accountExistsMessage(){
82+
JFrame accountExistsFrame = new JFrame();
83+
accountExistsFrame.setSize(400, 100);
84+
accountExistsFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
85+
JPanel accountExistsPanel = new JPanel();
86+
accountExistsPanel.setLayout(null);
87+
accountExistsFrame.add(accountExistsPanel);
88+
JLabel errorMessage = new JLabel("An account with this username or email already exists");
89+
errorMessage.setBounds(10,20, 350, 20);
90+
accountExistsPanel.add(errorMessage);
91+
accountExistsFrame.setVisible(true);
92+
}
93+
94+
public static void verificationSuccessMessage(){
95+
JFrame verificationSuccessFrame = new JFrame();
96+
verificationSuccessFrame.setSize(400, 100);
97+
verificationSuccessFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
98+
JPanel verificationSuccessPanel = new JPanel();
99+
verificationSuccessPanel.setLayout(null);
100+
verificationSuccessFrame.add(verificationSuccessPanel);
101+
JLabel errorMessage = new JLabel("Could not verify please try again");
102+
errorMessage.setBounds(10,20, 350, 20);
103+
verificationSuccessPanel.add(errorMessage);
104+
verificationSuccessFrame.setVisible(true);
105+
}
74106

75107
@Override
76108
public void actionPerformed(ActionEvent e) {
@@ -79,7 +111,7 @@ public void actionPerformed(ActionEvent e) {
79111
database.createUser(this.username, this.password, this.email, "Basic");
80112
System.out.println("Verification successful");
81113
}else{
82-
System.out.println("Could not verify please try again");
114+
verificationSuccessMessage();
83115
}
84116
}
85117
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package ControllersPresentersGateways;
2+
3+
import UseCase.UserCreator;
4+
5+
public class UserRegistrationGateway {
6+
private String username;
7+
private String password;
8+
private String email;
9+
private boolean userExists = false;
10+
private int code;
11+
private UserCreator database;
12+
13+
private String preference;
14+
15+
public void setUsername(String username){
16+
this.username = username;
17+
}
18+
public String getUsername(){
19+
return this.username;
20+
}
21+
22+
public void setPassword(String password){
23+
this.password = password;
24+
}
25+
public String getPassword(){
26+
return this.password;
27+
}
28+
29+
public void setEmail(String email){
30+
this.email = email;
31+
}
32+
public String getEmail(){
33+
return this.email;
34+
}
35+
36+
public void setUserExists(boolean userExists) {
37+
this.userExists = userExists;
38+
}
39+
40+
public boolean isUserExists() {
41+
return userExists;
42+
}
43+
44+
public int getCode() {
45+
return code;
46+
}
47+
public void setCode(int code){
48+
this.code = code;
49+
}
50+
51+
public void setDatabase(UserCreator database) {
52+
this.database = database;
53+
}
54+
55+
public UserCreator getDatabase(){
56+
return this.database;
57+
}
58+
59+
public void setPreference(String preference) {
60+
this.preference = preference;
61+
}
62+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ControllersPresentersGateways;
2+
3+
public interface UserRegistrator {
4+
void registerUser();
5+
}

src/main/java/UI/UserDatabase.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package UI;
22

3-
import Controllers.IRetrieveList;
4-
import Controllers.UserExists;
3+
import ControllersPresentersGateways.IRetrieveList;
4+
import ControllersPresentersGateways.UserExists;
55
import Entities.User;
66
import Entities.UserFactory;
77
import UseCase.UserCreator;
@@ -78,14 +78,6 @@ public List<User> getList() {
7878
ObjectInputStream in = new ObjectInputStream(fileIn)) {
7979

8080
users = (ArrayList<User>) in.readObject();
81-
/*
82-
while(true){
83-
try{
84-
Entities.User user = (Entities.User) in.readObject();
85-
users.add(user);}
86-
catch(EOFException e){
87-
break;
88-
}*/
8981
return users;
9082
}catch(EOFException e){
9183
return users;

src/main/java/UI/UserRegistrationUI.java

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package UI;
22

3+
import ControllersPresentersGateways.UserRegistrationController;
4+
import ControllersPresentersGateways.UserRegistrationGateway;
35
import javax.swing.*;
46
import java.awt.event.ActionEvent;
57
import java.awt.event.ActionListener;
68
import java.io.File;
9+
import java.util.Random;
710

811
public class UserRegistrationUI implements ActionListener {
912
private final UserDatabase database;
@@ -12,16 +15,16 @@ public class UserRegistrationUI implements ActionListener {
1215
private JTextField passwordText;
1316
private JTextField emailText;
1417
private JButton register;
15-
18+
private static JButton phoneVerify = new JButton("Phone");
19+
private static JButton emailVerify = new JButton("Email");
1620
private final int code;
1721

1822
public UserRegistrationUI(UserDatabase database) {
1923
this.database = database;
2024
/*TODO: For now the code is 389 for testing purposes, but once UI.UserVerificationUI.sendVerificationCode() is
2125
implemented this will be a random integer.
2226
*/
23-
/*code = new Random().nextInt(1244254);*/
24-
code = 389;
27+
code = new Random().nextInt(1244254);
2528
}
2629
void GetUserCredentials(){
2730
//Front end related objects
@@ -72,22 +75,27 @@ void GetUserCredentials(){
7275

7376
registerFrame.setVisible(true);
7477
}
75-
/*@Override*/
76-
/*public void registerUser(String username, String password, String email) {
77-
if(database.UserExists(username, email)){
78-
registrationSuccess.setText("The username or password is already in use, please try again");
79-
}else{
80-
registrationSuccess.setText("Please verify to create your account");
81-
UserVerificationUI verifyUser = new UserVerificationUI(code, email);
82-
if(verifyUser.verify(email)){
83-
database.createUser(username, password, email, "Basic");
84-
registrationSuccess.setText("Your account is now created");
85-
}else{
86-
registrationSuccess.setText("You could not be verified, please try again");
87-
};
88-
}
89-
}*/
90-
//For Testing purposes
78+
79+
public void getPreferredDeliveryMethod(){
80+
JFrame preference = new JFrame();
81+
preference.setSize(400, 200);
82+
JPanel preferancePanel = new JPanel();
83+
preference.add(preferancePanel);
84+
85+
JLabel message = new JLabel("Send verification code via:");
86+
message.setBounds(30, 120, 300, 20);
87+
preferancePanel.add(message);
88+
89+
emailVerify.setBounds(30, 150, 140, 25);
90+
emailVerify.addActionListener(this);
91+
phoneVerify.setBounds(150, 150, 140, 25);
92+
phoneVerify.addActionListener(this);
93+
preferancePanel.add(emailVerify);
94+
preferancePanel.add(phoneVerify);
95+
preference.setVisible(true);
96+
97+
}
98+
9199
public static void main(String[] args){
92100
UserDatabase testDB = new UserDatabase(new File("Test5"));
93101
System.out.println(testDB.UserExists("RandomUser", "[email protected]"));
@@ -102,7 +110,20 @@ public void actionPerformed(ActionEvent e) {
102110
String username = usernameText.getText();
103111
String password = passwordText.getText();
104112
String email = emailText.getText();
105-
UserRegistrationController verifyUser = new UserRegistrationController(code, username, password, email, database);
106-
verifyUser.registerUser();
113+
114+
UserRegistrationGateway properties = new UserRegistrationGateway();
115+
properties.setUsername(username);
116+
properties.setPassword(password);
117+
properties.setEmail(email);
118+
properties.setUserExists(database.UserExists(username, email));
119+
properties.setCode(code);
120+
properties.setDatabase(this.database);
121+
getPreferredDeliveryMethod();
122+
//Not an error below, we just have not implemented sending code via phone yet.
123+
if(e.getSource() == emailVerify || e.getSource() == phoneVerify){
124+
properties.setPreference("Email");
125+
UserRegistrationController verifyUser = new UserRegistrationController(properties);
126+
verifyUser.registerUser();
127+
}
107128
}
108129
}
Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
11
package UseCase;
2+
import java.util.Properties;
3+
import javax.mail.*;
4+
import javax.mail.internet.InternetAddress;
5+
import javax.mail.internet.MimeMessage;
26

3-
import javax.mail.Session;
47
public class EmailDelivery implements ISendVerificationCode {
58
public void sendVerificationCode(String email, int code){
69
/*TODO: When this is implemented, the verification code will be sent to the email specified by String email*/
710
System.out.println("Verification code sent to " + email);
811

12+
//email address we will send the code to
13+
String to = email;
14+
15+
//email address we are sending from
16+
String from = "[email protected]";
17+
18+
//Get System properties
19+
Properties properties = System.getProperties();
20+
21+
//Setting up the mail server
22+
properties.put("mail.smtp.host", "smtp.gmail.com");
23+
properties.put("mail.smtp.port", "465");
24+
properties.put("mail.smtp.ssl.enable", "true");
25+
properties.put("mail.smtp.auth", "true");
26+
27+
Session session = Session.getInstance(properties, new javax.mail.Authenticator(){
28+
@Override
29+
protected PasswordAuthentication getPasswordAuthentication() {
30+
return new PasswordAuthentication("[email protected]", "gker cnno jcxv psou");
31+
}
32+
});
33+
34+
session.setDebug(true);
35+
36+
try{
37+
MimeMessage message = new MimeMessage(session);
38+
message.setFrom(from);
39+
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
40+
message.setSubject("Verification Code for registration");
41+
message.setText("The verification code for your account is " + code + ". If this was not you, you can safely " +
42+
"ignore this email");
43+
Transport.send(message);
44+
System.out.println("success");
45+
} catch (MessagingException e) {
46+
throw new RuntimeException(e);
47+
}
48+
949
}
1050
}

0 commit comments

Comments
 (0)