content: package com.taskmanager.taskmanager.security.controller;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  import org.springframework.security.core.context.SecurityContextHolder;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.security.core.userdetails.UsernameNotFoundException;
  import org.springframework.security.crypto.password.PasswordEncoder;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  
  import com.taskmanager.taskmanager.entity.LoginRequest;
  import com.taskmanager.taskmanager.entity.RegisterRequest;
  import com.taskmanager.taskmanager.entity.User;
  import com.taskmanager.taskmanager.repository.UserRepository;
  import com.taskmanager.taskmanager.security.service.JwtService;
  import com.taskmanager.taskmanager.service.UserService;
  
  
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  
  
  @RestController
  @RequestMapping("/auth")
  public class AuthController {
  
      private final PasswordEncoder passwordEncoder;
  
      @Autowired
      private AuthenticationManager authenticationManager;
  
      @Autowired
      private JwtService jwtService;
  
      @Autowired
      private UserService userService;
  
      @Autowired
      private UserRepository userRepository;
  
      AuthController(PasswordEncoder passwordEncoder) {
          this.passwordEncoder = passwordEncoder;
      }
  
      @PostMapping("/register")
      public ResponseEntity<String> register(@RequestBody RegisterRequest request) {
          String encodedPassword = passwordEncoder.encode(request.getPassword());
          // Create a User from the request
          User user = new User();
          user.setEmail(request.getEmail());
          user.setPasswordHash(encodedPassword);
          userService.register(user);
          
          // Authenticate with raw password
          Authentication authentication = authenticationManager.authenticate(
              new UsernamePasswordAuthenticationToken(request.getEmail(), request.getPassword()));
          
          SecurityContextHolder.getContext().setAuthentication(authentication);
          UserDetails userDetails = (UserDetails) authentication.getPrincipal();
          String token = jwtService.generateToken(userDetails.getUsername());
  
          return ResponseEntity.ok(token);
      }
  
  
      @PostMapping("/login")
      public ResponseEntity<String> login(@RequestBody LoginRequest loginRequest) {
          // Create Spring's built-in Authentication token with user/pass
          Authentication authentication = authenticationManager.authenticate(
                  new UsernamePasswordAuthenticationToken(loginRequest.getEmail(), loginRequest.getPassword()));
          // Store the authentication in the SecurityContext for the current request
          SecurityContextHolder.getContext().setAuthentication(authentication);
          // Extract authenticated user details
          UserDetails userDetails = (UserDetails) authentication.getPrincipal();
          // Generate JWT token using the username
          return new ResponseEntity<>(jwtService.generateToken(userDetails.getUsername()), HttpStatus.OK);
      }
  }