Skip to content

Commit

Permalink
#100 with ray
Browse files Browse the repository at this point in the history
  • Loading branch information
dt0602 committed Dec 4, 2023
1 parent d6958c7 commit 8030a82
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/main/java/client/view/LoginPopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.event.ActionEvent;
Expand All @@ -15,6 +16,9 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

import javax.swing.JCheckBox;

import com.sun.net.httpserver.HttpExchange;

public class LoginPopup extends Stage {
Expand All @@ -26,6 +30,8 @@ public class LoginPopup extends Stage {
private Label usernameLabel;
private Label passwordLabel;
private boolean loggedIn = false;
private CheckBox autoLoginCheckbox;
private boolean autoLogin = false;

public LoginPopup() {

Expand All @@ -47,6 +53,13 @@ public LoginPopup() {
username.setStyle("-fx-alignment: center; -fx-font-weight: bold;");
password.setStyle("-fx-alignment: center; -fx-font-weight: bold;");

autoLoginCheckbox = new CheckBox("Automatic Login");
autoLoginCheckbox.setStyle("-fx-font-family: 'Lucida Bright';");
autoLoginCheckbox.setOnAction(event -> {
autoLogin = autoLoginCheckbox.isSelected();
});


loginAccountButton = new Button("Login");
loginAccountButton.setStyle(
"-fx-background-color: #bdd9bd; -fx-font-weight: bold; -fx-font-size: 13; -fx-font-family: 'Lucida Bright';");
Expand All @@ -69,7 +82,7 @@ public void setLoginAccountButtonAction(EventHandler<ActionEvent> eventHandler)
}

public void loginAccount(String username, String password) {
loggedIn = server.Login.loginAccount(username, password);
loggedIn = server.Login.loginAccount(username, password, autoLogin);
if (loggedIn) {
// If logged in successfully, close the login popup
this.close();
Expand Down Expand Up @@ -107,7 +120,7 @@ public void display() {
layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.setStyle("-fx-background-color: #93c994;");
layout.getChildren().addAll(usernameLabel, username, passwordLabel, password, buttonBox);
layout.getChildren().addAll(usernameLabel, username, passwordLabel, password, autoLoginCheckbox, buttonBox);

Scene scene = new Scene(layout, 400, 500);
setScene(scene);
Expand All @@ -125,5 +138,4 @@ public TextField getPassword() {
public Button getLoginAccountButton() {
return this.loginAccountButton;
}

}
30 changes: 29 additions & 1 deletion src/main/java/server/Login.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

import static com.mongodb.client.model.Filters.*;

import java.util.prefs.Preferences;

public class Login {
private static String userID;

public static boolean loginAccount(String username, String password) {

public static boolean loginAccount(String username, String password, boolean autoLogin) {
String uri = "mongodb+srv://aditijain:[email protected]/?retryWrites=true&w=majority";
MongoClient mongoClient = MongoClients.create(uri);
MongoDatabase PantryPalDB = mongoClient.getDatabase("PantryPal");
Expand All @@ -23,6 +26,11 @@ public static boolean loginAccount(String username, String password) {
System.out.println("Success");
userID = user.getObjectId("_id").toString();
System.out.println("User ID: " + userID);

if (autoLogin){
storeCredentials(username, password);
}

return true;
}
return false;
Expand All @@ -31,4 +39,24 @@ public static boolean loginAccount(String username, String password) {
public static String getID() {
return userID;
}


private static void storeCredentials(String username, String password) {
// Use Preferences API to securely store credentials
Preferences prefs = Preferences.userNodeForPackage(Login.class);
prefs.put("username", username);
prefs.put("password", password);
}

public static boolean attemptAutoLogin() {
// Attempt automatic login using stored credentials
Preferences prefs = Preferences.userNodeForPackage(Login.class);
String storedUsername = prefs.get("username", null);
String storedPassword = prefs.get("password", null);

boolean autoLogin = storedUsername != null && storedPassword != null;

return loginAccount(storedUsername, storedPassword, autoLogin);
}

}

0 comments on commit 8030a82

Please sign in to comment.