Skip to content

Commit

Permalink
feat(signup): add signup functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
teebow1e committed Jun 15, 2024
1 parent 83b2c32 commit deeda38
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 39 deletions.
13 changes: 7 additions & 6 deletions .config/accounts.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"users": [
{
"username": "a",
"password_hash": "$2a$10$t.g7znEK3FTRtqNZSHb4c.D.IdnUm8SBobTV/xjq6uvs7iB7qng/m"
}
]
"users" : [ {
"username" : "a",
"password_hash" : "$2a$10$t.g7znEK3FTRtqNZSHb4c.D.IdnUm8SBobTV/xjq6uvs7iB7qng/m"
}, {
"username" : "trung",
"password_hash" : "$2a$10$70uOzzH2wXJLvLgrNUDhh.GsDgHvWYohjGJrsffBMaaQyZu2OuwkK"
} ]
}
57 changes: 35 additions & 22 deletions src/main/java/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@

import javafx.event.ActionEvent;
import javafx.fxml.FXML;

import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.scene.control.*;
import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;
import java.util.logging.Level;

import ui.LoginForm;
import ui.SignUpForm;
import ui.WebLogManager;

import usermanagement.User;
import usermanagement.UserManagement;
import user.User;
import user.UserManagement;

import static utility.Utility.showAlert;

import java.io.File;
import java.util.List;
import java.util.logging.Logger;
import java.util.logging.Level;

public class Controller {
private Stage stage;
private List<User> userLists;
Expand All @@ -33,13 +31,14 @@ public class Controller {
@FXML
private TextField rePasswordField;

// CONSTANT VALUE HERE
private String dbFilePath = System.getProperty("user.dir")
+ File.separator
+ ".config"
+ File.separator
+ "accounts.json";

public void init(Stage stage) {
// CONSTANT VALUE HERE
String dbFilePath = System.getProperty("user.dir")
+ File.separator
+ ".config"
+ File.separator
+ "accounts.json";
File dbFile = new File(dbFilePath);
if (dbFile.exists()) {
userLists = UserManagement.readUserFile(dbFilePath);
Expand All @@ -64,15 +63,31 @@ private void returnToLogin(MouseEvent event) throws Exception{
loginForm.start(stage);
}
@FXML
private void handleSignupAccount(MouseEvent event) throws Exception{
private void handleSignupAccount(MouseEvent event) throws Exception {
String username = usernameField.getText();
String password = passwordField.getText();
String rePassword = rePasswordField.getText();

System.out.println("Username: " + username);
System.out.println("Password: " + password);
System.out.println("Re-entered Password: " + rePassword);
if (username.isEmpty()) {
showAlert("ERROR", "Username can not be empty");
returnToLogin(event);
return;
}

if (password.length() < 8) {
showAlert("ERROR", "Password must be longer than 8 characters.");
returnToLogin(event);
return;
}

if (!Objects.equals(password, rePassword)) {
showAlert("ERROR",
"Password and Re-enter password field must be the same."
);
returnToLogin(event);
return;
}
UserManagement.addUser(dbFilePath, username, password);
returnToLogin(event);
}

Expand All @@ -81,11 +96,9 @@ private void handleLoginAccount(MouseEvent event) throws Exception {
String username = usernameField.getText();
String password = passwordField.getText();

System.out.println("Username: " + username);
System.out.println("Password: " + password);
boolean authenticated = UserManagement.authenticateUser(userLists, username, password);
if (authenticated) {
logger.log(Level.INFO, "Login successful");
logger.log(Level.INFO, "Login successful with user " + username);
WebLogManager webLogManager = new WebLogManager();
webLogManager.start(stage);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package usermanagement;
package user;

public class User {
private final String username;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package usermanagement;
package user;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.mindrot.jbcrypt.BCrypt;

import java.io.*;
Expand Down Expand Up @@ -43,17 +45,19 @@ public static boolean authenticateUser(List<User> userList, String username, Str
return false;
}

public static void addUser(String fileName, User user) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true))) {
String hashedPassword = BCrypt.hashpw(user.getPasswordHash(), BCrypt.gensalt());
writer.write(user.getUsername() + "," + hashedPassword + "\n");
public static void addUser(String fileName, String username, String plaintextPassword) {
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode rootNode = objectMapper.readTree(new File(fileName));
ArrayNode usersNode = (ArrayNode) rootNode.path("users");
String hashedPassword = BCrypt.hashpw(plaintextPassword, BCrypt.gensalt());
ObjectNode newUserNode = objectMapper.createObjectNode();
newUserNode.put("username", username);
newUserNode.put("password_hash", hashedPassword);
usersNode.add(newUserNode);
objectMapper.writerWithDefaultPrettyPrinter().writeValue(new File(fileName), rootNode);
} catch (IOException e) {
logger.log(Level.SEVERE, "An error occurred while writing to the user file", e);
}
}

public static void main(String[] args) {
System.out.println(BCrypt.hashpw("a", BCrypt.gensalt()));
System.out.println(BCrypt.checkpw("a", "$2a$10$t.g7znEK3FTRtqNZSHb4c.D.IdnUm8SBobTV/xjq6uvs7iB7qng/m"));
}
}

0 comments on commit deeda38

Please sign in to comment.