Appearance
Auth State
A user's authentication state is described by the AuthState sealed type.
kotlin
public sealed interface AuthState {
// AuthState has not been determined yet, show loading
public data object NotReady : AuthState
// User is unauthenticated
public data object Unauthenticated : AuthState
// User is authenticated and has an assoicated PrivyUser object
public data class Authenticated(val user: PrivyUser) : AuthState
}
Auth state is exposed as a StateFlow on the Privy object:
kotlin
public interface Privy {
// A state flow that can be subscribed to for auth state updates
public val authState: StateFlow<AuthState>
}
There are various ways to determine user's auth state, outlined below:
1. Directly grab the User
As a convenience, you can grab the user object directly from the Privy instance. If the user is not null, there is an authenticated user.
kotlin
val privyUser = privy.user
if (privyUser != null) {
// User is authenticated
val linkedAccounts = privyUser.linkedAccounts
}
2. Grab the user's current auth state
kotlin
// Grab current auth state
val currentAuthState = privy.authState.value
if (currentAuthState is AuthState.Authenticated) {
// User is authenticated. Grab the user's linked accounts
val privyUser = currentAuthState.user
val linkedAccount = privyUser.linkedAccounts
}
3. Subscribe to auth state updates
kotlin
coroutineScope.launch {
privy.authState.collectLatest { authState ->
when(authState) {
is AuthState.Authenticated -> {
// User is authenticated. Grab the user's linked accounts
val privyUser = authState.user
val userId = privyUser.linkedAccounts
}
AuthState.NotReady -> {
// Privy not yet ready. Call privy.awaitReady()
}
AuthState.Unauthenticated -> {
// User in not authenticated. Perhaps show login screen.
}
}
}
}