Skip to content

Commit

Permalink
Clear out the trakt auth state on HTTP 401 (#888)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes committed Dec 30, 2021
1 parent c313c5b commit 5fbeea3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
14 changes: 13 additions & 1 deletion app/src/main/java/app/tivi/home/MainActivityViewModel.kt
Expand Up @@ -18,6 +18,7 @@ package app.tivi.home

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.tivi.domain.interactors.ClearTraktAuthState
import app.tivi.domain.interactors.UpdateUserDetails
import app.tivi.domain.observers.ObserveTraktAuthState
import app.tivi.domain.observers.ObserveUserDetails
Expand All @@ -26,13 +27,15 @@ import app.tivi.util.Logger
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import retrofit2.HttpException
import javax.inject.Inject

@HiltViewModel
class MainActivityViewModel @Inject constructor(
observeTraktAuthState: ObserveTraktAuthState,
private val updateUserDetails: UpdateUserDetails,
observeUserDetails: ObserveUserDetails,
private val clearTraktAuthState: ClearTraktAuthState,
private val logger: Logger
) : ViewModel() {
init {
Expand All @@ -53,7 +56,16 @@ class MainActivityViewModel @Inject constructor(

private fun refreshMe() {
viewModelScope.launch {
updateUserDetails.executeSync(UpdateUserDetails.Params("me", false))
try {
updateUserDetails.executeSync(UpdateUserDetails.Params("me", false))
} catch (e: HttpException) {
if (e.code() == 401) {
// If we got a 401 back from Trakt, we should clear out the auth state
clearTraktAuthState.executeSync(Unit)
}
} catch (t: Throwable) {
// no-op
}
}
}
}
Expand Up @@ -16,6 +16,7 @@

package app.tivi.data.repositories.traktusers

import app.tivi.extensions.withRetry
import org.threeten.bp.Instant
import org.threeten.bp.Period
import javax.inject.Inject
Expand All @@ -30,7 +31,9 @@ class TraktUsersRepository @Inject constructor(
fun observeUser(username: String) = traktUsersStore.observeUser(username)

suspend fun updateUser(username: String) {
var user = traktDataSource.getUser(username).let {
var user = withRetry {
traktDataSource.getUser(username)
}.let {
// Tag the user as 'me' if that's what we're requesting
if (username == "me") it.copy(isMe = true) else it
}
Expand Down
@@ -0,0 +1,29 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.tivi.domain.interactors

import app.tivi.domain.ResultInteractor
import app.tivi.trakt.TraktManager
import javax.inject.Inject

class ClearTraktAuthState @Inject constructor(
private val traktManager: TraktManager
) : ResultInteractor<Unit, Unit>() {
override suspend fun doWork(params: Unit) {
traktManager.clearAuth()
}
}
3 changes: 2 additions & 1 deletion trakt-auth/src/main/java/app/tivi/trakt/TraktManager.kt
Expand Up @@ -26,6 +26,7 @@ import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand All @@ -46,7 +47,7 @@ class TraktManager @Inject constructor(

private val _state = MutableStateFlow(TraktAuthState.LOGGED_OUT)
val state: StateFlow<TraktAuthState>
get() = _state
get() = _state.asStateFlow()

init {
// Observer which updates local state
Expand Down

0 comments on commit 5fbeea3

Please sign in to comment.