jeroenbourgois

jeroenbourgois

Set the LiveView socket session opts dynamically

My app (one instance) is accessible from two different domain name, let’s say a.com and b.com.

The app behaves different for each domain, like a different stylesheet. I have a plug that puts a domain assign on the conn which is used down the line. All this works just fine. However, the key name used to sign the cookie was the same for both domain names, which we did not want.

Today I managed to set the cookie key dynamically in a Plug. It feels kind of hacky, but it works. Here is how I solved it:

# The plug module
defmodule MyApp.Plug.Domain do
  import Plug.Conn

  def init(opts), do: opts

  def call(conn, _) do
    domain = decide_domain(conn)
    session_opts = session_opts(domain)

    conn
    |> assign(:domain, domain)
    |> put_private(:plug_session_opts, session_opts)
    |> local_configure_session(session_opts)
  end

  defp decide_domain(conn) do
    host = Map.get(conn, :host, "")

    cond do
      String.contains?(host, "a.dom") -> :domain_a
      String.contains?(host, "b.com") -> :domain_b
    end
  end

  defp local_configure_session(conn, session_opts) do
    opts = Plug.Session.init(session_opts)
    Plug.Session.call(conn, opts)
  end

  defp session_opts(domain) do
    host = Application.get_env(:my_app, :host)
    # SessionOpst is the basic default keyword list as you would expect
    Keyword.merge(SessionOpts.get(), key: Slug.slugify("_#{domain}_key_#{host}"))
  end
end

# early in the router
  pipeline :browser_no_csp do
    plug(:accepts, ["html", "json"])
    plug(MyApp.Plug.Domain)
    ...

# In MyAppWeb.Endpoint
# SessionOpts is a basic opts list for the cookie session, with 'key: "default_key"' because `key` is required.
plug(Plug.Session, SessionOpts.get())

The problem now is that the LiveView socket options remain the default_key and are not overwritten by my plug, causing the page to be redirected all the time. I set the LiveView socket in my Endpoint module as well, like the Plug.Session but it isn’t overwritten.

Is this possible, or am I aproaching this the wrong way?

Marked As Solved

jeroenbourgois

jeroenbourgois

Thanks for your input. @steffend that is exactly what I need. I have a workaround using a dictionary for now, but that would be cleaner.

Also Liked

steffend

steffend

Phoenix Core Team

Note that we’re also planning to move the socket definition into the router in the future. A socket defined there would then also use the regular session options, just as any other plug.

victor23k

victor23k

Hello! I also encountered this problem, and made a proposal to add the conn struct to the arguments in the mfa tuple. The sad part is that it would be a breaking change.

Luckily, @steffend proposed a workaround:

Hope it works for you.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement