Skip to content

Commit

Permalink
[chore] #64 intent qualifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Sangwook123 committed Jan 7, 2024
1 parent ea9dd07 commit ee83f77
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import okhttp3.Request
import okhttp3.Response
import okhttp3.Route
import org.sopt.common.intentprovider.IntentProvider
import org.sopt.common.intentprovider.LOGIN
import org.sopt.datastore.datastore.SecurityDataStore
import org.sopt.network.service.TokenRefreshService
import javax.inject.Inject
Expand All @@ -18,7 +19,7 @@ class LinkMindAuthenticator @Inject constructor(
private val dataStore: SecurityDataStore,
private val tokenRefreshService: TokenRefreshService,
@ApplicationContext private val context: Context,
private val intentProvider: IntentProvider,
@LOGIN private val intentProvider: IntentProvider,
) : Authenticator {
override fun authenticate(route: Route?, response: Response): Request? {
if (response.code == CODE_TOKEN_EXPIRED) {
Expand All @@ -37,7 +38,7 @@ class LinkMindAuthenticator @Inject constructor(
runBlocking {
dataStore.clearAll()
}
ProcessPhoenix.triggerRebirth(context, intentProvider.getAuthIntent())
ProcessPhoenix.triggerRebirth(context, intentProvider.getIntent())
}.getOrThrow()

return response.request.newBuilder()
Expand All @@ -46,6 +47,7 @@ class LinkMindAuthenticator @Inject constructor(
}
return null
}

companion object {
const val CODE_TOKEN_EXPIRED = 401
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import org.sopt.common.intentprovider.IntentProvider
import org.sopt.common.intentprovider.LOGIN
import org.sopt.datastore.datastore.SecurityDataStore
import javax.inject.Inject

class AuthenticationIntercept @Inject constructor(
private val dataStore: SecurityDataStore,
@ApplicationContext private val context: Context,
private val intentProvider: IntentProvider,
@LOGIN private val intentProvider: IntentProvider,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()
val authRequest = handleRequest(originalRequest)
val response = chain.proceed(authRequest)
if (response.code == CODE_INVALID_USER) {
runBlocking { dataStore.setAutoLogin(false) }
ProcessPhoenix.triggerRebirth(context, intentProvider.getAuthIntent())
ProcessPhoenix.triggerRebirth(context, intentProvider.getIntent())
}
return response
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.sopt.common.intentprovider.IntentProvider
import org.sopt.common.intentprovider.MAIN
import org.sopt.datastore.datastore.SecurityDataStore
import org.sopt.login.databinding.ActivityLoginBinding
import org.sopt.maincontainer.MainActivity
import org.sopt.oauthdomain.interactor.OAuthInteractor
import org.sopt.ui.context.toast
import org.sopt.ui.view.UiState
Expand All @@ -31,6 +32,10 @@ class LoginActivity : AppCompatActivity() {
@Inject
lateinit var dataStore: SecurityDataStore

@MAIN
@Inject
lateinit var intentProvider: IntentProvider

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLoginBinding.inflate(layoutInflater)
Expand All @@ -43,8 +48,7 @@ class LoginActivity : AppCompatActivity() {
private fun initCheckAutoLogin() {
lifecycleScope.launch {
if (dataStore.flowAutoLogin().first()) {
val intent = Intent(this@LoginActivity, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
val intent = intentProvider.getIntent()
startActivity(intent)
}
}
Expand All @@ -57,14 +61,12 @@ class LoginActivity : AppCompatActivity() {
when (state.data.isRegistered) {
true -> {
dataStore.setAutoLogin(true)
val intent = Intent(this@LoginActivity, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
val intent = intentProvider.getIntent()
startActivity(intent)
}

false -> {
val intent = Intent(this@LoginActivity, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
val intent = intentProvider.getIntent()
startActivity(intent)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import org.sopt.common.intentprovider.IntentProvider
import org.sopt.common.intentprovider.LOGIN
import org.sopt.datastore.datastore.SecurityDataStore
import org.sopt.mainfeature.R
import org.sopt.mypage.databinding.FragmentSettingsBinding
Expand All @@ -25,6 +26,7 @@ class SettingsFragment : Fragment() {
private val viewModel: SettingsViewModel by viewModels()

@Inject
@LOGIN
lateinit var intentProvider: IntentProvider

@Inject
Expand Down Expand Up @@ -60,7 +62,7 @@ class SettingsFragment : Fragment() {
when (state) {
is UiState.Success -> {
dataStore.setAutoLogin(false)
val intent = intentProvider.getAuthIntent()
val intent = intentProvider.getIntent()
startActivity(intent)
requireActivity().finish()
}
Expand All @@ -77,7 +79,7 @@ class SettingsFragment : Fragment() {
when (state) {
is UiState.Success -> {
dataStore.setAutoLogin(false)
val intent = intentProvider.getAuthIntent()
val intent = intentProvider.getIntent()
startActivity(intent)
requireActivity().finish()
}
Expand Down

0 comments on commit ee83f77

Please sign in to comment.