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

카카오 로그인 설정 #92

Merged
merged 3 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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,9 +10,17 @@ class AuthInterceptor @Inject constructor(
) : Interceptor {
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}")
.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
kez-lab marked this conversation as resolved.
Show resolved Hide resolved
): 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()
kez-lab marked this conversation as resolved.
Show resolved Hide resolved
}

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)
kez-lab marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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)
}
}