Skip to content

Commit

Permalink
Add more logging to authentication code
Browse files Browse the repository at this point in the history
(cherry picked from commit f185579)
  • Loading branch information
nielsvanvelzen committed Jul 1, 2023
1 parent df561f8 commit bf4fa8e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class AuthenticationRepositoryImpl(
}

private fun authenticateAutomatic(server: Server, user: User): Flow<LoginState> {
Timber.d("Authenticating user %s", user)
Timber.i("Authenticating user %s", user)

// Automatic logic is disabled when the always authenticate preference is enabled
if (authenticationPreferences[AuthenticationPreferences.alwaysAuthenticate]) return flowOf(RequireSignInState)
Expand Down Expand Up @@ -121,7 +121,7 @@ class AuthenticationRepositoryImpl(
}

private fun authenticateAuthenticationResult(server: Server, result: AuthenticationResult) = flow {
val accessToken = result.accessToken ?:return@flow emit(RequireSignInState)
val accessToken = result.accessToken ?: return@flow emit(RequireSignInState)
val userInfo = result.user ?: return@flow emit(RequireSignInState)
val user = PrivateUser(
id = userInfo.id,
Expand All @@ -135,8 +135,12 @@ class AuthenticationRepositoryImpl(

authenticateFinish(server, userInfo, accessToken)
val success = setActiveSession(user, server)
if (success) emit(AuthenticatedState)
else emit(RequireSignInState)
if (success) {
emit(AuthenticatedState)
} else {
Timber.w("Failed to set active session after authenticating")
emit(RequireSignInState)
}
}

private fun authenticateToken(server: Server, user: User) = flow {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ class ServerRepositoryImpl(

// Mutating data
override fun addServer(address: String): Flow<ServerAdditionState> = flow {
Timber.d("Adding server %s", address)
Timber.i("Adding server %s", address)

emit(ConnectingState(address))

val addressCandidates = jellyfin.discovery.getAddressCandidates(address)
Timber.d("Found ${addressCandidates.size} candidates")
Timber.i("Found ${addressCandidates.size} candidates")

val goodRecommendations = mutableListOf<RecommendedServerInfo>()
val badRecommendations = mutableListOf<RecommendedServerInfo>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SessionRepositoryImpl(
override val state = _state.asStateFlow()

override suspend fun restoreSession(): Unit = currentSessionMutex.withLock {
Timber.d("Restoring session")
Timber.i("Restoring session")
_state.value = SessionRepositoryState.RESTORING_SESSION

if (authenticationPreferences[AuthenticationPreferences.alwaysAuthenticate]) return destroyCurrentSession()
Expand All @@ -84,14 +84,17 @@ class SessionRepositoryImpl(

override suspend fun switchCurrentSession(serverId: UUID, userId: UUID): Boolean {
// No change in user - don't switch
if (currentSession.value?.userId == userId) return false
if (currentSession.value?.userId == userId) {
Timber.d("Current session user is the same as the requested user")
return false
}

_state.value = SessionRepositoryState.SWITCHING_SESSION
Timber.d("Switching current session to user $userId")
Timber.i("Switching current session to user $userId")

val session = createUserSession(serverId, userId)
if (session == null) {
Timber.d("Could not switch to non-existing session for user $userId")
Timber.w("Could not switch to non-existing session for user $userId")
return false
}

Expand All @@ -101,7 +104,7 @@ class SessionRepositoryImpl(
}

override fun destroyCurrentSession() {
Timber.d("Destroying current session")
Timber.i("Destroying current session")

userRepository.updateCurrentUser(null)
_currentSession.value = null
Expand All @@ -127,7 +130,7 @@ class SessionRepositoryImpl(
// Update session after binding the apiclient settings
val deviceInfo = session?.let { defaultDeviceInfo.forUser(it.userId) } ?: defaultDeviceInfo
val success = apiBinder.updateSession(session, deviceInfo)
Timber.d("Updating current session. userId=${session?.userId} apiBindingSuccess=${success}")
Timber.i("Updating current session. userId=${session?.userId} apiBindingSuccess=${success}")

if (success) {
userApiClient.applySession(session, deviceInfo)
Expand Down

0 comments on commit bf4fa8e

Please sign in to comment.