Sha256: 60e0288bcbe134282c31ccbf180a986d8dafb85cb41ab0a86f5e6ac414c5d017

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module GovukPersonalisation
  module AccountConcern
    extend ActiveSupport::Concern

    ACCOUNT_SESSION_REQUEST_HEADER_NAME = "HTTP_GOVUK_ACCOUNT_SESSION"
    ACCOUNT_SESSION_RESPONSE_HEADER_NAME = "GOVUK-Account-Session"
    ACCOUNT_END_SESSION_RESPONSE_HEADER_NAME = "GOVUK-Account-End-Session"
    ACCOUNT_SESSION_DEV_COOKIE_NAME = "govuk_account_session"

    included do
      before_action :fetch_account_session_header
      before_action :set_account_vary_header
      attr_accessor :account_session_header
    end

    def logged_in?
      account_session_header.present?
    end

    def fetch_account_session_header
      @account_session_header =
        if request.headers[ACCOUNT_SESSION_REQUEST_HEADER_NAME]
          request.headers[ACCOUNT_SESSION_REQUEST_HEADER_NAME].presence
        elsif Rails.env.development?
          cookies[ACCOUNT_SESSION_DEV_COOKIE_NAME]
        end
    end

    def set_account_vary_header
      response.headers["Vary"] = [response.headers["Vary"], ACCOUNT_SESSION_RESPONSE_HEADER_NAME].compact.join(", ")
    end

    def set_account_session_header(govuk_account_session = nil)
      @account_session_header = govuk_account_session if govuk_account_session
      response.headers[ACCOUNT_SESSION_RESPONSE_HEADER_NAME] = @account_session_header
      if Rails.env.development?
        cookies[ACCOUNT_SESSION_DEV_COOKIE_NAME] = {
          value: @account_session_header,
          domain: "dev.gov.uk",
        }
      end
    end

    def logout!
      response.headers[ACCOUNT_END_SESSION_RESPONSE_HEADER_NAME] = "1"
      @account_session_header = nil
      if Rails.env.development?
        cookies[ACCOUNT_SESSION_DEV_COOKIE_NAME] = {
          value: "",
          domain: "dev.gov.uk",
          expires: 1.second.ago,
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
govuk_personalisation-0.4.0 lib/govuk_personalisation/account_concern.rb
govuk_personalisation-0.3.0 lib/govuk_personalisation/account_concern.rb