From a26818f107c2f5bc2c24226551db0b04500d05e8 Mon Sep 17 00:00:00 2001 From: heshunme Date: Sun, 14 Jul 2024 14:59:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E5=B1=9E=E6=80=A7=EF=BC=8C=E4=B8=BA=E5=9C=A8?= =?UTF-8?q?header=E4=B8=AD=E6=B7=BB=E5=8A=A0=E7=99=BB=E5=BD=95=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=8C=89=E9=92=AE=E5=81=9A=E5=87=86=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/web/web_assignment/ProjectBean.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) 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; + } }