Skip to content

Commit

Permalink
[Merge] #69 -> develop
Browse files Browse the repository at this point in the history
[Fix] errorcode ์ถ”์ถœ ๋ฐฉ๋ฒ• ์ˆ˜์ •
  • Loading branch information
chattymin authored Jan 10, 2024
2 parents 70e0c49 + fa004bb commit a42301b
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 25 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/com/going/doorip/di/RetrofitModule.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.going.doorip.di

import com.going.data.interceptor.AuthInterceptor
import com.going.doorip.BuildConfig.BASE_URL
import com.going.doorip.di.qualifier.JWT
import com.going.ui.extension.isJsonArray
import com.going.ui.extension.isJsonObject
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import dagger.Module
import dagger.Provides
Expand Down Expand Up @@ -79,6 +82,3 @@ object RetrofitModule {
.addConverterFactory(factory)
.build()
}

fun String?.isJsonObject(): Boolean = this?.startsWith("{") == true && this.endsWith("}")
fun String?.isJsonArray(): Boolean = this?.startsWith("[") == true && this.endsWith("]")
5 changes: 5 additions & 0 deletions core-ui/src/main/java/com/going/ui/extension/StringExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.going.ui.extension

fun String?.isJsonObject(): Boolean = this?.startsWith("{") == true && this.endsWith("}")

fun String?.isJsonArray(): Boolean = this?.startsWith("[") == true && this.endsWith("]")
9 changes: 8 additions & 1 deletion data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ android {

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")

buildConfigField(
"String",
"BASE_URL",
com.android.build.gradle.internal.cxx.configure.gradleLocalProperties(rootDir)
.getProperty("base.url"),
)
}

compileOptions {
Expand Down Expand Up @@ -60,4 +67,4 @@ dependencies {
androidTestImplementation(androidTest)
androidTestImplementation(espresso)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.going.doorip.di
package com.going.data.interceptor

import android.content.Context
import com.going.data.BuildConfig.BASE_URL
import com.going.data.dto.BaseResponse
import com.going.data.local.GoingDataStore
import com.going.domain.entity.response.AuthTokenModel
import com.going.doorip.BuildConfig.BASE_URL
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.serialization.json.Json
import okhttp3.Interceptor
import okhttp3.Request
Expand All @@ -17,8 +15,8 @@ import javax.inject.Inject
class AuthInterceptor @Inject constructor(
private val json: Json,
private val dataStore: GoingDataStore,
@ApplicationContext private val context: Context,
) : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()

Expand All @@ -34,9 +32,17 @@ class AuthInterceptor @Inject constructor(
when (response.code) {
CODE_TOKEN_EXPIRED -> {
try {
// refreshToken์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ ์ธํ„ฐ์…‰ํ„ฐ ๋‚ด๋ถ€์—์„œ ์ง์ ‘์ ์œผ๋กœ ํ†ต์‹ ์„ ํ•˜๋Š”๊ฒŒ ๋งž์„๊นŒ?
// ์ง์ ‘ ํ†ต์‹ ํ•˜๋Š”๊ฒŒ ๊ธฐ์กด์˜ ํ†ต์‹ ๋ฐฉ๋ฒ•๊ณผ ์ฐจ์ด๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ†ต์ผ์„ฑ์„ ํ•ด์นœ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ฆ
// ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ๋˜๋ฉด Base Url์ด App๋ชจ๋“ˆ์—๋„ ํ•„์š”ํ•˜๊ณ , Data๋ชจ๋“ˆ์—๋„ ํ•„์š”ํ•จ
// ๊ทธ๋ž˜์„œ ์ง€๊ธˆ ์ƒ๊ฐ์œผ๋ก  Retrofit ๊ด€๋ จ ๋ชจ๋“ˆ์ด data ๋ชจ๋“ˆ์— ์žˆ์–ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์Œ
// ์•ฑ์žผ ๊ธฐ๊ฐ„๋‚ด์— ์ˆ˜์ •ํ•˜๋Š”๊ฒŒ ๋ชฉํ‘œ๋ผ ์ฃผ์„์„ ๋‚จ๊ฒจ๋‘๊ฒ ์Šต๋‹ˆ๋‹ค. ์ˆ˜์ • ๋ชปํ• ๊ฑฐ ๊ฐ™์œผ๋ฉด ์ฃผ์„๋งŒ ์ง€์šธ๊ฒŒ์šฉ
val refreshTokenRequest = originalRequest.newBuilder().post("".toRequestBody())
.url("$BASE_URL/api/users/reissue")
.addHeader(AUTHORIZATION, dataStore.refreshToken)
.addHeader(
AUTHORIZATION,
dataStore.refreshToken,
)
.build()
val refreshTokenResponse = chain.proceed(refreshTokenRequest)
Timber.d("GET REFRESH TOKEN : $refreshTokenResponse")
Expand All @@ -46,15 +52,30 @@ class AuthInterceptor @Inject constructor(
refreshTokenResponse.body?.string().toString(),
) as BaseResponse<AuthTokenModel>

with(dataStore) {
dataStore.apply {
accessToken = responseToken.data.accessToken
refreshToken = responseToken.data.refreshToken
}

refreshTokenResponse.close()

val newRequest = originalRequest.newAuthBuilder().build()
return chain.proceed(newRequest)
}

dataStore.apply {
accessToken = ""
refreshToken = ""
}

// refreshToken ๋งŒ๋ฃŒ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ ๋งŒ๋ฃŒ ์ฝ”๋“œ ํฌํ•จ ๋ฆฌ์Šคํฐ์Šค ๋ฆฌํ„ด
return refreshTokenResponse
} catch (t: Throwable) {
dataStore.apply {
accessToken = ""
refreshToken = ""
}

Timber.e(t)
}
}
Expand All @@ -63,10 +84,11 @@ class AuthInterceptor @Inject constructor(
}

private fun Request.newAuthBuilder() =
this.newBuilder().addHeader(AUTHORIZATION, "Bearer ${dataStore.accessToken}")
this.newBuilder().addHeader(AUTHORIZATION, "$BEARER ${dataStore.accessToken}")

companion object {
private const val CODE_TOKEN_EXPIRED = 401
private const val BEARER = "Bearer"
private const val AUTHORIZATION = "Authorization"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ interface TokenRepository {
fun getRefreshToken(): String

fun setTokens(accessToken: String, refreshToken: String)

fun clearTokens()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.going.presentation.R
import com.going.presentation.databinding.ActivitySigninBinding
import com.going.presentation.onboarding.signup.OnboardingProfileSettingActivity
import com.going.presentation.tendencytest.TendencyTestActivity
import com.going.presentation.tendencytest.TendencyTestSplashActivity
import com.going.ui.base.BaseActivity
import com.going.ui.extension.setOnSingleClickListener
import com.going.ui.extension.toast
Expand Down Expand Up @@ -84,7 +85,7 @@ class SignInActivity : BaseActivity<ActivitySigninBinding>(R.layout.activity_sig
}

private fun navigateToTendencyScreen() {
Intent(this, TendencyTestActivity::class.java).apply {
Intent(this, TendencyTestSplashActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class SplashActivity : BaseActivity<ActivitySplashBinding>(R.layout.activity_spl
if (viewModel.getHasAccessToken()) {
navigateToMainScreen()
} else {
// api ํ˜ธ์ถœ๋กœ ๋ณ€๊ฒฝ ์˜ˆ์ •
navigateToSignInScreen()
}
}, 3000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ class SplashViewModel @Inject constructor(
private val tokenRepository: TokenRepository,
) : ViewModel() {
fun getHasAccessToken(): Boolean = tokenRepository.getAccessToken().isNotBlank()

fun clearTokens() = tokenRepository.clearTokens()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.going.presentation.tendencytest

import android.content.Intent
import android.os.Bundle
import com.going.presentation.R
import com.going.presentation.databinding.ActivityTendencyTestSplashBinding
Expand All @@ -16,7 +17,13 @@ class TendencyTestSplashActivity :

private fun initStartBtnClickListener() {
binding.btnTendencySplashStart.setOnSingleClickListener {
// ํŽ˜์ด์ง€ ์ด๋™~
navigateToTendencyTestScreen()
}
}

private fun navigateToTendencyTestScreen() {
Intent(this, TendencyTestActivity::class.java).apply {
startActivity(this)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.going.presentation.util

import android.util.Log
import org.json.JSONObject
import retrofit2.HttpException

fun toErrorCode(throwable: Throwable): String = if (throwable is HttpException) {
val jsonTemp = throwable.response()?.errorBody()?.byteString().toString()
Log.e("TAG", "toErrorCode: $jsonTemp", )
val json = jsonTemp.slice(6 until jsonTemp.length - 2) + "}"
Log.e("TAG", "toErrorCode: $json", )
JSONObject(json).getString("code")
val codeIndex = jsonTemp.indexOf("code")
var errorCode = jsonTemp.slice(codeIndex + 7..codeIndex + 12)

if (errorCode.endsWith('\"')) errorCode = errorCode.slice(0 until errorCode.length - 1)

errorCode
} else {
"NOT_HTTP"
}

// ๊ธฐ๋Šฅ ์˜ค๋ฅ˜ ๋ฐœ๊ฒฌ!!!! ๋ฐ˜๋“œ์‹œ ์ˆ˜์ • ํ•„์š”!!!!!!
2 changes: 1 addition & 1 deletion presentation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<string name="will_be_update">ํ•ด๋‹น ๊ธฐ๋Šฅ์€ ์ถ”ํ›„ ์—…๋ฐ์ดํŠธ ์˜ˆ์ •์ด์—์š” :)</string>

<string name="sign_in_tv_title">์—ฌํ–‰์„ ์‹œ์ž‘ํ•ด๋ณด์„ธ์š”</string>
<string name="sign_in_tv_terms"><u>๊ฐœ์ธ์ •๋ณด์ฒ˜๋ฆฌ๋ฐฉ์นจ</u></string>
<string name="sign_in_tv_terms">๊ฐœ์ธ์ •๋ณด์ฒ˜๋ฆฌ๋ฐฉ์นจ</string>

<string name="counter">%1$d/%2$d</string>

Expand Down

0 comments on commit a42301b

Please sign in to comment.