@@ -40,6 +40,7 @@ import com.wafflestudio.snutt.users.repository.UserRepository
4040import org.slf4j.LoggerFactory
4141import org.springframework.aot.hint.annotation.RegisterReflectionForBinding
4242import org.springframework.context.ApplicationEventPublisher
43+ import org.springframework.dao.DuplicateKeyException
4344import org.springframework.data.redis.core.ReactiveStringRedisTemplate
4445import org.springframework.data.redis.core.getAndAwait
4546import org.springframework.stereotype.Service
@@ -181,7 +182,7 @@ class UserServiceImpl(
181182 override suspend fun registerLocal (localRegisterRequest : LocalRegisterRequest ): LoginResponse {
182183 val localId = localRegisterRequest.id
183184 val password = localRegisterRequest.password
184- val email = localRegisterRequest.email
185+ val email = localRegisterRequest.email?.trim()
185186
186187 val cacheKey = CacheKey .LOCK_REGISTER_LOCAL .build(localId)
187188
@@ -191,7 +192,7 @@ class UserServiceImpl(
191192 if (! authService.isValidPassword(password)) throw InvalidPasswordException
192193 email?.let {
193194 if (! authService.isValidEmail(email)) throw InvalidEmailException
194- userRepository.findByEmailIgnoreCaseAndIsEmailVerifiedTrueAndActiveTrue (email)?.let {
195+ userRepository.findByEmailAndIsEmailVerifiedTrueAndActiveTrue (email)?.let {
195196 throw DuplicateEmailException (getAttachedAuthProviders(it))
196197 }
197198 }
@@ -235,7 +236,7 @@ class UserServiceImpl(
235236 val credential = authService.buildFacebookCredential(oauth2UserResponse)
236237
237238 if (oauth2UserResponse.email != null ) {
238- userRepository.findByEmailIgnoreCaseAndIsEmailVerifiedTrueAndActiveTrue (oauth2UserResponse.email)?.let {
239+ userRepository.findByEmailAndIsEmailVerifiedTrueAndActiveTrue (oauth2UserResponse.email)?.let {
239240 throw DuplicateEmailException (getAttachedAuthProviders(it))
240241 }
241242 } else {
@@ -259,7 +260,7 @@ class UserServiceImpl(
259260 }
260261
261262 checkNotNull(oauth2UserResponse.email) { " google email is null: $oauth2UserResponse " }
262- userRepository.findByEmailIgnoreCaseAndIsEmailVerifiedTrueAndActiveTrue (oauth2UserResponse.email)?.let {
263+ userRepository.findByEmailAndIsEmailVerifiedTrueAndActiveTrue (oauth2UserResponse.email)?.let {
263264 throw DuplicateEmailException (getAttachedAuthProviders(it))
264265 }
265266
@@ -282,7 +283,7 @@ class UserServiceImpl(
282283 }
283284
284285 checkNotNull(oauth2UserResponse.email) { " kakao email is null: $oauth2UserResponse " }
285- userRepository.findByEmailIgnoreCaseAndIsEmailVerifiedTrueAndActiveTrue (oauth2UserResponse.email)?.let {
286+ userRepository.findByEmailAndIsEmailVerifiedTrueAndActiveTrue (oauth2UserResponse.email)?.let {
286287 throw DuplicateEmailException (getAttachedAuthProviders(it))
287288 }
288289
@@ -309,7 +310,7 @@ class UserServiceImpl(
309310 }
310311
311312 checkNotNull(oauth2UserResponse.email) { " apple email is null: $oauth2UserResponse " }
312- userRepository.findByEmailIgnoreCaseAndIsEmailVerifiedTrueAndActiveTrue (oauth2UserResponse.email)?.let {
313+ userRepository.findByEmailAndIsEmailVerifiedTrueAndActiveTrue (oauth2UserResponse.email)?.let {
313314 throw DuplicateEmailException (getAttachedAuthProviders(it))
314315 }
315316
@@ -349,6 +350,7 @@ class UserServiceImpl(
349350 isEmailVerified : Boolean ,
350351 ): LoginResponse {
351352 val credentialHash = authService.generateCredentialHash(credential)
353+ val email = email?.trim()
352354
353355 val randomNickname = userNicknameService.generateUniqueRandomNickname()
354356
@@ -384,12 +386,10 @@ class UserServiceImpl(
384386 user : User ,
385387 email : String ,
386388 ) {
389+ val email = email.trim()
387390 if (user.isEmailVerified == true ) throw EmailAlreadyVerifiedException
388391 if (! authService.isValidSnuMail(email)) throw InvalidEmailException
389- if (userRepository.existsByEmailIgnoreCaseAndIsEmailVerifiedTrueAndActiveTrue(
390- email,
391- )
392- ) {
392+ if (userRepository.existsByEmailAndIsEmailVerifiedTrueAndActiveTrue(email)) {
393393 throw DuplicateEmailException (getAttachedAuthProviders(user))
394394 }
395395 val key = VERIFICATION_CODE_PREFIX + user.id
@@ -408,7 +408,12 @@ class UserServiceImpl(
408408 email = value.email
409409 isEmailVerified = true
410410 }
411- userRepository.save(user)
411+ try {
412+ userRepository.save(user)
413+ } catch (e: DuplicateKeyException ) {
414+ val presentUser = userRepository.findByEmailAndIsEmailVerifiedTrueAndActiveTrue(value.email)
415+ throw DuplicateEmailException (getAttachedAuthProviders(presentUser ? : user))
416+ }
412417 redisTemplate.delete(key).subscribe()
413418 }
414419
@@ -445,7 +450,7 @@ class UserServiceImpl(
445450 val token = socialLoginRequest.token
446451 val oauth2UserResponse = authService.socialLoginWithAccessToken(authProvider, token)
447452 if (oauth2UserResponse.email != null ) {
448- val presentUser = userRepository.findByEmailIgnoreCaseAndIsEmailVerifiedTrueAndActiveTrue (oauth2UserResponse.email)
453+ val presentUser = userRepository.findByEmailAndIsEmailVerifiedTrueAndActiveTrue (oauth2UserResponse.email)
449454 if (presentUser != null && presentUser.id != user.id) {
450455 throw DuplicateEmailException (getAttachedAuthProviders(presentUser))
451456 }
@@ -572,6 +577,7 @@ class UserServiceImpl(
572577 }
573578
574579 override suspend fun sendLocalIdToEmail (email : String ) {
580+ val email = email.trim()
575581 val users = userRepository.findAllByEmail(email).filter { it.active }
576582 if (users.isEmpty()) throw UserNotFoundException
577583
@@ -608,8 +614,9 @@ class UserServiceImpl(
608614 }
609615
610616 override suspend fun sendResetPasswordCode (email : String ) {
617+ val email = email.trim()
611618 if (email.replace(emailMaskRegex, " *" ) == email) throw UpdateAppVersionException
612- val user = userRepository.findByEmailIgnoreCaseAndIsEmailVerifiedTrueAndActiveTrue (email) ? : throw UserNotFoundException
619+ val user = userRepository.findByEmailAndIsEmailVerifiedTrueAndActiveTrue (email) ? : throw UserNotFoundException
613620 val key = RESET_PASSWORD_CODE_PREFIX + user.id
614621 val code = Base64 .getUrlEncoder().encodeToString(Random .nextBytes(6 ))
615622 saveNewVerificationValue(email, code, key)
@@ -648,7 +655,7 @@ class UserServiceImpl(
648655 redisTemplate.delete(RESET_PASSWORD_CODE_PREFIX + user.id).subscribe()
649656 }
650657
651- override suspend fun getUsersByEmail (email : String ): List <User > = userRepository.findAllByEmail(email)
658+ override suspend fun getUsersByEmail (email : String ): List <User > = userRepository.findAllByEmail(email.trim() )
652659
653660 private suspend fun saveNewVerificationValue (
654661 email : String ,
0 commit comments