-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfitnessGoalsController.java
More file actions
83 lines (76 loc) · 3.14 KB
/
fitnessGoalsController.java
File metadata and controls
83 lines (76 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.example.jproject;
import java.io.IOException;
import java.lang.*;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
import java.sql.*;
public class LoginController{
@FXML
private TextField usernameField;
@FXML
private PasswordField passwordField;
@FXML
private Button loginButton;
private static final String jdbcURL = "jdbc:postgresql://localhost:5432/postgres";
private static final String username = "postgres";
private static final String password = "shreya123";
public void handleLoginButtonAction(ActionEvent event) throws IOException {
String username1 = usernameField.getText();
String password = passwordField.getText();
if (authenticate(username1, password)) {
showAlert("Login Successful", "Welcome, " + username1 + "!");
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("fitnessTracker.fxml"));
Parent nextRoot = loader.load();
NextPageController nextPageController = loader.getController();
ObservableList<Logs> data = nextPageController.getDataFromDatabase(username1);
nextPageController.setDataInTable(data);
nextPageController.setUsernameFromLogin(username1);
Stage stage = (Stage) loginButton.getScene().getWindow();
stage.setTitle("Exercise logs");
Scene scene = new Scene(nextRoot);
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
} else {
showAlert("Login Failed", "Invalid username or password.");
}
}
private static Connection connectToDatabase() throws SQLException {
return DriverManager.getConnection(jdbcURL, username, password);
}
private static boolean authenticate(String username, String password) {
try (Connection connection = connectToDatabase();
PreparedStatement statement = connection.prepareStatement(
"SELECT username,user_id, password_hash FROM users WHERE username = ?")) {
statement.setString(1, username);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
String storedPassword = resultSet.getString("password_hash");
int userID = resultSet.getInt("user_id");
String userName= resultSet.getString("username");
if (storedPassword.equals(password)) {
return true;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
private void showAlert(String title, String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
}