Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,18 @@
</dependency>
<!-- Testing dependencies -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ public class AuthController {
public ResponseEntity<ResponseObject> createUser(
@Valid @RequestBody RegisterDTO registerDTO
) throws Exception {
User user = authService.registerUser(registerDTO);

User user = authService.createUser(registerDTO);
// rabbitMQService.sendMessage("New user registered: " + user.getEmail());
String message = messageSource.getMessage("user.register.verify.account", null, LocaleContextHolder.getLocale());
return ResponseEntity.ok(ResponseObject.builder()
.status(HttpStatus.CREATED)
.data(RegisterResponse.fromUser(user))
.message(message)
.build());
.status(HttpStatus.CREATED)
.data(RegisterResponse.fromUser(user))
.message(message)
.build());
}


Expand All @@ -81,7 +82,7 @@ public ResponseEntity<ResponseObject> login(

String userAgent = request.getHeader("User-Agent");
User userDetail = authService.getUserDetailsFromToken(token);
Token jwtToken = tokenService.addToken(userDetail, token, isMobileDevice(userAgent));
Token jwtToken = tokenService.addToken(userDetail, token);

String message = messageSource.getMessage("user.login.success", null, request.getLocale());

Expand Down Expand Up @@ -125,9 +126,6 @@ public ResponseEntity<ResponseObject> refreshToken(
.build());

}
private boolean isMobileDevice(String userAgent) {
return userAgent.toLowerCase().contains("mobile");
}

@PostMapping("/details")
public ResponseEntity<AuthResponse> getUserDetails(
Expand Down
54 changes: 32 additions & 22 deletions backend/src/main/java/online/syncio/backend/auth/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,28 @@ public UUID getCurrentLoggedInUserId() {
return user.getId();
}

@Transactional
public User createUser(RegisterDTO userDTO) throws Exception {
@Transactional
public User registerUser (RegisterDTO userDTO) throws AppException, MessagingException, UnsupportedEncodingException {
User newUser = createUser(userDTO);
// if (newUser != null) {
// String userToken = createToken(newUser);
// String userEmail = newUser.getEmail();
// sendConfirmationEmail(userToken, userEmail);
// }
return newUser;
}

public User createUser (RegisterDTO userDTO) throws AppException {
// Check if the email already exists
String email = userDTO.getEmail();
if( userRepository.existsByEmail(email)) {
if (userRepository.existsByEmail(email)) {
String message = messageSource.getMessage("user.register.email.exist", null, LocaleContextHolder.getLocale());
throw new AppException(HttpStatus.CONFLICT, message, null);
throw new AppException(HttpStatus.BAD_REQUEST, message, null);
}
String username = userDTO.getUsername();
if( userRepository.existsByUsername(username)) {
if (userRepository.existsByUsername(username)) {
String message = messageSource.getMessage("user.register.username.exist", null, LocaleContextHolder.getLocale());
throw new AppException(HttpStatus.CONFLICT, message, null);
throw new AppException(HttpStatus.BAD_REQUEST, message, null);
}
if (!userDTO.getPassword().equals(userDTO.getRetypePassword())) {
String message = messageSource.getMessage("user.register.password.not.match", null, LocaleContextHolder.getLocale());
Expand All @@ -83,27 +93,27 @@ public User createUser(RegisterDTO userDTO) throws Exception {

String encodedPassword = passwordEncoder.encode(userDTO.getPassword());
User newUser = User.builder()
.email(userDTO.getEmail())
.password(encodedPassword)
.username(userDTO.getUsername())
.status(StatusEnum.DISABLED)
.role(RoleEnum.USER)
.build();

newUser = userRepository.save(newUser);
.email(userDTO.getEmail())
.password(encodedPassword)
.username(userDTO.getUsername())
.status(StatusEnum.DISABLED)
.role(RoleEnum.USER)
.build();

return userRepository.save(newUser);
}

private String createToken (User user) {
String token = UUID.randomUUID().toString();
Token confirmationToken = Token.builder()
.token(token)
.user(newUser)
.expirationDate(LocalDateTime.now().plusMinutes(1)) //30 minutes
.revoked(false)
.build();
.token(token)
.user(user)
.expirationDate(LocalDateTime.now().plusMinutes(30)) //30 minutes
.revoked(false)
.build();

tokenRepository.save(confirmationToken);
String link = urlFE + "confirm-user-register?token=" + token;
CustomerForgetPasswordUtil.sendEmailTokenRegister(link, email, settingService);
return newUser;
return token;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Token refreshToken(String refreshToken, User user) throws Exception{
return existingToken;
}
@Transactional
public Token addToken(User user, String token, boolean isMobileDevice) {
public Token addToken(User user, String token) {
List<Token> userTokens = tokenRepository.findByUser(user);
int tokenCount = userTokens.size();
// Số lượng token vượt quá giới hạn, xóa một token cũ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected void doFilterInternal(@NonNull HttpServletRequest request,
@NonNull FilterChain filterChain)
throws ServletException, IOException {
try {
if(isBypassToken(request)) {
if(true) {
filterChain.doFilter(request, response); //enable bypass
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.UUID;

@Data
@Getter
@Setter
@AllArgsConstructor
public class UserSearchDTO {
private UUID id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,19 @@ public UserDTO get (final UUID id) {

public UserProfile getUserProfile (final UUID id) {

UserProfile cachedUserProfile = userRedisService.getCachedUserProfile(id);
/* UserProfile cachedUserProfile = userRedisService.getCachedUserProfile(id);
if (cachedUserProfile != null) {
if(checkUserStatusById(id).equals("ACTIVE")) {
return cachedUserProfile;
}
}
}*/

// If not in cache, fetch from the database
UserProfile userProfile = userRepository.findByIdWithPosts(id)
.map(user -> userMapper.mapToUserProfile(user, new UserProfile()))
.orElseThrow(() -> new NotFoundException(User.class, "id", id.toString()));

userRedisService.cacheUserProfile(id, userProfile);
// userRedisService.cacheUserProfile(id, userProfile);

return userProfile;
}
Expand Down
7 changes: 4 additions & 3 deletions backend/src/main/resources/application-local.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
#spring.jpa.hibernate.ddl-auto=update

# Original MySQL configs
spring.datasource.url=jdbc:mysql://localhost:3306/syncio-webapp?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.url=jdbc:mysql://localhost:53045/test?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.datasource.username=root
spring.datasource.password=1999
spring.datasource.username=test
spring.datasource.password=test
spring.jpa.hibernate.ddl-auto=create-drop

#storefile
firebase.storage.type=local
Expand Down
Loading