11package net .hackyourfuture .coursehub .web ;
22
33import jakarta .servlet .http .HttpServletRequest ;
4- import net .hackyourfuture .coursehub .data .InstructorEntity ;
5- import net .hackyourfuture .coursehub .data .StudentEntity ;
6- import net .hackyourfuture .coursehub .data .UserAccountEntity ;
74import net .hackyourfuture .coursehub .repository .InstructorRepository ;
85import net .hackyourfuture .coursehub .repository .StudentRepository ;
9- import net .hackyourfuture .coursehub .repository . UserAccountRepository ;
6+ import net .hackyourfuture .coursehub .service . UserAuthenticationService ;
107import net .hackyourfuture .coursehub .web .model .HttpErrorResponse ;
118import net .hackyourfuture .coursehub .web .model .LoginRequest ;
129import net .hackyourfuture .coursehub .web .model .LoginSuccessResponse ;
10+ import net .hackyourfuture .coursehub .web .model .RegisterRequest ;
1311import org .springframework .http .HttpStatus ;
1412import org .springframework .http .ResponseEntity ;
1513import org .springframework .security .authentication .AuthenticationCredentialsNotFoundException ;
2826@ RestController
2927public class UserAuthenticationController {
3028 private final AuthenticationManager authenticationManager ;
31- private final UserAccountRepository userAccountRepository ;
29+ private final UserAuthenticationService userAuthenticationService ;
3230 private final StudentRepository studentRepository ;
33- private final InstructorRepository instructorRepository ;
3431
3532 public UserAuthenticationController (
3633 AuthenticationManager authenticationManager ,
37- UserAccountRepository userAccountRepository ,
38- StudentRepository studentRepository ,
39- InstructorRepository instructorRepository ) {
34+ UserAuthenticationService userAuthenticationService ,
35+ StudentRepository studentRepository ) {
4036 this .authenticationManager = authenticationManager ;
41- this .userAccountRepository = userAccountRepository ;
37+ this .userAuthenticationService = userAuthenticationService ;
4238 this .studentRepository = studentRepository ;
43- this .instructorRepository = instructorRepository ;
4439 }
4540
4641 @ PostMapping ("/login" )
4742 public ResponseEntity <Object > login (@ RequestBody LoginRequest request , HttpServletRequest httpRequest ) {
4843 try {
49- // Authenticate the user with the provided credentials (email and password)
50- Authentication authentication = authenticationManager .authenticate (
51- 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
58- UserAccountEntity user = userAccountRepository .findByEmailAddress (request .emailAddress ());
59- if (user == null ) {
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- }
77- }
78- return ResponseEntity .ok (
79- new LoginSuccessResponse (user .userId (), firstName , lastName , user .emailAddress (), user .role ()));
44+ var response = authenticate (httpRequest , request .emailAddress (), request .password ());
45+ return ResponseEntity .ok (response );
8046 } catch (AuthenticationException e ) {
8147 if (e instanceof BadCredentialsException ) {
8248 return ResponseEntity .status (HttpStatus .UNAUTHORIZED )
@@ -90,4 +56,43 @@ public ResponseEntity<Object> login(@RequestBody LoginRequest request, HttpServl
9056 .body (new HttpErrorResponse ("Something went wrong" ));
9157 }
9258 }
93- }
59+
60+ @ PostMapping ("/logout" )
61+ public ResponseEntity <?> logout (HttpServletRequest httpRequest ) {
62+ SecurityContextHolder .clearContext ();
63+ httpRequest .getSession ().invalidate ();
64+ return ResponseEntity .ok ().build ();
65+ }
66+
67+ @ PostMapping ("/register" )
68+ public LoginSuccessResponse register (@ RequestBody RegisterRequest request , HttpServletRequest httpRequest ) {
69+ userAuthenticationService .register (
70+ request .firstName (),
71+ request .lastName (),
72+ request .emailAddress (),
73+ request .password ()
74+ );
75+
76+ return authenticate (httpRequest , request .emailAddress (), request .password ());
77+ }
78+
79+ private LoginSuccessResponse authenticate (HttpServletRequest httpRequest , String email , String password ) {
80+ // Authenticate the user with the provided credentials (email and password)
81+ Authentication authentication = authenticationManager .authenticate (
82+ new UsernamePasswordAuthenticationToken (email , password ));
83+ // Save the authenticated user in the Spring security context
84+ SecurityContextHolder .getContext ().setAuthentication (authentication );
85+ // Ensure a session is created for the authenticated user
86+ httpRequest .getSession (true );
87+
88+ // Retrieve the corresponding user data to return in a login response
89+ var authenticatedUser = userAuthenticationService .currentAuthenticatedUser ();
90+ return new LoginSuccessResponse (
91+ authenticatedUser .userId (),
92+ authenticatedUser .firstName (),
93+ authenticatedUser .lastName (),
94+ authenticatedUser .emailAddress (),
95+ authenticatedUser .role ()
96+ );
97+ }
98+ }
0 commit comments