Skip to content

Improve Wear OS authentication #1499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package dev.johnoreilly.confetti.wear.auth

import dev.johnoreilly.confetti.auth.Authentication
import dev.johnoreilly.confetti.auth.SignInError
import dev.johnoreilly.confetti.auth.SignInResult
import dev.johnoreilly.confetti.auth.SignInSuccess
import dev.johnoreilly.confetti.auth.User
import dev.johnoreilly.confetti.wear.settings.PhoneSettingsSync
import kotlinx.coroutines.CoroutineScope
Expand All @@ -14,6 +11,18 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.stateIn

/**
* Authentication implementation for Wear OS that leverages phone authentication.
*
* This class primarily uses a default local authentication mechanism but adds the ability to
* seamlessly sign in using an ID token synced from the user's phone.
*
* @param settingsSync An instance of [PhoneSettingsSync] to access phone settings,
* specifically the ID token.
* @param defaultAuthentication The default authentication mechanism to be used when
* phone authentication is not available or fails.
* @param coroutineScope The coroutine scope in which the authentication operations are performed.
*/
class WearAuthentication(
val settingsSync: PhoneSettingsSync,
val defaultAuthentication: Authentication,
Expand All @@ -33,20 +42,4 @@ class WearAuthentication(
started = SharingStarted.Lazily,
initialValue = defaultAuthentication.currentUser.value
)

override suspend fun signIn(idToken: String): SignInResult {
val token = settingsSync.settingsFlow.first().idToken

return if (token != null) {
defaultAuthentication.signIn(token)
SignInSuccess
} else {
defaultAuthentication.signOut()
SignInError(Exception("No mobile idToken"))
}
}

override fun signOut() {
defaultAuthentication.signOut()
}
}
Loading