11package net .hackyourfuture .coursehub .web ;
22
33import jakarta .servlet .http .HttpServletRequest ;
4- import net . hackyourfuture . coursehub . repository . StudentRepository ;
4+ import jakarta . servlet . http . HttpServletResponse ;
55import net .hackyourfuture .coursehub .service .UserAuthenticationService ;
66import net .hackyourfuture .coursehub .web .model .HttpErrorResponse ;
77import net .hackyourfuture .coursehub .web .model .LoginRequest ;
1616import org .springframework .security .core .Authentication ;
1717import org .springframework .security .core .AuthenticationException ;
1818import org .springframework .security .core .context .SecurityContextHolder ;
19+ import org .springframework .security .web .context .HttpSessionSecurityContextRepository ;
20+ import org .springframework .security .web .context .SecurityContextRepository ;
1921import org .springframework .validation .annotation .Validated ;
2022import org .springframework .web .bind .annotation .PostMapping ;
2123import org .springframework .web .bind .annotation .RequestBody ;
2628public class UserAuthenticationController {
2729 private final AuthenticationManager authenticationManager ;
2830 private final UserAuthenticationService userAuthenticationService ;
29- private final StudentRepository studentRepository ;
31+ private final SecurityContextRepository securityContextRepository = new HttpSessionSecurityContextRepository () ;
3032
3133 public UserAuthenticationController (
3234 AuthenticationManager authenticationManager ,
33- UserAuthenticationService userAuthenticationService ,
34- StudentRepository studentRepository ) {
35+ UserAuthenticationService userAuthenticationService ) {
3536 this .authenticationManager = authenticationManager ;
3637 this .userAuthenticationService = userAuthenticationService ;
37- this .studentRepository = studentRepository ;
3838 }
3939
4040 @ PostMapping ("/login" )
41- public ResponseEntity <Object > login (@ RequestBody LoginRequest request , HttpServletRequest httpRequest ) {
41+ public ResponseEntity <Object > login (@ RequestBody LoginRequest request , HttpServletRequest httpRequest , HttpServletResponse httpResponse ) {
4242 try {
43- var response = authenticate (httpRequest , request .emailAddress (), request .password ());
43+ var response = authenticate (httpRequest , httpResponse , request .emailAddress (), request .password ());
4444 return ResponseEntity .ok (response );
4545 } catch (AuthenticationException e ) {
4646 if (e instanceof BadCredentialsException ) {
@@ -64,25 +64,29 @@ public ResponseEntity<?> logout(HttpServletRequest httpRequest) {
6464 }
6565
6666 @ PostMapping ("/register" )
67- public LoginSuccessResponse register (@ RequestBody RegisterRequest request , HttpServletRequest httpRequest ) {
67+ public LoginSuccessResponse register (@ RequestBody RegisterRequest request , HttpServletRequest httpRequest , HttpServletResponse httpResponse ) {
6868 userAuthenticationService .register (
6969 request .firstName (),
7070 request .lastName (),
7171 request .emailAddress (),
7272 request .password ()
7373 );
7474
75- return authenticate (httpRequest , request .emailAddress (), request .password ());
75+ return authenticate (httpRequest , httpResponse , request .emailAddress (), request .password ());
7676 }
7777
78- private LoginSuccessResponse authenticate (HttpServletRequest httpRequest , String email , String password ) {
78+ private LoginSuccessResponse authenticate (HttpServletRequest request , HttpServletResponse response , String email , String password ) {
7979 // Authenticate the user with the provided credentials (email and password)
8080 Authentication authentication = authenticationManager .authenticate (
8181 new UsernamePasswordAuthenticationToken (email , password ));
82+
83+ SecurityContextHolder .clearContext ();
84+ var context = SecurityContextHolder .createEmptyContext ();
85+ context .setAuthentication (authentication );
86+ SecurityContextHolder .setContext (context );
87+
8288 // Save the authenticated user in the Spring security context
83- SecurityContextHolder .getContext ().setAuthentication (authentication );
84- // Ensure a session is created for the authenticated user
85- httpRequest .getSession (true );
89+ securityContextRepository .saveContext (context , request , response );
8690
8791 // Retrieve the corresponding user data to return in a login response
8892 var authenticatedUser = userAuthenticationService .currentAuthenticatedUser ();
0 commit comments