Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] 카카오 자동 로그인 및 유저 정보 로직 수정 #164

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import com.jjbaksa.jjbaksa.base.BaseViewModel
import com.jjbaksa.jjbaksa.model.ShopContent
import com.jjbaksa.jjbaksa.util.MyInfo
import com.jjbaksa.jjbaksa.util.toShopContent
import com.kakao.sdk.user.UserApiClient
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
Expand Down Expand Up @@ -76,31 +74,16 @@ class HomeViewModel @Inject constructor(
MyInfo.reviews = homeRepository.getMyInfoReviews()
MyInfo.profileImage = homeRepository.getMyInfoProfileImage()
MyInfo.token = homeRepository.getMyInfoToken()
getKakaoInfo()
}
}
}
}

fun getKakaoInfo() {
UserApiClient.instance.me { user, error ->
if (error != null) {
Timber.tag("kakao_user_error").e(error)
} else if (user != null) {
if (homeRepository.getMyInfoAccount().isEmpty()) MyInfo.account =
user.kakaoAccount?.email.toString()
if (homeRepository.getMyInfoProfileImage().isEmpty()) MyInfo.profileImage =
user.kakaoAccount?.profile?.thumbnailImageUrl.toString()
if (homeRepository.getMyInfoNickname().isEmpty()) {
MyInfo.nickname = user.kakaoAccount?.profile?.nickname.toString()
}
}
}
}

fun clearDataStoreNoneAutoLogin() {
runBlocking {
userUseCase.clearDataStoreNoneAutoLogin()
if (!homeRepository.getMyInfoAutoLogin()) {
runBlocking {
userUseCase.logout()
}
}
}
}
1 change: 1 addition & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ android {

dependencies {
implementation(project(":domain"))
implementation("com.kakao.sdk:v2-user:2.11.0")
KotlinDependencies.run {
implementation(kotlin)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jjbaksa.data.repository

import android.net.Uri
import android.util.Log
import com.jjbaksa.data.datasource.local.UserLocalDataSource
import com.jjbaksa.data.datasource.remote.UserRemoteDataSource
import com.jjbaksa.data.mapper.FormDataUtil
Expand All @@ -21,7 +22,9 @@ import com.jjbaksa.domain.model.user.User
import com.jjbaksa.domain.model.user.UserList
import com.jjbaksa.domain.model.user.WithdrawalReasonReq
import com.jjbaksa.domain.repository.UserRepository
import com.kakao.sdk.user.UserApiClient
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.runBlocking
import javax.inject.Inject

class UserRepositoryImpl @Inject constructor(
Expand All @@ -32,8 +35,20 @@ class UserRepositoryImpl @Inject constructor(
runCatching {
userRemoteDataSource.postLoginSNS(token, snsType).toLoginResult()
}.onSuccess {
userLocalDataSource.saveAutoLogin(true)
userLocalDataSource.saveAccessToken(it.accessToken)
userLocalDataSource.saveRefreshToken(it.refreshToken)
UserApiClient.instance.me { user, error ->
if (error != null) {
Log.e("로그", "error $error")
} else if (user != null) {
runBlocking {
userLocalDataSource.saveAccount(user.kakaoAccount?.email.toString())
userLocalDataSource.saveProfileImage(user.kakaoAccount?.profile?.thumbnailImageUrl.toString())
userLocalDataSource.saveNickname(user.kakaoAccount?.profile?.nickname.toString())
}
}
}
}

override suspend fun getUserMe(): Flow<Result<User>> {
Expand All @@ -42,10 +57,14 @@ class UserRepositoryImpl @Inject constructor(
remoteData = {
if (it.isSuccessful) {
val user = it.body()?.toUser() ?: User()
userLocalDataSource.saveNickname(user.nickname)
if (userLocalDataSource.getNickname().isEmpty()) {
userLocalDataSource.saveNickname(user.nickname)
}
if (userLocalDataSource.getProfileImage().isEmpty()) {
userLocalDataSource.saveProfileImage(user.profileImage.url.toString())
}
userLocalDataSource.saveFollowers(user.userCountResponse.friendCount)
userLocalDataSource.saveReviews(user.userCountResponse.reviewCount)
userLocalDataSource.saveProfileImage(user.profileImage.url.toString())
}
},
mapper = {
Expand Down Expand Up @@ -322,12 +341,6 @@ class UserRepositoryImpl @Inject constructor(
userLocalDataSource.clearDataStore()
}

override suspend fun clearDataStoreNoneAutoLogin() {
if (!userLocalDataSource.getAutoLoginFlag()) {
userLocalDataSource.clearDataStore()
}
}

override fun getAutoLoginFlag(): Boolean {
return userLocalDataSource.getAutoLoginFlag()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ interface UserRepository {
suspend fun postUserEmailCheck(email: String): Flow<Result<Login>>

suspend fun logout()
suspend fun clearDataStoreNoneAutoLogin()
fun getAutoLoginFlag(): Boolean
fun getAccount(): String
fun getNickname(): String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ class UserUseCase @Inject constructor(
return userRepository.logout()
}

suspend fun clearDataStoreNoneAutoLogin() {
userRepository.clearDataStoreNoneAutoLogin()
}

suspend fun postUserWithdrawalReason(
withdrawalReason: WithdrawalReasonReq,
onError: (String) -> Unit
Expand Down
Loading