11package net .hackyourfuture .coursehub .web ;
22
3+ import jakarta .servlet .http .HttpServletRequest ;
4+ import net .hackyourfuture .coursehub .data .InstructorEntity ;
5+ import net .hackyourfuture .coursehub .data .StudentEntity ;
36import net .hackyourfuture .coursehub .data .UserAccountEntity ;
7+ import net .hackyourfuture .coursehub .repository .InstructorRepository ;
8+ import net .hackyourfuture .coursehub .repository .StudentRepository ;
49import net .hackyourfuture .coursehub .repository .UserAccountRepository ;
10+ import net .hackyourfuture .coursehub .web .model .HttpErrorResponse ;
511import net .hackyourfuture .coursehub .web .model .LoginRequest ;
6- import net .hackyourfuture .coursehub .web .model .LoginResponse ;
12+ import net .hackyourfuture .coursehub .web .model .LoginSuccessResponse ;
13+ import org .springframework .http .HttpStatus ;
14+ import org .springframework .http .ResponseEntity ;
715import org .springframework .security .authentication .AuthenticationCredentialsNotFoundException ;
816import org .springframework .security .authentication .AuthenticationManager ;
917import org .springframework .security .authentication .BadCredentialsException ;
1018import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
1119import org .springframework .security .core .Authentication ;
1220import org .springframework .security .core .AuthenticationException ;
21+ import org .springframework .security .core .context .SecurityContextHolder ;
22+ import org .springframework .validation .annotation .Validated ;
1323import org .springframework .web .bind .annotation .PostMapping ;
1424import org .springframework .web .bind .annotation .RequestBody ;
1525import org .springframework .web .bind .annotation .RestController ;
1626
27+ @ Validated // will make sure that every request body annotated with @RequestBody is validated
1728@ RestController
1829public class UserAuthenticationController {
1930 private final AuthenticationManager authenticationManager ;
2031 private final UserAccountRepository userAccountRepository ;
32+ private final StudentRepository studentRepository ;
33+ private final InstructorRepository instructorRepository ;
2134
2235 public UserAuthenticationController (
2336 AuthenticationManager authenticationManager ,
24- UserAccountRepository userAccountRepository ) {
37+ UserAccountRepository userAccountRepository ,
38+ StudentRepository studentRepository ,
39+ InstructorRepository instructorRepository ) {
2540 this .authenticationManager = authenticationManager ;
2641 this .userAccountRepository = userAccountRepository ;
42+ this .studentRepository = studentRepository ;
43+ this .instructorRepository = instructorRepository ;
2744 }
2845
2946 @ PostMapping ("/login" )
30- public LoginResponse login (@ RequestBody LoginRequest request ) {
47+ public ResponseEntity < Object > login (@ RequestBody LoginRequest request , HttpServletRequest httpRequest ) {
3148 try {
49+ // Authenticate the user with the provided credentials (email and password)
3250 Authentication authentication = authenticationManager .authenticate (
3351 new UsernamePasswordAuthenticationToken (request .emailAddress (), request .password ()));
52+ // Save the authenticated user in the Spring security context
53+ SecurityContextHolder .getContext ().setAuthentication (authentication );
54+ // Ensure a session is created for the authenticated user
55+ httpRequest .getSession (true );
56+
57+ // Retrieve the corresponding user data from the database to return
3458 UserAccountEntity user = userAccountRepository .findByEmailAddress (request .emailAddress ());
3559 if (user == null ) {
36- return new LoginResponse (null , false , "No user found for provided email address" );
60+ return ResponseEntity .status (HttpStatus .UNAUTHORIZED )
61+ .body (new HttpErrorResponse ("No user found for provided email address" ));
62+ }
63+
64+ String firstName = null ;
65+ String lastName = null ;
66+ switch (user .role ()) {
67+ case student -> {
68+ StudentEntity student = studentRepository .findById (user .userId ());
69+ firstName = student .firstName ();
70+ lastName = student .lastName ();
71+ }
72+ case instructor -> {
73+ InstructorEntity instructor = instructorRepository .findById (user .userId ());
74+ firstName = instructor .firstName ();
75+ lastName = instructor .lastName ();
76+ }
3777 }
38- return new LoginResponse (user .userId (), authentication .isAuthenticated (), null );
78+ return ResponseEntity .ok (
79+ new LoginSuccessResponse (user .userId (), firstName , lastName , user .emailAddress (), user .role ()));
3980 } catch (AuthenticationException e ) {
4081 if (e instanceof BadCredentialsException ) {
41- return new LoginResponse (null , false , "Invalid credentials provided" );
82+ return ResponseEntity .status (HttpStatus .UNAUTHORIZED )
83+ .body (new HttpErrorResponse ("Invalid credentials provided" ));
4284 }
4385 if (e instanceof AuthenticationCredentialsNotFoundException ) {
44- return new LoginResponse (null , false , "No user found for provided email address" );
86+ return ResponseEntity .status (HttpStatus .UNAUTHORIZED )
87+ .body (new HttpErrorResponse ("No user found for provided email address" ));
4588 }
46- return new LoginResponse (null , false , "Something went wrong" );
89+ return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR )
90+ .body (new HttpErrorResponse ("Something went wrong" ));
4791 }
4892 }
49- }
93+ }
0 commit comments