Update authentication to use REST API
This commit is contained in:
@@ -3,11 +3,13 @@ package org.example.petshopdesktop.auth;
|
||||
public class UserSession {
|
||||
private static UserSession instance;
|
||||
|
||||
private Integer userId;
|
||||
private Integer employeeId;
|
||||
private Long userId;
|
||||
private Long employeeId;
|
||||
private String username;
|
||||
private String employeeName;
|
||||
private Role role;
|
||||
private String jwtToken;
|
||||
private Long storeId;
|
||||
|
||||
private UserSession() {}
|
||||
|
||||
@@ -18,12 +20,13 @@ public class UserSession {
|
||||
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.employeeId = employeeId;
|
||||
this.employeeId = userId;
|
||||
this.username = username;
|
||||
this.employeeName = employeeName;
|
||||
this.employeeName = username;
|
||||
this.role = role;
|
||||
this.jwtToken = jwtToken;
|
||||
}
|
||||
|
||||
public void logout() {
|
||||
@@ -32,13 +35,15 @@ public class UserSession {
|
||||
this.username = null;
|
||||
this.employeeName = null;
|
||||
this.role = null;
|
||||
this.jwtToken = null;
|
||||
this.storeId = null;
|
||||
}
|
||||
|
||||
public Integer getUserId() {
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public Integer getEmployeeId() {
|
||||
public Long getEmployeeId() {
|
||||
return employeeId;
|
||||
}
|
||||
|
||||
@@ -54,6 +59,18 @@ public class UserSession {
|
||||
return role;
|
||||
}
|
||||
|
||||
public String getJwtToken() {
|
||||
return jwtToken;
|
||||
}
|
||||
|
||||
public Long getStoreId() {
|
||||
return storeId;
|
||||
}
|
||||
|
||||
public void setStoreId(Long storeId) {
|
||||
this.storeId = storeId;
|
||||
}
|
||||
|
||||
public boolean isLoggedIn() {
|
||||
return username != null && role != null;
|
||||
}
|
||||
|
||||
@@ -10,13 +10,17 @@ import javafx.scene.control.PasswordField;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.stage.Modality;
|
||||
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.database.ConnectionDB;
|
||||
import org.example.petshopdesktop.database.UserDB;
|
||||
import org.example.petshopdesktop.models.User;
|
||||
import org.example.petshopdesktop.util.ActivityLogger;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public class LoginController {
|
||||
|
||||
@@ -32,15 +36,6 @@ public class LoginController {
|
||||
@FXML
|
||||
public void initialize() {
|
||||
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
|
||||
@@ -54,39 +49,43 @@ public class LoginController {
|
||||
}
|
||||
|
||||
try {
|
||||
User user = UserDB.authenticate(username, password);
|
||||
if (user == null) {
|
||||
lblError.setText("Invalid username or password.");
|
||||
txtPassword.clear();
|
||||
return;
|
||||
ApiClient apiClient = ApiClient.getInstance();
|
||||
|
||||
LoginRequest loginRequest = new LoginRequest(username, password);
|
||||
LoginResponse loginResponse = apiClient.post("/api/v1/auth/login", loginRequest, LoginResponse.class);
|
||||
|
||||
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();
|
||||
|
||||
} catch (SQLException e) {
|
||||
} catch (Exception e) {
|
||||
ActivityLogger.getInstance().logException(
|
||||
"LoginController.btnLoginClicked",
|
||||
e,
|
||||
"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")) {
|
||||
lblError.setText("Database error. Check Docker and connectionpetstore.properties.");
|
||||
|
||||
String errorMsg = e.getMessage();
|
||||
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 {
|
||||
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