AshCookieConsent.Storage (AshCookieConsent v0.1.0)

View Source

Three-tier storage management for consent data.

Implements a hierarchical storage system with the following priority:

Read Priority (fastest to slowest)

  1. Connection/Socket assigns (request-scoped, in-memory)
  2. Session (server-side, encrypted)
  3. Cookie (client-side, signed)
  4. Database (persistent, audit trail)

Write Strategy

When consent is updated, it's written to all applicable tiers:

  1. Database (if user is authenticated)
  2. Cookie (always, for persistence)
  3. Session (always, for performance)
  4. Assigns (always, for immediate access)

Examples

# Get consent from any available tier
consent = AshCookieConsent.Storage.get_consent(conn)

# Save consent to all tiers
conn = AshCookieConsent.Storage.put_consent(conn, consent, resource: MyApp.ConsentSettings)

# Delete consent from all tiers
conn = AshCookieConsent.Storage.delete_consent(conn)

Summary

Functions

Deletes consent from all storage tiers.

Gets consent from the highest priority available tier.

Saves consent to all applicable storage tiers.

Syncs consent from database to cookie when consent is loaded.

Syncs consent from cookie to database on user login.

Functions

delete_consent(conn, opts \\ [])

Deletes consent from all storage tiers.

Examples

conn = AshCookieConsent.Storage.delete_consent(conn)

get_consent(conn, opts \\ [])

Gets consent from the highest priority available tier.

Checks in order: assigns → session → cookie → database

Returns the consent map or nil if no consent found.

Examples

consent = AshCookieConsent.Storage.get_consent(conn)
# => %{"terms" => "v1.0", "groups" => ["essential", "analytics"], ...}

put_consent(conn, consent, opts \\ [])

Saves consent to all applicable storage tiers.

Options

  • :resource - Ash resource module for database storage
  • :user_id_key - Key in assigns for user ID (default: :current_user_id)
  • :session_key - Session key for consent (default: "consent")
  • :cookie_name - Cookie name (default: "_consent")
  • :skip_database - Skip database save even if authenticated (default: false)

Examples

conn = AshCookieConsent.Storage.put_consent(conn, consent,
  resource: MyApp.ConsentSettings
)

sync_from_database(conn, opts)

Syncs consent from database to cookie when consent is loaded.

Useful for ensuring cookie is up-to-date with latest database consent.

Examples

conn = AshCookieConsent.Storage.sync_from_database(conn,
  resource: MyApp.ConsentSettings,
  user_id: user.id
)

sync_on_login(conn, opts)

Syncs consent from cookie to database on user login.

Merges existing cookie consent with database consent, preferring database.

Examples

conn = AshCookieConsent.Storage.sync_on_login(conn,
  resource: MyApp.ConsentSettings,
  user_id: user.id
)