Login and Registration System Design: Layered Authorization in Practice

    Design login and registration around four layers: identity, sessions, authorization, and metering. This article covers registration safeguards, token lifecycle management, subscription-based permissions, configurable pricing and quotas, and a security checklist for deployment. These practices help keep account systems stable and auditable as business policies change.

    1. Why Login and Registration Form the Foundation

    Every multi-user platform starts with an account system. It determines who can access the platform, what they can do, and how their usage is measured. Requirements such as subscription-based feature access and adjustable API pricing are ultimately problems of layered authorization and usage-based billing built on that foundation.

    Login and registration should therefore be designed as more than a pair of forms. A practical architecture separates four layers: identity, sessions, authorization, and metering.

    Layer Responsibility Key Outputs
    Identity Registration, login, account recovery, and account deletion Unique account identifiers and credential hashes
    Session Maintaining login state and controlling expiration Access tokens and refresh tokens
    Authorization Determining access to resources and capabilities Role- or attribute-based policies and subscription plan identifiers
    Metering Recording usage, enforcing limits, and supporting billing Request logs and quota counters

    2. Registration: Prioritize Uniqueness and Abuse Prevention

    Registration is not primarily about collecting information. It is about ensuring unique account identifiers and preventing automated account creation.

    1. Choose stable account identifiers. Use an internally generated, immutable ID as the primary key. Treat phone numbers and email addresses as verifiable login aliases. This keeps historical data linked to the account when users change their contact details.
    2. Validate verification codes. SMS and email codes should be single-use and short-lived—commonly around five minutes. Limit how frequently codes can be sent to the same destination.
    3. Enforce password requirements and secure hashing. Store hashes, never plaintext passwords. Use a salted, slow password-hashing algorithm with an adjustable cost, such as bcrypt, scrypt, or Argon2. Increase the cost parameters gradually as hardware improves.
    4. Make submissions idempotent. Deduplicate registration requests using an idempotency key so repeated clicks do not create duplicate accounts.

    Illustrative pseudocode:

    1. def register(alias, code, password, idem_key):
    2. if not store.set_if_absent(f"idem:{idem_key}", 1, ttl=600):
    3. return "duplicate_request"
    4. if not verify_code(alias, code): # Single-use, short-lived
    5. return "invalid_code"
    6. if store.exists_user(alias):
    7. return "alias_taken"
    8. uid = gen_immutable_id()
    9. store.create_user(uid, alias,
    10. password_hash=hash_password(password), # Slow, salted hash
    11. plan="free")
    12. audit("user.register", uid)
    13. return issue_session(uid)

    3. Authentication and Sessions: Short-Lived Access Tokens, Revocable Refresh Tokens

    A successful login should not hand the client a permanently valid credential. A common approach uses two tokens:

    • Access token: Short-lived, validated without server-side session state, and included with requests.
    • Refresh token: Longer-lived, used only to obtain new access tokens, and revocable by the server.
    1. def issue_session(uid):
    2. access = sign_jwt({"sub": uid, "plan": get_plan(uid)}, ttl=900)
    3. refresh = random_token()
    4. store.save_refresh(uid, hash(refresh), ttl=30*86400)
    5. return {"access": access, "refresh": refresh}

    Three engineering requirements are essential:

    • Token validation must check the signature, issuer, audience, and expiration.
    • Password changes, account deletion, and suspicious login detection must support bulk invalidation of refresh tokens.
    • Tokens must never appear in URLs or logs.

    Use risk-based multi-factor authentication rather than requiring it for every login. Signals such as a new device, an unusual location, or repeated failed attempts can trigger a second factor. Time-based one-time passwords are a common option.

    4. Layered Authorization: Treat Subscription Plans as Policy Data

    Subscription-based feature access should not become a collection of hard-coded conditional branches. Instead, model plans as sets of capabilities and evaluate access through role- or attribute-based policies.

    1. CAP = {"chat": ["free", "plus", "pro"],
    2. "batch": ["pro"],
    3. "agent": ["pro"]}
    4. def allowed(uid, feature):
    5. plan = get_plan(uid)
    6. return plan in CAP.get(feature, [])

    Separating policy from application code allows new tiers and entitlement changes to be introduced through configuration. Roll changes out gradually to control their impact.

    Record authorization decisions in audit logs so teams can later explain why a request was allowed or denied.

    5. Metering and Quotas: Explain Usage and Configure Pricing

    The metering layer needs to deliver accurate calculations, understandable usage records, and policies that can change without a release.

    • Measure accurately. Record input and output size, timestamps, account identifiers, and capability identifiers for each request. Send these records to a message queue for asynchronous aggregation rather than blocking the main request path.
    • Make usage understandable. Show users detailed usage records and remaining allowances. Implement quotas with atomic counters, and return explicit error codes when limits are exceeded instead of a generic failure.
    • Keep policies configurable. Externalize unit prices, free allowances, and rate-limit thresholds. Distribute them through a configuration service and retain configuration versions so pricing changes do not require an application release.

    Apply rate limits at multiple levels, including account, IP address, and API endpoint. Use token buckets to handle traffic bursts, and indicate when clients can retry after exceeding a limit.

    6. Security, Observability, and a Deployment Checklist

    • Prevent credential stuffing. Apply backoff to failed login attempts across both account and IP dimensions. Require stronger verification when suspicious activity is detected.
    • Protect transport and storage. Encrypt traffic throughout the system. Keep keys in a dedicated key management service rather than hard-coding them.
    • Monitor operational health. Track registration success rates, login failure rates, token refresh failure rates, and quota rejection rates, with alerts for abnormal behavior.
    • Address privacy and compliance. Explain how information will be used on the registration page, and provide account deletion and data export options.
    • Check readiness before launch. Verify uniqueness constraints, single-use verification codes, password-hashing costs, token revocation paths, idempotency keys, and audit fields.

    7. Conclusion

    Login and registration sit at the intersection of identity, session management, authorization, and metering. Treating subscription entitlements as policy data, distributing pricing and quota settings through configuration, and auditing each authorization decision helps keep the system stable and its behavior explainable as business requirements change.

    These layers and code sketches map to common cloud capabilities, including key management, configuration services, message queues, logging, monitoring, and alerts. They can be implemented without depending on a particular vendor’s proprietary interfaces.