andrewb
I have a multi-tenant application where my Users can access more than one tenant. I am authenticating the User at the top level URL (example.com) and then adding the tenants they have access to using a plug. Initially, I am taking the first tenants subdomain and storing that in the conn conn.assigns.subdomain. This all happens in the :browser pipeline in the router.
I am looking for a way I can add the required subdomain to the host so that when I route them to the Dashboard the URL is:
I am using Phoenix 1.4 Dev and used the put_router_url (https://bit.ly/2IxXMDX) function:
def index(conn, _params) do
url = %URI{host: conn.assigns.subdomain <> "." <> conn.host}
conn
|> put_router_url(url)
|> render(to: Routes.dashboard_path(conn, :index))
end
Looking at the source I thought this would then ensure that the generated path would include the subdomain.
Looking for some guidance on the best way to do this, any thoughts appreciated.
Andrew
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











First 8 of 8 Posts
Gazler
I think you are looking for the
_urlhelpers instead of the_pathhelpers._pathwill be a relative path whereas_urlwill be the full URL.andrewb
Thanks Gazler. I had actually changed that after I had posted but I am still unable to redirect to a url with a subdomain after a user is logged in.
chrismccord
Note that your code uses the non-put-router-url’d connection when call dashboard_url. So you need to rebind it, otherwise it is ignoring the previous plug calls:
andrewb
Thanks, Chris. Changing that has me a bit further. I am sure you knew I would run into problems once I started redirecting. I hope you might be able to help a little bit more.
As I mentioned above I am trying to add a subdomain after a user logs in (As they may be able to access multiple subdomains). By default I am directing them to the first subdomain.
I have a Plug.check_session to see if there is a user_id in the session cookie. If there is an there is no subdomain on the host I add in default subdomain and redirect to the new url.
If there is a subdomain I pass it through adding the user as the current_user.
end
I changed my index action on the Dashboard Controller on the basis that the host will include the subdomain by this stage:
Running this when I authenticate it adds the subdomain and redirects but then I end up at the login page, presumably because I get a new session for eth subdomain. I modified the config in my endpoint.ex:
But now I get a CSRF error:
Any direction or examples will be greatly appreciated.
chrismccord
As a sanity check, make sure you clear your cookies or use a new
key. It’s possible the old session is now invalid and throwing the csrf error when it tries to lift it out of the existing cookie, but it’s just a hunch at the moment.andrewb
I have tried both those options but same result. I found this in the docs (Plug.CSRFProtection — Plug v1.20.2):
But I am not sure how or where to use
get_csrf_token_for/1…Any ideas?
chrismccord
In your
form_for’s, you need to pass a non-host specific token if you intend for forms on subdomains to post to a root domain or vice versa. By default, Phoenix HTML’sform_forwill generate a token specific to the host of the form action, if it exists, which sounds like the cause of your issues, but it’s not clear how you are handling subdomains and form action urls. Try doing:andrewb
Chris,
Thank you very much for your time but it was a user error!
I figured it must be to do with the session cookie and sharing it across the main domain and subdomain and after a lot of reading, I ran across an article that mentioned that browsers do not allow cross-domain session sharing for .com and localhost!
One of the many things I have forgotten over the years. Anyway … changed my host file and the config to use local.host and it all worked as planned. The plus side is I have a much better understanding of the
connlifecycle and how Phoenix handles routing.Again thanks for your time. I love Phoenix and can’t wait to see what happens with LiveView.
All the best.
Andrew