diff --git a/src/main/kotlin/com/toyProject7/karrot/socialLogin/handler/CustomAuthenticationSuccessHandler.kt b/src/main/kotlin/com/toyProject7/karrot/socialLogin/handler/CustomAuthenticationSuccessHandler.kt index 5d84393..319c3f5 100644 --- a/src/main/kotlin/com/toyProject7/karrot/socialLogin/handler/CustomAuthenticationSuccessHandler.kt +++ b/src/main/kotlin/com/toyProject7/karrot/socialLogin/handler/CustomAuthenticationSuccessHandler.kt @@ -31,10 +31,9 @@ class CustomAuthenticationSuccessHandler( val attributes = oauth2User.attributes val providerId = extractProviderId(attributes, provider) val email = extractEmail(attributes, provider) - val name = extractName(attributes, provider) // Create or retrieve the user - val user = userService.createOrRetrieveSocialUser(email, providerId, provider, name) + val user = userService.createOrRetrieveSocialUser(email, providerId, provider) // Generate JWT val accessToken = UserAccessTokenUtil.generateAccessToken(user.id) @@ -86,7 +85,7 @@ class CustomAuthenticationSuccessHandler( } } - private fun extractName( + /*private fun extractName( attributes: Map, provider: String, ): String { @@ -100,5 +99,5 @@ class CustomAuthenticationSuccessHandler( } else -> "Unknown" } - } + }*/ } diff --git a/src/main/kotlin/com/toyProject7/karrot/user/service/UserService.kt b/src/main/kotlin/com/toyProject7/karrot/user/service/UserService.kt index bf5a8e5..5d1ef37 100644 --- a/src/main/kotlin/com/toyProject7/karrot/user/service/UserService.kt +++ b/src/main/kotlin/com/toyProject7/karrot/user/service/UserService.kt @@ -108,20 +108,30 @@ class UserService( email: String, providerId: String, provider: String, - username: String, ): User { // Check if the user exists by email val existingUser = userRepository.findSocialUserByEmail(email) return existingUser?.let { - // Convert existingUser (of type SocialUser) to User DTO + // If the user exists, convert to User DTO and return User.fromEntity(it) } ?: run { - // If the user doesn't exist, create a new one + // If the user doesn't exist, generate a unique random username + var username = generateRandomString() + var isUnique = false + + while (!isUnique) { + if (!userRepository.existsByNickname(username)) { + isUnique = true + } + username = generateRandomString() + } + + // Create a new SocialUser with the generated username val newUser = SocialUser( email = email, - nickname = username, + nickname = username, // Assign the generated username provider = provider, providerId = providerId, location = "void", @@ -129,18 +139,29 @@ class UserService( imageUrl = null, updatedAt = Instant.now(), ) - val savedUser = userRepository.save(newUser) // This should save as SocialUser + // Save the new user + val savedUser = userRepository.save(newUser) + + // Create and save the associated profile val profileEntity = ProfileEntity( user = newUser, ) profileService.saveProfileEntity(profileEntity) - User.fromEntity(savedUser) // Convert and return as User DTO + // Convert and return as User DTO + User.fromEntity(savedUser) } } + private fun generateRandomString(length: Int = 8): String { + val characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + return (1..length) + .map { characters.random() } + .joinToString("") + } + @Transactional fun getUserEntityById(id: String): UserEntity { return userRepository.findByIdOrNull(id) ?: throw UserNotFoundException()