getUserIdOrThrow

abstract suspend fun getUserIdOrThrow(): String

Retrieves the authenticated user's ID, or throws if user is not authenticated.

This is a convenience function to get the user ID when authentication is required. If the user ID is empty (not authenticated), this function throws IllegalStateException.

When to Use

  • Before operations that require authentication

  • In repositories that need the current user ID

  • When user ID is mandatory for the operation

Example

suspend fun loadUserData(): Result<UserData> = suspendRunCatching {
val userId = userPreferencesDataSource.getUserIdOrThrow()
networkDataSource.fetchUserData(userId)
}

Return

The authenticated user's ID as a String.

Throws

if the user is not authenticated (ID is empty).