I'm currently in a network service, where im adding an interceptor and listening for a 401. If a 401 happens, i then want to sign the user out and navigate back to my LoginActivity. This all works as expected, except the coroutine keeps being ran even after navigating to LoginActivity, causing new activities to be created constantly. Is there a way i can cancel this coroutine once the navigation has happened?
val httpClient = OkHttpClient.Builder().apply {
addInterceptor {
val token = runBlocking {
tokenStore.getToken().first()?.token
}
val response = it.proceed(
it.request().newBuilder()
.addHeader(HEADER_AUTH, "Bearer $token")
.addHeader(HEADER_ACCEPT, "application/json").build()
)
if (response.code == 401) {
GlobalScope.launch {
signout(
tokenStore,
manager,
context
)
}
context.startActivity(
Intent(context, LoginActivity::class.java).setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
)
)
}
response
}
addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
})
}.build()