Skip to content

Profiles and PIN

Status: saved users on a server as profiles with avatars (#245) and an optional local PIN (#299) or phone unlock pattern (#330); built on phone and TV. As of 2026-11-25 (main).

  • Lock rules, pattern geometry, and PBKDF2 hashing live in application/.../profilelock/: ProfileLock (process-wide singleton bound in data’s DatabaseModule), ProfilePinHasher, ProfilePattern, and ProfileLockEntry.
  • Switching and PIN/pattern prompts live in setup/.../presentation/: UsersViewModel, ProfilePinGateViewModel, ProfilePinSettingsViewModel, and PinPrompt.
  • Phone UI lives in app/phone/.../presentation/setup/pin/ (ProfilePinGateScreen, ProfilePinScreen, PinDialog, PatternGrid, PatternDialog, SwitchPinDialog) and ProfileBottomSheet (app/phone/.../presentation/film/components/ProfileBottomSheet.kt). TV UI lives in app/tv/.../ce/presentation/pin/ (TvProfilePinGateScreen, TvProfilePinScreen, TvPinDialog, TvPinPad, TvSwitchPinDialog) and app/tv/.../presentation/setup/users/UsersScreen.kt.
  • Start-up gating (MainState.profileLocked and MainState.askUserOnStart) is derived in MainViewModel (core/.../viewmodels/MainViewModel.kt) and enforced in both NavigationRoots before Home renders.
  • Reference screenshots for phone and TV are in docs/profiles/pin/.
  • Test fast: ./gradlew :application:test, ./gradlew :data:testDebugUnitTest --tests '*ProfileLockTest*', and ./gradlew :setup:testDebugUnitTest --tests '*Pin*'.

Each signed-in user on a server is stored in Room’s users table (UserEntity / User). EphyraAvatar (core/.../presentation/ephyra/EphyraAvatar.kt) shows the user’s Jellyfin picture when User.primaryImageTag is present, or initials (avatarInitials, falling back to "?" when blank) on one of four theme container colours picked deterministically from UserId (avatarColorIndex, preserving pre-#773 UUID hash colours). A locked profile (User.hasPin) displays a lock badge; the signed-in user gets a check badge (current). Phone and TV wrap EphyraAvatar as UserAvatar.

Users switch profiles from HomeHeader → ProfileBottomSheet on phone (which lists other saved users and server public users), from the drawer’s profile header (MainScreen) → UsersScreen on TV, or from Settings → Users. Both ProfileBottomSheet and UsersScreen drive UsersViewModel. On phone the sheet also offers “Approve a Quick Connect code” (#855): QuickConnectApproveDialog takes the six digits another device (a TV, a browser) shows, and QuickConnectApproveViewModel (setup/.../presentation/quickconnect/) approves them as the signed-in user, so that device is signed in as them. The entry is hidden when the server type has no Quick Connect (ServerCapabilities.quickConnect) or the server says it is turned off. TV has no such entry. When appPreferences.askUserOnStart is enabled and the active server has more than one saved user, MainState.askUserOnStart is true and both NavigationRoots push UsersRoute once per app start (rememberSaveable; on TV a WatchNextLink takes precedence).

A profile may hold one local lock at a time (User.pinHash, User.pinSalt, User.lockType, User.pinOnStart):

  • PIN (ProfileLockType.PIN, phone and TV): 4 to 6 ASCII digits (ProfilePinHasher.MIN_LENGTH..MAX_LENGTH).
  • Pattern (ProfileLockType.PATTERN, phone only): 4 to 9 distinct dots on a 3×3 grid numbered 0..8 (ProfilePattern), serialized as dash-separated indices (e.g. "0-1-2-5-8"). A straight line across an unconnected middle dot automatically connects that dot (ProfilePattern.connect). Setting one type replaces the other (ProfileLock.setPin, ProfileLock.setPattern).
  • Both secrets are hashed locally by ProfilePinHasher (PBKDF2-HMAC-SHA256, 20,000 iterations, 16-byte random salt, 256-bit key, constant-time MessageDigest.isEqual comparison) and never leave the device.

ProfileLock keeps in-memory unlock state for the process lifetime: lastUnlockedUserId tracks the single active profile, while unlockedUserIds accumulates profiles unlocked or switched to in this process so MainViewModel.profileLocked does not flash the start gate on the outgoing profile while Room catches up. Switching to another locked profile requires its lock (ProfileLock.needsPinToSwitch). Setting a PIN/pattern or toggling pinOnStart from inside Settings unlocks that profile immediately.

On a new process, ProfileLock.isLockedOnStart is true when the current user has a lock, pinOnStart is true (default), and user.id !in unlockedUserIds. Both NavigationRoots render ProfilePinGateScreen / TvProfilePinGateScreen instead of Home while setupComplete && profileLocked, so Home loads nothing until unlocked (and on TV a Watch Next deep link waits on the back stack underneath). Cancelling at start (ProfilePinGateAction.OnCancel or Back) calls SetupRepository.clearCurrentUser(), dropping to UsersRoute.

Settings → Profile lock (phone) / Settings → Profile PIN (TV, switched by AppVariant in SettingsViewModel) opens ProfilePinScreen / TvProfilePinScreen backed by ProfilePinSettingsViewModel. Setting a new lock asks twice (PinStep.New, PinStep.Confirm); changing, switching type, or removing asks for the current lock first (PinStep.Current). A forgotten lock cannot be recovered: the user must remove the profile or sign in again, which clears the lock.

Path What
domain/.../models/User.kt User, ProfileLockType (PIN, PATTERN), User.hasPin, User.profileLockType
application/.../profilelock/ProfileLock.kt Process unlock state, setPin, setPattern, clearPin, verify, needsPinToSwitch, isLockedOnStart
application/.../profilelock/ProfilePinHasher.kt PBKDF2-HMAC-SHA256 hashing (ProfilePinHasher), ProfilePattern, ProfileLockEntry
application/.../repository/UserLocks.kt Store port for writing pinHash, pinSalt, lockType, and pinOnStart
data/.../database/RoomServerStore.kt Room UserLocks implementation over ServerDatabaseDao and UserEntity; singleton ProfileLock bound in DatabaseModule
setup/.../presentation/pin/ ProfilePinGateViewModel, ProfilePinSettingsViewModel (PinStep), PinPrompt (PinError)
setup/.../presentation/users/UsersViewModel.kt Profile switching, public user refresh, switch lock prompt
core/.../viewmodels/MainViewModel.kt Derives MainState.profileLocked and MainState.askUserOnStart
core/.../presentation/ephyra/EphyraAvatar.kt Shared avatar (picture, initials, deterministic colour, lock/current badges); ErrorShake.kt for errorShake
app/phone/.../presentation/setup/pin/ ProfilePinGateScreen, ProfilePinScreen, PinDialog, PatternGrid, PatternDialog, SwitchPinDialog
setup/.../presentation/quickconnect/ QuickConnectApproveViewModel, QuickConnectApproveState (QuickConnectApproveStatus), QuickConnectApproveAction (#855)
app/phone/.../presentation/film/components/ProfileBottomSheet.kt Phone Home profile sheet; QuickConnectApproveDialog; HomeHeader, UserAvatar, UsersScreen
app/tv/.../ce/presentation/pin/ TvProfilePinGateScreen, TvProfilePinScreen, TvPinDialog, TvPinPad, TvSwitchPinDialog
app/tv/.../presentation/setup/users/UsersScreen.kt TV user picker, UserItem, drawer item in MainScreen
app/phone/.../NavigationRoot.kt, app/tv/.../NavigationRoot.kt Gates Home behind ProfilePinGateScreen / TvProfilePinGateScreen, pushes UsersRoute for askUserOnStart, hosts ProfilePinRoute
  • R4: no production file outside data imports ServerDatabaseDao. ProfileLock reads users through StoredUsers and writes locks through UserLocks.
  • R9: only the apps depend on data; setup, settings, and core never depend on data (DatabaseModule in data provides UserLocks and the singleton ProfileLock to Hilt).
  • R10: UserId and ServerId are value classes (Ids.kt), never String or java.util.UUID in production signatures outside the adapter.
  • R19: application/.../profilelock/ stays pure Kotlin (ProfilePinHasher uses only JDK javax.crypto, java.security, and java.util.Base64).
  • Translations: all CE profile/lock strings (ce_profile_*, ce_users_*, ce_settings_profile_*) must be added to strings_ce.xml in all 36 languages and pass check-translations.py.
Terminal window
./gradlew :application:test
./gradlew :data:testDebugUnitTest --tests '*ProfileLockTest*'
./gradlew :setup:testDebugUnitTest --tests '*Pin*'
./gradlew :setup:testDebugUnitTest --tests '*QuickConnectApprove*'
./gradlew :core:testLibreDebugUnitTest --tests '*EphyraAvatarTest*'
./gradlew :app:phone:testLibreDebugUnitTest --tests '*ProfileBottomSheetTest*'
./gradlew :app:tv:testLibreDebugUnitTest --tests '*TvPinPadTest*' --tests '*MainReturnLoadsTest*'

Unit tests cover hashing and pattern geometry (ProfilePinHasherTest, ProfilePatternTest), lock state and cross-type verification (ProfileLockTest), switch/gate/settings flows over FakeRoles (ProfilePinSwitchTest), avatar initials and legacy UUID colour stability (EphyraAvatarTest), landscape profile sheet layout (ProfileBottomSheetTest), approving a Quick Connect code and when the entry shows (QuickConnectApproveTest), TV remote key handling (TvPinPadTest), and TV drawer user reload on return (MainReturnLoadsTest).

On a device or emulator, tools/test-server/setup.sh creates multiple users (tester, second, nopass, nodownload; see test-environment.md) so multi-profile switching, askUserOnStart, and PIN/pattern locks can be exercised against http://10.0.2.2:8096.

What unit tests do not cover:

  • PatternGrid touch-drag gestures, errorShake timing, and ProfileBottomSheet cross-window hiding on HomeScreen (#872).
  • Room auto-migrations (14→15, 16→17, 17→18) preserving existing logins and lock rows on upgrade (ProfilePinSwitchTest and ProfileLockTest run against in-memory FakeRoles).
  • TV Watch Next deep links launching into a locked profile (holding the Watch Next stack beneath TvProfilePinGateScreen until unlocked) and physical remote Enter/OK quirks.
Terminal window
./gradlew ktfmtCheck
./gradlew :application:test :architecture:test
./gradlew :data:testDebugUnitTest --tests '*ProfileLockTest*'
./gradlew :setup:testDebugUnitTest --tests '*Pin*'
./gradlew :app:phone:compileLibreDebugKotlin :app:tv:compileLibreDebugKotlin
python3 .claude/skills/start-work/scripts/check-docs.py
python3 .claude/skills/translate-strings/scripts/check-translations.py # when strings changed
  • #245 / #248: profile switching UI (ProfileBottomSheet, UsersScreen), EphyraAvatar deterministic initials/colours (preserving legacy UUID.hashCode() colours in #773), User.primaryImageTag, and askUserOnStart.
  • #299 / #329: local profile PIN stored as a salted PBKDF2-HMAC-SHA256 hash on the User row (20,000 iterations so slow TV boxes check without a visible wait: it keeps out someone on the couch, not someone who extracts the database; no attempt lockout or inactivity re-lock), ProfileLock singleton, start gate before Home, TvPinPad, and Settings → Profile PIN.
  • #330 / #333: unlock pattern on phone (ProfileLockType, ProfilePattern, PatternGrid, PatternDialog), reusing pinHash/pinSalt so each profile has one lock at a time, and extending ProfilePinSettingsViewModel / PinPrompt while keeping TV’s exhaustive when branches untouched.
  • #408, #409 / #532: TV drawer reloads the user on every return (loadsOnReturn) so the lock badge tracks PIN changes immediately; TvPinPad moves focus to OK on a remote number key press so OK submits instead of typing a digit.
  • #440: ProfileBottomSheet opens fully (skipPartiallyExpanded = true) and scrolls vertically in short landscape windows so Settings remains reachable.
  • #866 / #873, #872 / #881: tab root ViewModels (FavoritesViewModel, SearchViewModel, DownloadsViewModel) and HomeScreen (SessionUser, hiding the sheet before removal, key(state.user?.id)) drop the previous user’s rows immediately on switch.
  • #855: the phone profile sheet approves the Quick Connect code of another device. A whole code only, one request at a time; the field stays filled after a failure so a typo can be fixed. See setup-and-servers for the port.
  • #967, #988: user lock writes live behind UserLocks (RoomServerStore), and setCurrentUser updates only currentUserId instead of replacing the ServerEntity row.
  • tv-material button closure capture (TvPinPad.kt): androidx.tv.material3.Button retains the click lambda from its first composition for D-pad OK. Because multi-step dialogs (TvProfilePinScreen) pass a new pin state per step, TvPinPad reads pin and onSubmit via rememberUpdatedState. Without it, Change PIN re-saves the old PIN.
  • Remote OK vs number keys (TvPinPad.kt, #409): TvPinPad intercepts digit keys regardless of focused button, so a digit key must also request focus on okKey; Key.Enter / Key.DirectionCenter is deliberately not handled in onPreviewKeyEvent because some remotes send Enter for D-pad OK.
  • Re-authenticating wipes the lock (SetupRepositoryImpl.kt): insertUser uses OnConflictStrategy.REPLACE with default pinHash = null, so signing in again as a locked user clears their local PIN/pattern (the documented recovery path for a forgotten lock).
  • Do not shrink unlockedUserIds on switch (ProfileLock.kt): unlockedUserIds intentionally keeps previously unlocked user IDs while lastUnlockedUserId tracks only the latest profile. Removing the old user ID immediately on switch causes MainViewModel.profileLocked to flash the start gate on the outgoing profile while Room catches up.
  • Cross-user frame leakage on switch (#866, #872): ProfileBottomSheet switches users while HomeScreen and tab root ViewModels remain alive; user-scoped content must be gated with shownTo(userId) / LocalServerSession.current.userId (and SessionUser on HomeScreen) rather than waiting for Room’s flow.
  • Keep PinStep and PinError TV-safe: ProfilePinSettingsViewModel and PinPrompt are shared by phone and TV; adding new PinStep or PinError subtypes breaks TV’s exhaustive when branches unless TV is updated in the same change.

Source: docs/areas/profiles.md · raw Markdown