diff --git a/app/build.gradle b/app/build.gradle index 9d1c672c0..50b8d3bf9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -19,8 +19,8 @@ android { applicationId "org.sopt.havit" minSdk 23 targetSdk 31 - versionCode 104 - versionName "1.0.4" + versionCode 105 + versionName "1.0.5" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" buildConfigField("String", "HAVIT_BASE_URL_DEV", properties["HAVIT_BASE_URL_DEV"]) buildConfigField("String", "HAVIT_BASE_URL_PROD", properties["HAVIT_BASE_URL_PROD"]) diff --git a/app/src/main/java/org/sopt/havit/di/ServiceModule.kt b/app/src/main/java/org/sopt/havit/di/ServiceModule.kt index 81d5df00a..688e29f07 100644 --- a/app/src/main/java/org/sopt/havit/di/ServiceModule.kt +++ b/app/src/main/java/org/sopt/havit/di/ServiceModule.kt @@ -15,4 +15,5 @@ object ServiceModule { @Singleton fun provideHavitService(retrofit: Retrofit): HavitApi = retrofit.create(HavitApi::class.java) + } diff --git a/app/src/main/java/org/sopt/havit/ui/sign/KakaoLoginService.kt b/app/src/main/java/org/sopt/havit/ui/sign/KakaoLoginService.kt index b7e3d985b..e2fe95eb6 100644 --- a/app/src/main/java/org/sopt/havit/ui/sign/KakaoLoginService.kt +++ b/app/src/main/java/org/sopt/havit/ui/sign/KakaoLoginService.kt @@ -5,6 +5,7 @@ import android.util.Log import com.kakao.sdk.auth.model.OAuthToken import com.kakao.sdk.auth.model.Prompt import com.kakao.sdk.user.UserApiClient +import com.kakao.sdk.user.model.User import dagger.hilt.android.qualifiers.ActivityContext import org.sopt.havit.data.local.HavitAuthLocalPreferences import javax.inject.Inject @@ -13,8 +14,6 @@ class KakaoLoginService @Inject constructor( @ActivityContext private val context: Context, private val preferences: HavitAuthLocalPreferences ) { - - fun setLoginWithAccount(kakaoLoginCallback: (OAuthToken?, Throwable?) -> Unit) { UserApiClient.instance.loginWithKakaoAccount( context, @@ -23,23 +22,6 @@ class KakaoLoginService @Inject constructor( ) } - private fun getKakaoUserInfo() { - UserApiClient.instance.me { user, error -> - if (error != null) { - Log.d("TAG", "사용자 정보 요청 실패", error) - } else if (user != null) { - Log.d("TAG", "사용자 정보 요청 성공") - val age = user.kakaoAccount?.ageRange.toString().split("_") - - preferences.userEmail = user.kakaoAccount?.email.toString() - preferences.userGender = user.kakaoAccount?.gender.toString() - preferences.userAge = (age[1].toInt() + age[2].toInt()) / 2 - preferences.userNickName = user.kakaoAccount?.profile?.nickname ?: "" - - } - } - } - fun setKakaoLogin(kakaoLoginCallback: (OAuthToken?, Throwable?) -> Unit) { if (UserApiClient.instance.isKakaoTalkLoginAvailable(context)) { UserApiClient.instance.loginWithKakaoTalk( @@ -54,7 +36,7 @@ class KakaoLoginService @Inject constructor( } } - fun getUserNeedNewScopes() { + fun getUserNeedNewScopes(isGetUserInfo: (Boolean) -> Unit) { UserApiClient.instance.me { user, error -> if (error != null) { Log.d("TAG", "사용자 정보 요청 실패", error) @@ -72,22 +54,42 @@ class KakaoLoginService @Inject constructor( if (scopes.isNotEmpty()) { Log.d("TAG", "사용자에게 추가 동의를 받아야 합니다.") // scope 목록을 전달하여 카카오 로그인 요청 - UserApiClient.instance.loginWithNewScopes( - context, - scopes - ) { _, error -> + UserApiClient.instance.loginWithNewScopes(context, scopes) { token, error -> if (error != null) { - Log.d("TAG", "사용자 추가 동의 실패", error) + Log.e("TAG", "사용자 추가 동의 실패", error) } else { + Log.d("TAG", "allowed scopes: ${token!!.scopes}") + // 사용자 정보 재요청 - getKakaoUserInfo() + UserApiClient.instance.me { user, error -> + if (error != null) { + Log.e("TAG", "사용자 정보 요청 실패", error) + isGetUserInfo(false) + } else if (user != null) { + Log.i("TAG", "사용자 정보 요청 성공") + getUserInfo(user) + isGetUserInfo(true) + } + } } } + } else { - getKakaoUserInfo() + getUserInfo(user) + isGetUserInfo(true) } } } } -} \ No newline at end of file + private fun getUserInfo(user: User) { + val age = user.kakaoAccount?.ageRange.toString().split("_") + + preferences.userEmail = user.kakaoAccount?.email.toString() + preferences.userGender = user.kakaoAccount?.gender.toString() + preferences.userAge = (age[1].toInt() + age[2].toInt()) / 2 + preferences.userNickName = user.kakaoAccount?.profile?.nickname ?: "" + } + + +} diff --git a/app/src/main/java/org/sopt/havit/ui/sign/SignInViewModel.kt b/app/src/main/java/org/sopt/havit/ui/sign/SignInViewModel.kt index 512b07785..9d3e6bc2e 100644 --- a/app/src/main/java/org/sopt/havit/ui/sign/SignInViewModel.kt +++ b/app/src/main/java/org/sopt/havit/ui/sign/SignInViewModel.kt @@ -33,9 +33,6 @@ class SignInViewModel @Inject constructor( private var _isAlreadyUser = MutableLiveData>() var isAlreadyUser: LiveData> = _isAlreadyUser - private var _isNeedScopes = MutableLiveData>() - var isNeedScopes: LiveData> = _isNeedScopes - private var _accessToken = MutableLiveData() var accessToken: LiveData = _accessToken @@ -48,12 +45,14 @@ class SignInViewModel @Inject constructor( val kakaoLoginCallback: (OAuthToken?, Throwable?) -> Unit = { token, error -> if (error != null) { - _isSuccessKakaoLogin.value = Event(false) + if (error.toString().contains("statusCode=302")) { + _isSuccessKakaoLogin.value = Event(false) + } + Log.d("TAG", "카카오계정으로 로그인 실패 ${error}") } else if (token != null) { Log.d("TAG", "카카오계정으로 로그인 성공 ${token.accessToken}") - setKakaoToken(token.accessToken) - _isNeedScopes.value = Event(true) _isSuccessKakaoLogin.value = Event(true) + setKakaoToken(token.accessToken) } } diff --git a/app/src/main/java/org/sopt/havit/ui/sign/SplashWithSignActivity.kt b/app/src/main/java/org/sopt/havit/ui/sign/SplashWithSignActivity.kt index d6b482566..87c1bbac7 100644 --- a/app/src/main/java/org/sopt/havit/ui/sign/SplashWithSignActivity.kt +++ b/app/src/main/java/org/sopt/havit/ui/sign/SplashWithSignActivity.kt @@ -7,7 +7,6 @@ import android.view.animation.Animation import android.view.animation.AnimationUtils import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels -import com.kakao.sdk.user.UserApiClient import dagger.hilt.android.AndroidEntryPoint import org.sopt.havit.MainActivity import org.sopt.havit.R @@ -28,12 +27,11 @@ import kotlin.properties.Delegates class SplashWithSignActivity : BaseBindingActivity(R.layout.activity_splash_with_sign) { - @Inject - lateinit var kakaoLoginService: KakaoLoginService + lateinit var preference: HavitSharedPreference @Inject - lateinit var preference: HavitSharedPreference + lateinit var kakaoLoginService: KakaoLoginService private val signInViewModel: SignInViewModel by viewModels() private var isFromShare by Delegates.notNull() @@ -57,26 +55,27 @@ class SplashWithSignActivity : setContentView(binding.root) binding.main = signInViewModel initFcmToken() - initSuccessKakaoLoginOserver() + initSuccessKakaoLoginObserver() initWhereSplashComesFrom() setLoginGuideIfFromShare() setSplashView() setListeners() isAlreadyUserObserver() - isNeedScopesObserver() } private fun initFcmToken() { signInViewModel.initFcmToken() } - private fun initSuccessKakaoLoginOserver() { - signInViewModel.isSuccessKakaoLogin.observe(this, EventObserver { - if (it) signInViewModel.getSignIn() - else UserApiClient.instance.loginWithKakaoAccount( - this, - callback = signInViewModel.kakaoLoginCallback - ) + private fun initSuccessKakaoLoginObserver() { + signInViewModel.isSuccessKakaoLogin.observe(this, EventObserver { isSuccess -> + if (isSuccess) { + kakaoLoginService.getUserNeedNewScopes { isGetUserInfo -> + if (isGetUserInfo) signInViewModel.getSignIn() + } + } else { + kakaoLoginService.setLoginWithAccount(signInViewModel.kakaoLoginCallback) + } }) } @@ -142,14 +141,10 @@ class SplashWithSignActivity : setAutoLogin() } binding.btnKakaoLogin.setOnSinglePostClickListener { - kakaoLoginService.setKakaoLogin( - signInViewModel.kakaoLoginCallback - ) + kakaoLoginService.setKakaoLogin(signInViewModel.kakaoLoginCallback) } binding.tvAnotherLogin.setOnClickListener { - kakaoLoginService.setLoginWithAccount( - signInViewModel.kakaoLoginCallback - ) + kakaoLoginService.setLoginWithAccount(signInViewModel.kakaoLoginCallback) } } @@ -165,12 +160,6 @@ class SplashWithSignActivity : ) } - private fun isNeedScopesObserver() { - signInViewModel.isNeedScopes.observe(this) { - kakaoLoginService.getUserNeedNewScopes() - } - } - private fun isAlreadyUserObserver() { signInViewModel.isAlreadyUser.observe( this,