Update authentication to use REST API
This commit is contained in:
@@ -3,11 +3,13 @@ package org.example.petshopdesktop.auth;
|
|||||||
public class UserSession {
|
public class UserSession {
|
||||||
private static UserSession instance;
|
private static UserSession instance;
|
||||||
|
|
||||||
private Integer userId;
|
private Long userId;
|
||||||
private Integer employeeId;
|
private Long employeeId;
|
||||||
private String username;
|
private String username;
|
||||||
private String employeeName;
|
private String employeeName;
|
||||||
private Role role;
|
private Role role;
|
||||||
|
private String jwtToken;
|
||||||
|
private Long storeId;
|
||||||
|
|
||||||
private UserSession() {}
|
private UserSession() {}
|
||||||
|
|
||||||
@@ -18,12 +20,13 @@ public class UserSession {
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void login(int userId, int employeeId, String username, String employeeName, Role role) {
|
public void login(Long userId, String username, Role role, String jwtToken) {
|
||||||
this.userId = userId;
|
this.userId = userId;
|
||||||
this.employeeId = employeeId;
|
this.employeeId = userId;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.employeeName = employeeName;
|
this.employeeName = username;
|
||||||
this.role = role;
|
this.role = role;
|
||||||
|
this.jwtToken = jwtToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void logout() {
|
public void logout() {
|
||||||
@@ -32,13 +35,15 @@ public class UserSession {
|
|||||||
this.username = null;
|
this.username = null;
|
||||||
this.employeeName = null;
|
this.employeeName = null;
|
||||||
this.role = null;
|
this.role = null;
|
||||||
|
this.jwtToken = null;
|
||||||
|
this.storeId = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getUserId() {
|
public Long getUserId() {
|
||||||
return userId;
|
return userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getEmployeeId() {
|
public Long getEmployeeId() {
|
||||||
return employeeId;
|
return employeeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,6 +59,18 @@ public class UserSession {
|
|||||||
return role;
|
return role;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getJwtToken() {
|
||||||
|
return jwtToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getStoreId() {
|
||||||
|
return storeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStoreId(Long storeId) {
|
||||||
|
this.storeId = storeId;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isLoggedIn() {
|
public boolean isLoggedIn() {
|
||||||
return username != null && role != null;
|
return username != null && role != null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,13 +10,17 @@ import javafx.scene.control.PasswordField;
|
|||||||
import javafx.scene.control.TextField;
|
import javafx.scene.control.TextField;
|
||||||
import javafx.stage.Modality;
|
import javafx.stage.Modality;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
import org.example.petshopdesktop.api.ApiClient;
|
||||||
|
import org.example.petshopdesktop.api.dto.auth.LoginRequest;
|
||||||
|
import org.example.petshopdesktop.api.dto.auth.LoginResponse;
|
||||||
|
import org.example.petshopdesktop.api.dto.auth.UserInfoResponse;
|
||||||
|
import org.example.petshopdesktop.api.dto.common.DropdownOption;
|
||||||
|
import org.example.petshopdesktop.api.endpoints.DropdownApi;
|
||||||
|
import org.example.petshopdesktop.auth.Role;
|
||||||
import org.example.petshopdesktop.auth.UserSession;
|
import org.example.petshopdesktop.auth.UserSession;
|
||||||
import org.example.petshopdesktop.database.ConnectionDB;
|
|
||||||
import org.example.petshopdesktop.database.UserDB;
|
|
||||||
import org.example.petshopdesktop.models.User;
|
|
||||||
import org.example.petshopdesktop.util.ActivityLogger;
|
import org.example.petshopdesktop.util.ActivityLogger;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.util.List;
|
||||||
|
|
||||||
public class LoginController {
|
public class LoginController {
|
||||||
|
|
||||||
@@ -32,15 +36,6 @@ public class LoginController {
|
|||||||
@FXML
|
@FXML
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
lblError.setText("");
|
lblError.setText("");
|
||||||
try {
|
|
||||||
ConnectionDB.getConnection().close();
|
|
||||||
try {
|
|
||||||
UserDB.initializeTable();
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
lblError.setText("Database is not connected. Check Docker and connectionpetstore.properties.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
@@ -54,39 +49,43 @@ public class LoginController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
User user = UserDB.authenticate(username, password);
|
ApiClient apiClient = ApiClient.getInstance();
|
||||||
if (user == null) {
|
|
||||||
lblError.setText("Invalid username or password.");
|
LoginRequest loginRequest = new LoginRequest(username, password);
|
||||||
txtPassword.clear();
|
LoginResponse loginResponse = apiClient.post("/api/v1/auth/login", loginRequest, LoginResponse.class);
|
||||||
return;
|
|
||||||
|
String token = loginResponse.getToken();
|
||||||
|
String roleStr = loginResponse.getRole();
|
||||||
|
Role role = Role.valueOf(roleStr.toUpperCase());
|
||||||
|
|
||||||
|
UserSession.getInstance().login(null, username, role, token);
|
||||||
|
|
||||||
|
UserInfoResponse userInfo = apiClient.get("/api/v1/auth/me", UserInfoResponse.class);
|
||||||
|
UserSession.getInstance().login(userInfo.getId(), username, role, token);
|
||||||
|
|
||||||
|
List<DropdownOption> stores = DropdownApi.getInstance().getStores();
|
||||||
|
if (!stores.isEmpty()) {
|
||||||
|
UserSession.getInstance().setStoreId(stores.get(0).getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
UserSession.getInstance().login(
|
|
||||||
user.getUserId(),
|
|
||||||
user.getEmployeeId(),
|
|
||||||
user.getUsername(),
|
|
||||||
user.getEmployeeFullName(),
|
|
||||||
user.getRole()
|
|
||||||
);
|
|
||||||
openMainLayout();
|
openMainLayout();
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (Exception e) {
|
||||||
ActivityLogger.getInstance().logException(
|
ActivityLogger.getInstance().logException(
|
||||||
"LoginController.btnLoginClicked",
|
"LoginController.btnLoginClicked",
|
||||||
e,
|
e,
|
||||||
"Authentication attempt for username: " + username);
|
"Authentication attempt for username: " + username);
|
||||||
String msg = e.getMessage() == null ? "" : e.getMessage().toLowerCase();
|
|
||||||
if (msg.contains("doesn't exist") || msg.contains("unknown database") || msg.contains("access denied")) {
|
String errorMsg = e.getMessage();
|
||||||
lblError.setText("Database error. Check Docker and connectionpetstore.properties.");
|
if (errorMsg != null && errorMsg.contains("Authentication failed")) {
|
||||||
|
lblError.setText("Invalid username or password.");
|
||||||
|
txtPassword.clear();
|
||||||
|
} else if (e.getCause() instanceof java.net.ConnectException ||
|
||||||
|
e instanceof java.net.http.HttpConnectTimeoutException) {
|
||||||
|
lblError.setText("Backend is not reachable, check backend docker compose and port 8080.");
|
||||||
} else {
|
} else {
|
||||||
lblError.setText("Login failed. Check username and password.");
|
lblError.setText(errorMsg != null ? errorMsg : "Login failed. Please try again.");
|
||||||
}
|
}
|
||||||
} catch (RuntimeException e) {
|
|
||||||
ActivityLogger.getInstance().logException(
|
|
||||||
"LoginController.btnLoginClicked",
|
|
||||||
e,
|
|
||||||
"Database connection");
|
|
||||||
lblError.setText("Database is not connected. Check Docker and connectionpetstore.properties.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user