jeroenbourgois

jeroenbourgois

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?

Showing Posts 1 to 4

jeroenbourgois

jeroenbourgois OP

Ok, I am an idiot who does not RTFM… Sorry

socket "/live", MyAppWeb.UserSocket,
  websocket: [connect_info: [session: {__MODULE__, :runtime_opts, []}]]

# ...

def runtime_opts() do
  Keyword.put(@session_options, :domain, host())
end

However, this will not allow me to access the conn object, which I need in order to determine the correct domain name.

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.

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.

jeroenbourgois

jeroenbourgois OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews