Skip to content

Commit dabe3e6

Browse files
refactor: remove unused login methods
login methods provided by any other class else the AuthenticationController were removed for lack of use
1 parent d3cd0bd commit dabe3e6

14 files changed

Lines changed: 14 additions & 279 deletions

src/main/java/com/vianavitor/simplelibrarygame/controller/AdministratorController.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,4 @@ public ResponseEntity<ApiResponse<Administrator>> register(@RequestBody @Valid U
2929
return ResponseEntity.status(HttpStatus.CREATED)
3030
.body(ApiResponse.success(saved, "Administrator registered", req.getRequestURI()));
3131
}
32-
33-
@PostMapping("/login")
34-
public ResponseEntity<ApiResponse<Long>> login(@RequestBody Map<String, String> credentials, HttpServletRequest request) {
35-
String username = credentials.get("username");
36-
String password = credentials.get("password");
37-
Long userId = administratorService.login(username, password);
38-
ApiResponse<Long> response = ApiResponse.success(userId, "Login successful", request.getRequestURI());
39-
return ResponseEntity.ok(response);
40-
}
4132
}

src/main/java/com/vianavitor/simplelibrarygame/controller/LibrarianController.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ public ResponseEntity<ApiResponse<Librarian>> register(@RequestBody Librarian li
2626
return ResponseEntity.status(HttpStatus.CREATED).body(response);
2727
}
2828

29-
@PostMapping("/login")
30-
public ResponseEntity<ApiResponse<Long>> login(@RequestBody Map<String, String> credentials, HttpServletRequest request) {
31-
Long id = librarianService.login(credentials.get("username"), credentials.get("password"));
32-
ApiResponse<Long> response = ApiResponse.success(id, "Login successful", request.getRequestURI());
33-
return ResponseEntity.ok(response);
34-
}
35-
3629
@GetMapping
3730
public ResponseEntity<ApiResponse<List<Librarian>>> getAll(HttpServletRequest request) {
3831
List<Librarian> librarians = librarianService.getAll();

src/main/java/com/vianavitor/simplelibrarygame/controller/ProfessorController.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ public ResponseEntity<ApiResponse<Professor>> register(@Valid @RequestBody Regis
3333
.body(ApiResponse.success(professor, "Professor registered", req.getRequestURI()));
3434
}
3535

36-
@PostMapping("/login")
37-
public ResponseEntity<ApiResponse<Long>> login(@Valid @RequestBody LoginRequest request, HttpServletRequest req) {
38-
Long id = professorService.login(request.username(), request.password());
39-
return ResponseEntity.ok(ApiResponse.success(id, "Login successful", req.getRequestURI()));
40-
}
41-
4236
@GetMapping
4337
public ResponseEntity<ApiResponse<List<Professor>>> getAll(HttpServletRequest request) {
4438
List<Professor> professors = professorService.getAll();

src/main/java/com/vianavitor/simplelibrarygame/controller/StudentController.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ public ResponseEntity<ApiResponse<Student>> register(@Valid @RequestBody Registe
3737
.body(ApiResponse.success(student, "Student registered", req.getRequestURI()));
3838
}
3939

40-
@PostMapping("/login")
41-
public ResponseEntity<ApiResponse<Long>> login(@Valid @RequestBody LoginRequest request, HttpServletRequest req) {
42-
Long id = studentService.login(request.username(), request.password());
43-
return ResponseEntity.ok(ApiResponse.success(id, "Login successful", req.getRequestURI()));
44-
}
45-
4640
@GetMapping("/{id}/stats")
4741
public ResponseEntity<ApiResponse<StudentStats>> getStats(@PathVariable Long id, HttpServletRequest request) {
4842
StudentStats stats = studentService.getStats(id);

src/main/java/com/vianavitor/simplelibrarygame/controller/auth/AuthenticationController.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.vianavitor.simplelibrarygame.dto.ApiResponse;
55
import com.vianavitor.simplelibrarygame.dto.request.LoginRequest;
66
import com.vianavitor.simplelibrarygame.model.utils.classes.User;
7+
import com.vianavitor.simplelibrarygame.repository.UserRepository;
78
import com.vianavitor.simplelibrarygame.service.auth.AuthorizationService;
89
import com.vianavitor.simplelibrarygame.service.auth.TokenService;
910
import jakarta.servlet.http.HttpServletRequest;
@@ -19,6 +20,8 @@
1920
import org.springframework.web.bind.annotation.RequestMapping;
2021
import org.springframework.web.bind.annotation.RestController;
2122

23+
import java.time.LocalDate;
24+
2225
@RestController
2326
@RequestMapping("/auth")
2427
public class AuthenticationController {
@@ -31,19 +34,27 @@ public class AuthenticationController {
3134
@Autowired
3235
private TokenService tokenService;
3336

37+
@Autowired
38+
private UserRepository repository;
39+
3440
@PostMapping("/login")
3541
public ResponseEntity<ApiResponse<?>> login(@RequestBody @Valid LoginRequest request, HttpServletRequest req)
3642
throws JWTCreationException, DisabledException, BadCredentialsException {
3743
var login = new UsernamePasswordAuthenticationToken(request.username(), request.password());
3844
var auth = authenticationManager.authenticate(login);
3945

40-
if (auth.getPrincipal() == null) {
46+
User user = (User) auth.getPrincipal();
47+
48+
if (user == null) {
4149
return ResponseEntity
4250
.internalServerError()
4351
.body(ApiResponse.error("missing principal", req.getRequestURI()));
4452
}
4553

46-
String token = tokenService.generateToken((User) auth.getPrincipal());
54+
user.setLastLogin(LocalDate.now());
55+
repository.save(user);
56+
57+
String token = tokenService.generateToken(user);
4758
return ResponseEntity.ok(ApiResponse.success(token, "Login successful", req.getRequestURI()));
4859
}
4960
}

src/main/java/com/vianavitor/simplelibrarygame/service/AdministratorService.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,6 @@ public Administrator register(Administrator entity) {
3737
return repository.save(entity);
3838
}
3939

40-
public Long login(String username, String password) throws InvalidOperationException, UserDeactivatedException{
41-
Administrator administrator = (Administrator) repository.findByUsername(username)
42-
.orElseThrow(() -> new InvalidOperationException("invalid username or password"));
43-
44-
boolean invalidPassword = !encoder.matches(password, administrator.getPassword());
45-
46-
if (invalidPassword) {
47-
throw new InvalidOperationException("invalid username or password");
48-
}
49-
50-
boolean wasUserDeactivated = !administrator.isActive();
51-
if (wasUserDeactivated) {
52-
throw new UserDeactivatedException("this user was deactivated, talk with a administrator or administrador to get more information");
53-
}
54-
55-
// TODO: create JWT Token for authentication
56-
// ...
57-
58-
administrator.setLastLogin(LocalDate.now());
59-
repository.save(administrator);
60-
61-
return administrator.getId();
62-
}
63-
6440
@Override
6541
public void deactivate(Long id) {
6642
Administrator administrator = repository.findById(id)

src/main/java/com/vianavitor/simplelibrarygame/service/LibrarianService.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,6 @@ public Librarian register(Librarian entity) throws DuplicateResourceException {
3737
return repository.save(entity);
3838
}
3939

40-
@Override
41-
public Long login(String username, String password) throws InvalidOperationException, UserDeactivatedException{
42-
Librarian librarian = (Librarian) repository.findByUsername(username)
43-
.orElseThrow(() -> new InvalidOperationException("invalid username or password"));
44-
45-
boolean invalidPassword = !encoder.matches(password, librarian.getPassword());
46-
47-
if (invalidPassword) {
48-
throw new InvalidOperationException("invalid username or password");
49-
}
50-
51-
boolean wasUserDeactivated = !librarian.isActive();
52-
if (wasUserDeactivated) {
53-
throw new UserDeactivatedException("this user was deactivated, talk with a professor or administrador to get more information");
54-
}
55-
56-
// TODO: create JWT Token for authentication
57-
// ...
58-
59-
librarian.setLastLogin(LocalDate.now());
60-
repository.save(librarian);
61-
62-
return librarian.getId();
63-
}
64-
6540
public List<Librarian> getAll() {
6641
return (List<Librarian>) repository.findAll();
6742
}

src/main/java/com/vianavitor/simplelibrarygame/service/ProfessorService.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,6 @@ public void register(Professor newProfessor, String classroomCode) throws Duplic
5252
this.register(newProfessor);
5353
}
5454

55-
@Override
56-
public Long login(String username, String password) throws InvalidOperationException, UserDeactivatedException {
57-
Professor professor = (Professor) repository.findByUsername(username)
58-
.orElseThrow(() -> new InvalidOperationException("invalid username or password"));
59-
60-
boolean invalidPassword = !encoder.matches(password, professor.getPassword());
61-
62-
if (invalidPassword) {
63-
throw new InvalidOperationException("invalid username or password");
64-
}
65-
66-
boolean wasUserDeactivated = !professor.isActive();
67-
if (wasUserDeactivated) {
68-
throw new UserDeactivatedException("this user was deactivated, talk with a professor or administrador to get more information");
69-
}
70-
71-
// TODO: create JWT Token for authentication
72-
// ...
73-
74-
professor.setLastLogin(LocalDate.now());
75-
repository.save(professor);
76-
77-
return professor.getId();
78-
}
79-
8055
public List<Professor> getAll() {
8156
return (List<Professor>) repository.findAll();
8257
}

src/main/java/com/vianavitor/simplelibrarygame/service/StudentService.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -93,31 +93,6 @@ public StudentStats getStats(Long id) throws ResourceNotFoundException {
9393
.orElseThrow(() -> new ResourceNotFoundException("not found student stats"));
9494
}
9595

96-
@Override
97-
public Long login(String username, String password) throws InvalidOperationException, UserDeactivatedException{
98-
Student student = (Student) repository.findByUsername(username)
99-
.orElseThrow(() -> new InvalidOperationException("invalid username or password"));
100-
101-
boolean invalidPassword = !encoder.matches(password, student.getPassword());
102-
103-
if (invalidPassword) {
104-
throw new InvalidOperationException("invalid username or password");
105-
}
106-
107-
boolean wasUserDeactivated = !student.isActive();
108-
if (wasUserDeactivated) {
109-
throw new UserDeactivatedException("this user was deactivated, talk with a professor or administrador to get more information");
110-
}
111-
112-
// TODO: create JWT Token for authentication
113-
// ...
114-
115-
student.setLastLogin(LocalDate.now());
116-
repository.save(student);
117-
118-
return student.getId();
119-
}
120-
12196
public List<Student> getAll() {
12297
return (List<Student>) repository.findAll();
12398
}

src/main/java/com/vianavitor/simplelibrarygame/service/utils/ManageableUser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public interface ManageableUser<T extends User> {
66

77
T register(T entity);
88

9-
Long login(String username, String password);
9+
// Long login(String username, String password);
1010

1111
void deactivate(Long id);
1212

0 commit comments

Comments
 (0)