feat: implement AuthService and AuthController
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
package com.example.bankcards.controller;
|
||||||
|
|
||||||
|
import com.example.bankcards.dto.AuthResponse;
|
||||||
|
import com.example.bankcards.dto.LoginRequest;
|
||||||
|
import com.example.bankcards.dto.RegisterRequest;
|
||||||
|
import com.example.bankcards.service.AuthService;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/auth")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class AuthController {
|
||||||
|
|
||||||
|
private final AuthService authService;
|
||||||
|
|
||||||
|
@PostMapping("/register")
|
||||||
|
public ResponseEntity<AuthResponse> register(
|
||||||
|
@Valid @RequestBody RegisterRequest request
|
||||||
|
) {
|
||||||
|
return ResponseEntity.ok(authService.register(request));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/login")
|
||||||
|
public ResponseEntity<AuthResponse> login(
|
||||||
|
@Valid @RequestBody LoginRequest request
|
||||||
|
) {
|
||||||
|
return ResponseEntity.ok(authService.login(request));
|
||||||
|
}
|
||||||
|
}
|
||||||
76
src/main/java/com/example/bankcards/service/AuthService.java
Normal file
76
src/main/java/com/example/bankcards/service/AuthService.java
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package com.example.bankcards.service;
|
||||||
|
|
||||||
|
import com.example.bankcards.dto.AuthResponse;
|
||||||
|
import com.example.bankcards.dto.LoginRequest;
|
||||||
|
import com.example.bankcards.dto.RegisterRequest;
|
||||||
|
import com.example.bankcards.entity.Role;
|
||||||
|
import com.example.bankcards.entity.User;
|
||||||
|
import com.example.bankcards.exception.BadRequestException;
|
||||||
|
import com.example.bankcards.repository.UserRepository;
|
||||||
|
import com.example.bankcards.security.JwtService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class AuthService {
|
||||||
|
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
private final JwtService jwtService;
|
||||||
|
private final AuthenticationManager authenticationManager;
|
||||||
|
private final UserDetailsService userDetailsService;
|
||||||
|
|
||||||
|
public AuthResponse register(RegisterRequest request) {
|
||||||
|
if (userRepository.existsByUsername(request.getUsername())) {
|
||||||
|
throw new BadRequestException("Username already taken");
|
||||||
|
}
|
||||||
|
if (userRepository.existsByEmail(request.getEmail())) {
|
||||||
|
throw new BadRequestException("Email already registered");
|
||||||
|
}
|
||||||
|
|
||||||
|
User user = User.builder()
|
||||||
|
.username(request.getUsername())
|
||||||
|
.email(request.getEmail())
|
||||||
|
.password(passwordEncoder.encode(request.getPassword()))
|
||||||
|
.role(Role.ROLE_USER)
|
||||||
|
.build();
|
||||||
|
userRepository.save(user);
|
||||||
|
|
||||||
|
UserDetails userDetails = userDetailsService.loadUserByUsername(
|
||||||
|
user.getUsername()
|
||||||
|
);
|
||||||
|
String token = jwtService.generateToken(userDetails);
|
||||||
|
return new AuthResponse(
|
||||||
|
token,
|
||||||
|
user.getUsername(),
|
||||||
|
user.getRole().name()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AuthResponse login(LoginRequest request) {
|
||||||
|
authenticationManager.authenticate(
|
||||||
|
new UsernamePasswordAuthenticationToken(
|
||||||
|
request.getUsername(),
|
||||||
|
request.getPassword()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
UserDetails userDetails = userDetailsService.loadUserByUsername(
|
||||||
|
request.getUsername()
|
||||||
|
);
|
||||||
|
String token = jwtService.generateToken(userDetails);
|
||||||
|
User user = userRepository
|
||||||
|
.findByUsername(request.getUsername())
|
||||||
|
.orElseThrow();
|
||||||
|
return new AuthResponse(
|
||||||
|
token,
|
||||||
|
user.getUsername(),
|
||||||
|
user.getRole().name()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user