diff --git a/src/main/java/com/web/web_assignment/ProjectBean.java b/src/main/java/com/web/web_assignment/ProjectBean.java index 23f2510..9a8daef 100644 --- a/src/main/java/com/web/web_assignment/ProjectBean.java +++ b/src/main/java/com/web/web_assignment/ProjectBean.java @@ -24,6 +24,7 @@ public class ProjectBean implements Serializable { private UserInfo userInfo; // User info for the current user private String username; private String password; + private boolean isLoggedIn = false; public String projectKeywords = ""; // String to hold project keywords public String newKeyword = ""; public String projectCollaborators = ""; // String to hold project collaborators @@ -136,12 +137,16 @@ public class ProjectBean implements Serializable { } public String register() { + isLoggedIn = false; try { databaseService.registerUser(username, password); } catch (SQLException e) { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Registration failed", null)); return "register"; } + FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Registration succeeded", null)); + FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("username"); + FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("password"); return "login"; } @@ -151,12 +156,18 @@ public class ProjectBean implements Serializable { FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("username", username); loadUserInfo(); loadProjects(); - return "project"; + isLoggedIn = true; + return "index"; } else { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Login failed", null)); + FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("username"); + isLoggedIn = false; return "login"; } } catch (SQLException e) { + FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Login failed", null)); + FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("username"); + isLoggedIn = false; e.printStackTrace(); return "login"; } @@ -164,6 +175,7 @@ public class ProjectBean implements Serializable { public String logout() { FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); + isLoggedIn = false; return "index"; } @@ -239,4 +251,12 @@ public class ProjectBean implements Serializable { public void setNewKeyword(String newKeyword) { this.newKeyword = newKeyword; } + + public boolean isLoggedIn() { + return isLoggedIn; + } + + public void setLoggedIn(boolean loggedIn) { + isLoggedIn = loggedIn; + } }