Skip to content

Commit

Permalink
카카오 로그인 설정 (#92)
Browse files Browse the repository at this point in the history
* 카카오 로그인 설정

* 카카오 로그인 이슈 수정

* 코드리뷰 반영
  • Loading branch information
l2hyunwoo authored Jul 7, 2023
1 parent f510097 commit df319a5
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ import javax.inject.Inject
class AuthInterceptor @Inject constructor(
private val dataStore: PophoryDataStore
) : Interceptor {
private val encodedToken: String
get() = "Bearer ${dataStore.accessToken}"

override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()
if (!shouldRequestAuthenticatedHeaders(originalRequest.url.encodedPath)) {
return chain.proceed(originalRequest)
}
val headerRequest = originalRequest.newBuilder()
.header("Authorization", "Bearer ${dataStore.accessToken}")
.header("Authorization", encodedToken)
.build()
return chain.proceed(headerRequest)
}

private fun shouldRequestAuthenticatedHeaders(encodedPath: String) = when (encodedPath) {
"/api/v1/auth" -> false
else -> true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class AuthResponse(
val accessToken: String,
@SerialName("refreshToken")
val refreshToken: String,
@SerialName("isRegistered")
@SerialName("registered")
val isRegistered: Boolean
) {
fun toUserAuthentication() = UserAuthentication(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.teampophory.pophory.data.network.model.auth

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class SocialType(
@SerialName("socialType")
val socialType: String
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.teampophory.pophory.data.network.service

import com.teampophory.pophory.data.network.model.auth.AuthResponse
import com.teampophory.pophory.data.network.model.auth.SocialType
import retrofit2.http.Body
import retrofit2.http.Header
import retrofit2.http.POST
import retrofit2.http.PUT
Expand All @@ -9,6 +11,7 @@ interface AuthService {
@POST("api/v1/auth")
suspend fun login(
@Header("Authorization") authorization: String,
@Body socialType: SocialType
): AuthResponse

@PUT("api/v1/auth")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.teampophory.pophory.data.repository.auth
import com.teampophory.pophory.data.local.PophoryDataStore
import com.teampophory.pophory.data.model.auth.Token
import com.teampophory.pophory.data.model.auth.UserAuthentication
import com.teampophory.pophory.data.network.model.auth.SocialType
import com.teampophory.pophory.data.network.service.AuthService
import javax.inject.Inject

Expand All @@ -12,7 +13,7 @@ class DefaultAuthRepository @Inject constructor(
) : AuthRepository {
override suspend fun login(socialToken: String): UserAuthentication {
val authorization = "Bearer $socialToken"
return service.login(authorization).toUserAuthentication()
return service.login(authorization, SocialType("KAKAO")).toUserAuthentication()
}

override fun save(token: Token) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package com.teampophory.pophory.feature.auth.model

typealias KakaoToken = com.kakao.sdk.auth.model.OAuthToken

sealed interface OAuthToken
sealed interface OAuthToken {
val accessToken: String
}

data class KakaoOAuthToken(
val accessToken: String,
override val accessToken: String,
val refreshToken: String,
) : OAuthToken

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.teampophory.pophory.feature.home

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
Expand Down Expand Up @@ -63,4 +65,12 @@ class HomeActivity : AppCompatActivity() {
replace(R.id.home_fcv, fragment)
}
}


companion object {
@JvmStatic
fun getIntent(context: Context) = Intent(context, HomeActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
package com.teampophory.pophory.feature.onboarding

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2
import com.teampophory.pophory.R
import com.teampophory.pophory.common.context.snackBar
import com.teampophory.pophory.common.view.viewBinding
import com.teampophory.pophory.config.di.qualifier.Kakao
import com.teampophory.pophory.data.model.auth.UserAccountState
import com.teampophory.pophory.databinding.ActivityOnBoardingBinding
import com.teampophory.pophory.domain.AuthUseCase
import com.teampophory.pophory.feature.auth.social.OAuthService
import com.teampophory.pophory.feature.home.HomeActivity
import com.teampophory.pophory.feature.onboarding.adapter.OnBoardingViewPagerAdapter
import com.teampophory.pophory.feature.signup.SignUpActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

@AndroidEntryPoint
class OnBoardingActivity : AppCompatActivity() {

private val binding by viewBinding(ActivityOnBoardingBinding::inflate)
private lateinit var viewPager: ViewPager2
private lateinit var adapter: OnBoardingViewPagerAdapter

@Inject
@Kakao
lateinit var kakaoAuthService: OAuthService

@Inject
lateinit var authUseCase: AuthUseCase

override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(binding.root)
setViewPager()
clickKakaoLoginBtn()
setOnLoginPressed()
}

private val pageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
Expand All @@ -41,27 +54,46 @@ class OnBoardingActivity : AppCompatActivity() {
}
}

// TODO 카카오 로그인으로 수정 예정
private fun clickKakaoLoginBtn() {
private fun setOnLoginPressed() {
binding.btnStartSocialLogin.setOnClickListener {
val intent = Intent(this, SignUpActivity::class.java)
startActivity(intent)
lifecycleScope.launch {
runCatching {
kakaoAuthService.login()
}.onSuccess { token ->
authUseCase(token.accessToken)
.onSuccess { state ->
when (state) {
UserAccountState.REGISTERED -> {
startActivity(HomeActivity.getIntent(this@OnBoardingActivity))
}

UserAccountState.UNREGISTERED -> {
val intent =
Intent(this@OnBoardingActivity, SignUpActivity::class.java)
startActivity(intent)
}
}
}
.onFailure {
Timber.e(it)
snackBar(binding.root) { "로그인에 실패했습니다." }
}
}.onFailure(Timber::e)
}
}
}

private fun setViewPager() {
viewPager = binding.viewpagerOnboarding
adapter = OnBoardingViewPagerAdapter()
val adapter = OnBoardingViewPagerAdapter()

// TODO 이미지로 수정 예정
adapter.submitList(
mutableListOf(
OnBoardingData(R.drawable.img_onboarding01),
OnBoardingData(R.drawable.img_onboarding02),
OnBoardingData(R.drawable.img_onboarding03)
)
)
viewPager.adapter = adapter
viewPager.registerOnPageChangeCallback(pageChangeCallback)
binding.viewpagerOnboarding.adapter = adapter
binding.viewpagerOnboarding.registerOnPageChangeCallback(pageChangeCallback)
}
}

0 comments on commit df319a5

Please sign in to comment.