Building a Multi-Device Login State System from Scratch: General Technical Practices for Online Video Platforms

    This article explains how to design a unified authentication and multi-device data synchronization architecture for online video platforms. It covers a three-layer structure of unified authentication, business gateway, and data synchronization service, a dual-token refresh mechanism, conflict-merge strategies for playback progress, engineering practices for danmaku and comments, and approaches to balancing security with user experience.

    Building a Multi-Device Login State System from Scratch: General Technical Practices for Online Video Platforms

    1. Requirement Abstraction: Common Technical Challenges of Video Platforms

    A typical online video platform, regardless of scale, aggregates multiple business entry points on its homepage — membership purchases, comics, esports events, live streaming, and more. The foundation that keeps these entries running smoothly is a unified login state and multi-device data capability. The core requirements can be abstracted into four points:

    1. Tiered experience before and after login: Anonymous users can browse and watch basic content for free, while logged-in users unlock high-definition quality, danmaku and comments, playback history sync, messages, and personalized feeds.
    2. Multi-device consistency: Playback progress, favorites, and history must sync in near real time across phones, tablets, desktop clients, and the web.
    3. High-concurrency reads and writes: When a popular video goes live, writes of playback records, danmaku, and comments form obvious traffic spikes.
    4. Security and compliance: Secure storage, transmission, and expiration management of account credentials directly determine the platform’s security baseline.

    2. Overall Architecture Design

    A three-layer structure is recommended: unified authentication layer + business gateway + data synchronization service.

    1. Unified authentication layer: Centrally handles registration, login, logout, and token refresh. All business lines validate tokens through the gateway, avoiding inconsistent security postures caused by each business implementing its own login logic.
    2. Business gateway: Handles routing, rate limiting, and pre-validation of authentication. It applies differentiated policies for anonymous and logged-in users — for example, anonymous users can only pull basic resolutions, while logged-in users get high-definition access based on their tier.
    3. Data synchronization service: Centered on playback records, favorites, and messages, it provides a sync channel based on “local-first writes + asynchronous upload + conflict merging.”

    3. Login State Management: Tokens and Refresh Mechanism

    Modern platforms generally adopt a dual-token scheme: a short-lived access token handles API authentication, while a long-lived refresh token enables seamless renewal. Key design points include:

    • Keep the access token lifetime at the minute level to limit the blast radius of a leak;
    • Bind the refresh token to a device fingerprint so it can be revoked per device;
    • The refresh endpoint must have replay protection — an old refresh token is invalidated after a single use.

    Sample code (pseudocode):

    1. async function refreshToken(oldRefreshToken, deviceFingerprint) {
    2. const record = await tokenStore.find(oldRefreshToken);
    3. if (!record || record.revoked || record.device !== deviceFingerprint) {
    4. throw new AuthError('invalid_token');
    5. }
    6. await tokenStore.rotate(oldRefreshToken); // Invalidate the old token immediately
    7. return issueTokens(record.userId, deviceFingerprint);
    8. }

    On the client side, 401 responses should be handled uniformly in a request interceptor: first attempt a silent refresh and replay the original request; only when the refresh fails should the user be guided to log in again. This avoids repeated redirects to the login page when multiple concurrent requests fail simultaneously.

    4. Multi-Device Sync: Merge Strategy for Playback Records

    The most easily overlooked aspect of playback progress sync is conflict handling. When a user watches the same video on two devices in sequence, the server cannot simply apply “last write wins.” Instead, it should perform a semantic merge based on timestamps and playback positions:

    1. The client writes progress locally every 10–30 seconds and uploads in batches;
    2. The server prefers the record that is “updated later and has advanced further in playback position”;
    3. If the position difference between the two ends is below a threshold (e.g., 5 seconds), treat them as the same progress to avoid jumping back and forth.

    5. Interactive Features: Engineering Essentials for Danmaku and Comments

    A danmaku (bullet comment) system is essentially log-type data with “high write volume and reads by time window.” A common approach is to shard storage by video ID and time segment; the player requests danmaku for the current playback time window, then performs render throttling and deduplication on the client. Comments follow a more traditional pagination and tree-structured reply model, with emphasis on sensitive content filtering and rate limiting.

    6. Balancing Security and Experience

    • Graphical or behavioral CAPTCHAs should be triggered by abnormal login frequency, not enabled unconditionally for all users;
    • Message and feed pushes should combine persistent connections with offline catch-up pulls, ensuring eventual consistency on weak networks;
    • The anonymous-to-logged-in path must be smooth: a login prompt shown mid-playback should not interrupt progress, and the original state should be restored automatically after a successful login.

    7. Conclusion

    The competitiveness of a multi-device video platform comes not only from content, but also from the underlying experience of “log in and sync, resume anywhere.” Unified authentication, semantic conflict merging, sharded danmaku storage, and progressive security policies form a reusable technical skeleton for this type of platform. Developers can build on this skeleton according to their business scale, gradually adding personalized recommendations and risk control capabilities as the platform evolves.