Introducing a new way to review your PRs.

Read the announcement

iv-org / invidious

Invidious is an alternative front-end to YouTube

View on GitHub

19 documents · Updated Oct 12, 2026

docs / accounts-sessions-and-preferences

Accounts, sessions, and preferences

Invidious accounts connect a person's saved activity and viewing choices across requests. Login creates a server-backed session, cookies carry the session or anonymous preferences, and account routes manage passwords, history, tokens, and account removal.

Account access

Login and registration

The login flow checks the instance policy before it accepts a password. An existing account starts a new session after password verification. An unknown email can create an account only when registration is open. The login page also supports a selected account type, although the built-in account path is the one that creates and checks Invidious users.

Registration creates an account with empty saved activity and the current instance defaults. If the visitor already has anonymous preferences, registration moves those choices into the new account before it clears the anonymous preference cookie. A normal login does not replace the saved preferences of an existing account.

Route groupUser goal
/loginSign in or register when the instance allows it
/signoutEnd the current session
/change_passwordReplace the account password
/delete_accountRemove the account and its sessions
/clear_watch_historyRemove saved watch history without removing the account

References: src/…/login.cr• L4-151 src/…/account.cr• L10-185 src/…/routing.cr• L56-75 src/…/users.cr• L6-22

Account controls

Account actions require a signed request token tied to the current session. This check protects state-changing actions such as password changes, sign-out, history clearing, account deletion, and token changes. Unauthenticated visitors are sent back to the relevant page or to login when an account is required.

The account page also lets a user approve an access token for another client. The user selects the requested access scope and optional expiry. The token can be sent to a callback or shown for later use, and the token manager can revoke a session entry.

References: src/…/account.cr• L10-79 src/…/account.cr• L143-185 src/…/account.cr• L192-345

CAPTCHA protection

A CAPTCHA is a human check used during new-account creation when the administrator enables it. Invidious shows a clock image and expects the time represented by the hands. The answer is checked with a short-lived signed challenge before registration continues.

The check applies to registration rather than to successful login for an existing account. If the answer is missing or invalid, account creation does not proceed.

References: src/…/login.cr• L77-117 src/…/captcha.cr• L7-59 src/…/config.cr• L134-141

Session handling

Session creation and lookup

A session is a random identifier stored in the database with the account email and issue time. The browser receives that identifier in the SID cookie. On later requests, the request setup uses the identifier to find the account and places the account and session context on the request.

The same session store supports browser requests and authenticated API requests. API clients can present an approved access token, while browser requests use the SID cookie. A token cannot be treated as a browser session identifier.

The following diagram shows how a login becomes account context on later requests.

flowchart LR
    A[Login credentials] --> B[Account check]
    B --> C[Session record]
    C --> D[SID cookie]
    D --> E[Later request]
    E --> F[Session lookup]
    F --> G[User and preferences context]
    G --> H[Account-aware route]

References: src/…/login.cr• L44-65 src/…/sessions.cr• L10-19 src/…/before_all.cr• L83-118 src/…/handlers.cr• L74-119

The SID cookie identifies a server-side session and is marked for browser-only use. The PREFS cookie stores anonymous viewing choices so a visitor can keep those choices without an account. Both cookies use the instance domain and a long expiry. The preference cookie is readable by the client because the browser must send its value back as preference data.

When an account is found, its stored preferences replace the anonymous preferences for the request. URL options such as theme and language can then adjust the active request without changing the stored account until the user saves preferences.

References: src/…/cookies.cr• L7-50 src/…/before_all.cr• L14-31 src/…/before_all.cr• L90-118

Session termination

Sign-out removes the current session record and expires the browser cookies. Account deletion removes all sessions for the account and expires the cookies as well. The session manager lists active sessions by issue time so a user can revoke a selected session without ending the others.

References: src/…/login.cr• L153-181 src/…/account.cr• L104-136 src/…/account.cr• L275-345 src/…/sessions.cr• L25-73

Preference persistence

Preference model

Preferences describe the user's display, player, feed, language, privacy, and history choices. The model supplies instance defaults when values are absent and accepts older serialized forms so stored data can continue to load after the preference shape changes.

Some values are bounded or normalized when they are read. This keeps user-controlled values within safe limits and lets old Boolean or scalar forms remain usable.

References: src/…/preferences.cr• L1-60 src/…/preferences.cr• L62-123 src/…/config.cr• L18-65

Preference update flow

The preferences route turns form choices into one preference set. For a signed-in user, it saves that set with the account. For a visitor, it writes the set to PREFS. The theme toggle follows the same split, so a theme change remains account-specific after login and cookie-based before login.

The account preference page also exposes selected instance controls to an administrator. Those controls change instance defaults and access policy rather than the current user's personal settings.

References: src/…/preferences.cr• L13-243 src/…/preferences.cr• L245-288 src/…/users.cr• L167-175 src/…/config.cr• L134-141

Account data portability

The data control route gives a signed-in user a place to import account data from supported sources. Imports can add subscriptions, playlists, and watch history to the account. The detailed formats and transfer behavior are documented in User data import and export.

These account records connect to the user's personal library and channel feed. Read Playlists and personal library for saved collections and Channel content and subscriptions for subscriptions and feed updates.

References: src/…/preferences.cr• L290-370 src/…/imports.cr• L1-120 src/…/users.cr• L42-116

Change checklist

When these files or sections are changed, remember to consider these:

  • Changes to the session cookie or session table must stay aligned with browser authentication, authenticated API requests, token approval, and token revocation.
  • Changes to preference fields or converters must preserve both anonymous PREFS data and the account preference value used during registration conversion.
  • Changes to login or registration policy must remain consistent with the login page, form handling, administrator controls, and public registration status.
  • Changes to account deletion must remove both the account record and every session or account-specific view that can retain access or data.
  • Changes to import handling must preserve the link between data-control uploads and the account records used by playlists, subscriptions, and watch history.

Important failure modes

TriggerConsequenceGuard
A stale or unknown SID reaches an account routeThe request has no usable account context and protected work is not performedLook up the session before loading the user
A browser sends a token in the SID cookieThe request is rejected instead of treating an access token as a browser sessionReject session values with the token prefix
A registration CAPTCHA answer is absent or wrongA new account is not createdRequire the signed CAPTCHA challenge to validate
An anonymous user saves preferences and then registersThe new account could lose the visitor's choicesCopy the active preferences into the new account before expiring PREFS
An account is deleted without removing its sessionsOld browsers or clients can retain accessDelete sessions by account email and expire browser cookies

Key files

FileWhy to read it
src/invidious/routes/login.crMain login, registration, preference conversion, and sign-out flow
src/invidious/routes/account.crPassword, history, deletion, and access-token account actions
src/invidious/routes/before_all.crRequest-wide preference and browser-session resolution
src/invidious/helpers/handlers.crAuthenticated API session and access-token behavior
src/invidious/user/cookies.crContract for SID and PREFS cookies
src/invidious/database/sessions.crSession persistence and revocation rules
src/invidious/routes/preferences.crPreference save, theme toggle, and data-control entry points
src/invidious/user/preferences.crPreference fields, defaults, and compatibility rules
src/invidious/database/users.crAccount persistence for preferences, passwords, history, and subscriptions
src/invidious/user/captcha.crRegistration human-check generation and challenge format