Skip to content

Commit

Permalink
Merge :: 구글 연동 해제 & 리프레시 에러 해결 (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
seu1a authored Oct 25, 2024
2 parents fd2273b + 61182ea commit 7e39838
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.seugi.api.domain.oauth.adapter.`in`.controller

import com.seugi.api.domain.oauth.port.`in`.GoogleRemoveUseCase
import com.seugi.api.global.common.annotation.GetAuthenticatedId
import com.seugi.api.global.response.BaseResponse
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/oauth/google")
class GoogleRemoveController (
private val service: GoogleRemoveUseCase
) {

@DeleteMapping("/remove")
fun remove(@GetAuthenticatedId userId: Long): BaseResponse<Unit> {
service.remove(userId)
return BaseResponse(message = "삭제 성공 !")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.seugi.api.domain.oauth.adapter.out.mapper.OAuthMapper
import com.seugi.api.domain.oauth.adapter.out.repository.OAuthRepository
import com.seugi.api.domain.oauth.application.exception.OAuthErrorCode
import com.seugi.api.domain.oauth.application.model.OAuth
import com.seugi.api.domain.oauth.port.out.DeleteOAuthPort
import com.seugi.api.domain.oauth.port.out.ExistOAuthPort
import com.seugi.api.domain.oauth.port.out.LoadOAuthPort
import com.seugi.api.domain.oauth.port.out.SaveOAuthPort
Expand All @@ -19,7 +20,7 @@ class OAuthAdapter (
private val oAuthRepository: OAuthRepository,
private val memberRepository: MemberRepository,
private val oAuthMapper: OAuthMapper
): LoadOAuthPort, ExistOAuthPort, SaveOAuthPort {
): LoadOAuthPort, ExistOAuthPort, SaveOAuthPort, DeleteOAuthPort {

override fun loadOAuthByMemberIdAndProvider(memberId: Long, provider: Provider): OAuth {
val member = memberRepository.findByIdOrNull(memberId)
Expand Down Expand Up @@ -51,4 +52,10 @@ class OAuthAdapter (
)
}

override fun deleteOAuth(memberId: Long, provider: Provider) {
val member = memberRepository.findByIdOrNull(memberId)
?: throw CustomException(MemberErrorCode.MEMBER_NOT_FOUND)

oAuthRepository.deleteByMemberAndProvider(member, provider)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ interface OAuthCustomRepository {
fun findByMemberAndProvider(member: MemberEntity, provider: Provider): OAuthEntity?
fun findByProviderAndSub(provider: Provider, sub: String): OAuthEntity?
fun existsByMemberAndProvider(member: MemberEntity, provider: Provider): Boolean
fun deleteByMemberAndProvider(member: MemberEntity, provider: Provider)

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ interface OAuthRepository : CrudRepository<OAuthEntity, Long>, OAuthCustomReposi
override fun findByMemberAndProvider(member: MemberEntity, provider: Provider): OAuthEntity?
override fun findByProviderAndSub(provider: Provider, sub: String): OAuthEntity?
override fun existsByMemberAndProvider(member: MemberEntity, provider: Provider): Boolean
override fun deleteByMemberAndProvider(member: MemberEntity, provider: Provider)

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,15 @@ class OAuthRepositoryCustomImpl (
.fetchOne() != null
}

override fun deleteByMemberAndProvider(member: MemberEntity, provider: Provider) {
val entity = QOAuthEntity.oAuthEntity

jpaQueryFactory
.delete(entity)
.where(
entity.member.eq(member),
entity.provider.eq(provider)
)
.execute()
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.seugi.api.domain.oauth.application.service

import com.google.api.client.auth.oauth2.TokenResponseException
import com.google.api.client.json.gson.GsonFactory
import com.seugi.api.global.auth.oauth.google.GoogleProperties
import com.google.api.client.googleapis.auth.oauth2.GoogleRefreshTokenRequest
import com.google.api.client.googleapis.json.GoogleJsonResponseException
import com.google.api.client.http.javanet.NetHttpTransport
import com.seugi.api.domain.oauth.application.exception.OAuthErrorCode
import com.seugi.api.domain.oauth.application.model.OAuth
Expand Down Expand Up @@ -32,7 +32,7 @@ class GoogleRefreshService (
oauth.accessToken = OAuthAccessToken(accessToken)

saveOAuthPort.saveOAuth(oauth)
} catch (e: GoogleJsonResponseException) {
} catch (e: TokenResponseException) {
throw CustomException(OAuthErrorCode.OAUTH_REFRESH_EXPIRED)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.seugi.api.domain.oauth.application.service

import com.seugi.api.domain.oauth.application.exception.OAuthErrorCode
import com.seugi.api.domain.oauth.port.`in`.GoogleRemoveUseCase
import com.seugi.api.domain.oauth.port.out.DeleteOAuthPort
import com.seugi.api.domain.oauth.port.out.ExistOAuthPort
import com.seugi.api.global.auth.oauth.enums.Provider
import com.seugi.api.global.exception.CustomException
import jakarta.transaction.Transactional
import org.springframework.stereotype.Service

@Service
class GoogleRemoveService (
private val existOAuthPort: ExistOAuthPort,
private val deleteOAuthPort: DeleteOAuthPort
) : GoogleRemoveUseCase {

@Transactional
override fun remove(userId: Long) {
if (!existOAuthPort.existOAuthByMemberIdAndProvider(userId, Provider.GOOGLE)) {
throw CustomException(OAuthErrorCode.OAUTH_NOT_FOUND)
}

deleteOAuthPort.deleteOAuth(userId, Provider.GOOGLE)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.seugi.api.domain.oauth.port.`in`

interface GoogleRemoveUseCase {

fun remove(userId: Long)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.seugi.api.domain.oauth.port.out

import com.seugi.api.global.auth.oauth.enums.Provider

interface DeleteOAuthPort {

fun deleteOAuth(memberId: Long, provider: Provider)

}

0 comments on commit 7e39838

Please sign in to comment.