AshCookieConsent.Plug (AshCookieConsent v0.1.0)
View SourcePhoenix Plug for cookie consent management in traditional controller-based applications.
This plug loads consent from the three-tier storage system and sets assigns for use in templates and controllers.
Usage
Add to your router pipeline:
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_cookies # Required for reading consent cookie
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
plug AshCookieConsent.Plug # resource is optional for lightweight mode
endImportant: plug :fetch_cookies must come before AshCookieConsent.Plug to populate
conn.req_cookies for the consent library to read the _consent cookie.
Configuration
The plug accepts the following options:
:resource- (optional) Ash resource module for database persistence. Not needed for lightweight cookie/session storage.:cookie_name- Cookie name (default: "_consent"):session_key- Session key (default: "consent"):user_id_key- Key in assigns for user ID (default: :current_user_id):cookie_opts- Additional cookie options (seeAshCookieConsent.Cookie):skip_session_cache- Skip caching consent in Phoenix session (default: false). Set to true to avoid session interference.
Assigns Set
The plug sets the following assigns:
:consent- The consent data map (or nil):show_consent_modal- Boolean flag indicating if modal should show:cookie_groups- Configured cookie groups
Examples
# In your router
plug AshCookieConsent.Plug,
resource: MyApp.Consent.ConsentSettings,
cookie_name: "my_consent",
user_id_key: :user_id
# In your templates
<%= if @show_consent_modal do %>
<.consent_modal
current_consent={@consent}
cookie_groups={@cookie_groups}
/>
<% end %>
# In your controllers
if AshCookieConsent.consent_given?(conn, "analytics") do
# Load analytics
end