EdmondFrank
How can I pass session from conn to live_dashboard custom page
I want to add a custom page for live_dashboard to show logged user information.
I assigned conn with current_user by plug admin_basic_auth but I only get an empty session list at NotificationPage mount function
# lib/search_box_web/router.ex
pipeline :admins_only do
plug :fetch_session
plug :admin_basic_auth
end
defp admin_basic_auth(conn, _opts) do
user_id = get_session(conn, :user_id)
not_found = fn(conn) -> conn |> Plug.Conn.send_resp(404, "not found") |> halt() end
if user_id != nil do
case Repo.get(User, user_id) do
nil -> not_found.(conn)
user -> assign(conn, :current_user, user)
end
else
not_found.(conn)
end
...
scope "/admin" do
pipe_through [:protect_from_forgery, :admins_only]
live_dashboard "/dashboard",
metrics: SearchBoxWeb.Telemetry,
ecto_repos: [SearchBox.Repo],
additional_pages: [
notification_page: SearchBoxWeb.Live.NotificationPage
]
end
defmodule SearchBoxWeb.Live.NotificationPage do
@moduledoc false
use Phoenix.LiveDashboard.PageBuilder
alias SearchBox.User
alias SearchBox.Repo
@impl true
def menu_link(_, _) do
{:ok, "Notifications"}
end
@impl true
def mount(unsigned_params, session, socket) do
IO.inspect(unsigned_params) #=> %{"page" => "notification_page"}
IO.inspect(session) #=> []
...
{ :ok, assign_new(socket, :current_user, fn -> lookup_user(user_id) end) }
end
...
end
Marked As Solved
EdmondFrank
You can get the session information by get_connect_info/1
But I’m not sure if this is the best option
def mount(_unsigned_params, _session, socket) do
user_id =
get_connect_info(socket)
|> get_in([:session, "user_id"])
if user_id do
{ :ok, assign_new(socket, :current_user, fn -> Repo.get(User, user_id) end) }
end
...
end
Popular in Questions
How to bind a phoenix app to a specific ip address?
could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Hello, how can I check the Phoenix version ?
Thanks !
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
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
Other popular topics
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
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
Hi everyone,
I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
I would like to know what is the best IDE for elixir development?
New
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...
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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









