Skip to content

Commit

Permalink
📝 when new social user is created its given a random 8 length nickname
Browse files Browse the repository at this point in the history
  • Loading branch information
jafacode committed Jan 31, 2025
1 parent 789834c commit b6623b8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -86,7 +85,7 @@ class CustomAuthenticationSuccessHandler(
}
}

private fun extractName(
/*private fun extractName(
attributes: Map<String, Any>,
provider: String,
): String {
Expand All @@ -100,5 +99,5 @@ class CustomAuthenticationSuccessHandler(
}
else -> "Unknown"
}
}
}*/
}
33 changes: 27 additions & 6 deletions src/main/kotlin/com/toyProject7/karrot/user/service/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,39 +108,60 @@ 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",
temperature = 36.5,
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()
Expand Down

0 comments on commit b6623b8

Please sign in to comment.