dodo
Phoenix LiveDashboard with token authentication
I am working on a Phoenix Json API and would like to enable the live dashboard in production. My :require_authentication plug checks the Authorization header for a valid token, is there a way to integrate this into the live dashboard?
Most Liked
msimonborg
You can also drop your route into a preexisting authenticated scope if you have one
scope "/" do
pipe_through [:browser, :require_authentication]
# ... other authenticated routes
live_dashboard "/dashboard"
end
or, if you only want to require authentication in production but not in other environments:
live_dashboard_pipeline =
case Mix.env() do
:prod -> [:browser, :require_authentication]
env when env in [:dev, :test] -> :browser
end
scope "/" do
pipe_through live_dashboard_pipeline
live_dashboard "/dashboard"
end
You may want to conditionally render the link as well:
<%= if function_exported?(Routes, :live_dashboard_path, 2) && @current_user do %>
<%= link "Dashboard", to: Routes.live_dashboard_path(@conn, :home) %>
<% end %>
hubertlepicki
I have something like this in my router:
pipeline :protected do
plug MyApp.RequireAuthenticated
end
scope path: "/admin/dashboard" do
import Phoenix.LiveDashboard.Router
scope "/" do
pipe_through(:browser)
pipe_through(:protected)
live_dashboard "/"
end
end
Which is basically created an authenticated pipeline, that has one Plug that you seem to already have. Then, create a scope at where you want to have your dashboard, and make that scope go through the pipeline you just defined.
hubertlepicki
I don’t see the problem, and maybe you don’t need to write a custom plug but use this Plug.BasicAuth — Plug v1.20.2 ?
Popular in Questions
Other popular 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
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








