Skip to content

Commit

Permalink
Modified LoginController
Browse files Browse the repository at this point in the history
-Added method setSessionAttributes in LoginController
-Added method getPersonalInfo in DAOPersonalInfo
-Refactored DAOAPersonalInfo to DAOPersonalInfo
-Added method getPersonalInfo to UserRegistry
-Added methods getUserByIdOrEmail and isTherapist in UserData
-Added Servlet-API in pom.xml
  • Loading branch information
panuozzo77 committed Dec 23, 2023
1 parent 42a34a5 commit cdebe7d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@
<artifactId>jakarta.mail</artifactId>
<version>1.6.5</version>
</dependency>

<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api</artifactId>
<version>4.0.1</version>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

</dependencies>
Expand Down
37 changes: 28 additions & 9 deletions src/main/java/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package controller;

import model.DAO.DAOUser;
import model.DAO.DAOPersonalInfo;
import model.entity.PersonalInfo;
import model.entity.User;
import model.service.login.Authenticator;
import model.service.user.UserData;
import model.service.user.UserRegistry;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
Expand All @@ -27,24 +30,40 @@ public void init() {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


String email = request.getParameter("email");
String password = request.getParameter("password");

System.out.println(email);
System.out.println(password);
int result = authService.authenticate(email, password);

if (result > 0) {
// Login success, redirect to the welcome page
HttpSession session = request.getSession();
DAOUser daouser=new DAOUser();
User user_logged= daouser.getUserByIdOrEmail(email);
session.setAttribute("user_logged", user_logged);
// Login success, defining its Session attributes
setSessionAttributes(result,request);
response.sendRedirect("JSP/welcome.jsp");
} else {
// Login failed, redirect back to the login page
response.sendRedirect("JSP/login.jsp?error=1");
}
}

private void setSessionAttributes(int id, HttpServletRequest request){
HttpSession session = request.getSession();

UserData userData = new UserData();
UserRegistry userReg = new UserRegistry();

User user = userData.getUserByIdOrEmail(id);
PersonalInfo personalInfo = userReg.getPersonalInfo(id);

session.setAttribute("id", id);
session.setAttribute("name", personalInfo.getFirstname());

if(userData.isTherapist(user)) {
session.setAttribute("type", "patient");
session.setAttribute("therapist", user.getIdTherapist());
}
else {
session.setAttribute("type", "therapist");
}
}

}
2 changes: 1 addition & 1 deletion src/main/java/model/DAO/DAOPersonalInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;

public class DAOAPersonalInfo {
public class DAOPersonalInfo {

private PersonalInfo getPersonalInfoFromResultSet(ResultSet resultSet) throws SQLException {
PersonalInfo personalInfo = new PersonalInfo();
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/model/service/user/UserData.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model.service.user;

import model.DAO.DAOUser;
import model.entity.User;

public class UserData /*implements UserDataInterface */{
DAOUser daoUser = new DAOUser();
Expand All @@ -12,4 +13,12 @@ public boolean checkIfEmailExists(String email) {
public int createUser(String email, String password, int therapistId) {
return daoUser.createUser(email, password, therapistId);
}

public User getUserByIdOrEmail(Object idOrEmail) {
return daoUser.getUserByIdOrEmail(idOrEmail);
}

public boolean isTherapist(User user){
return user.getIdTherapist() > 0;
}
}
9 changes: 9 additions & 0 deletions src/main/java/model/service/user/UserRegistry.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package model.service.user;

import model.DAO.DAOPersonalInfo;
import model.entity.PersonalInfo;

public class UserRegistry implements UserRegistryInterface {
DAOPersonalInfo db = new DAOPersonalInfo();

public boolean firstAccess(int id, String name, String surname) {//TODO e da inserire nel metodo di registrazione!
System.out.println("hello, this is a placeholder");
return true; //TODO Remove it!
}

public PersonalInfo getPersonalInfo(int id) {
return db.getPersonalInfo(id);
}

}

0 comments on commit cdebe7d

Please sign in to comment.