|
| 1 | +package com.alwinsimon.UserManagementJavaSpringBoot.Service; |
| 2 | + |
| 3 | +import com.alwinsimon.UserManagementJavaSpringBoot.Config.Auth.AuthenticationRequest; |
| 4 | +import com.alwinsimon.UserManagementJavaSpringBoot.Config.Auth.AuthenticationResponse; |
| 5 | +import com.alwinsimon.UserManagementJavaSpringBoot.Config.Auth.RegisterRequest; |
| 6 | +import com.alwinsimon.UserManagementJavaSpringBoot.Model.Role; |
| 7 | +import com.alwinsimon.UserManagementJavaSpringBoot.Model.User; |
| 8 | +import com.alwinsimon.UserManagementJavaSpringBoot.Repository.UserRepository; |
| 9 | +import lombok.RequiredArgsConstructor; |
| 10 | +import org.springframework.security.authentication.AuthenticationManager; |
| 11 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 12 | +import org.springframework.security.core.userdetails.UsernameNotFoundException; |
| 13 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 14 | +import org.springframework.stereotype.Service; |
| 15 | + |
| 16 | +@Service |
| 17 | +@RequiredArgsConstructor |
| 18 | +public class AuthenticationService { |
| 19 | + |
| 20 | + private final UserRepository userRepository; |
| 21 | + private final PasswordEncoder passwordEncoder; |
| 22 | + private final JwtService jwtService; |
| 23 | + private final AuthenticationManager authenticationManager; |
| 24 | + |
| 25 | + public AuthenticationResponse register(RegisterRequest request) { |
| 26 | + |
| 27 | + // Build a user using builder in user model. |
| 28 | + var user = User.builder() |
| 29 | + .name(request.getName()) |
| 30 | + .gender(request.getGender()) |
| 31 | + .email(request.getEmail()) |
| 32 | + .password(passwordEncoder.encode(request.getPassword())) |
| 33 | + .role(Role.USER) |
| 34 | + .build(); |
| 35 | + |
| 36 | + // Save User to DB using UserRepository |
| 37 | + userRepository.save(user); |
| 38 | + |
| 39 | + // Generate a JWT Token to return along with Response. |
| 40 | + var jwtToken = jwtService.generateJwtToken(user); |
| 41 | + |
| 42 | + return AuthenticationResponse.builder() |
| 43 | + .token(jwtToken) |
| 44 | + .build(); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + public AuthenticationResponse authenticate(AuthenticationRequest request) { |
| 49 | + |
| 50 | + // Try Authenticating user with Authentication Manager |
| 51 | + authenticationManager.authenticate( |
| 52 | + new UsernamePasswordAuthenticationToken( |
| 53 | + request.getEmail(), |
| 54 | + request.getPassword() |
| 55 | + ) |
| 56 | + ); |
| 57 | + |
| 58 | + /** |
| 59 | + * If the authentication manager authenticated user without throwing any exception |
| 60 | + * Find user and generate auth token |
| 61 | + * Send auth token back to user. |
| 62 | + */ |
| 63 | + |
| 64 | + var user = userRepository.findByEmail(request.getEmail()) |
| 65 | + .orElseThrow(() -> new UsernameNotFoundException("User not found.")); |
| 66 | + |
| 67 | + // Generate a JWT Token to return along with Response. |
| 68 | + var jwtToken = jwtService.generateJwtToken(user); |
| 69 | + |
| 70 | + return AuthenticationResponse.builder() |
| 71 | + .token(jwtToken) |
| 72 | + .build(); |
| 73 | + |
| 74 | + } |
| 75 | +} |
0 commit comments