EdmondFrank

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

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

Where Next?

Popular in Questions Top

nobody
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
electic
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ycv005
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
freewebwithme
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
bsollish-terakeet
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
script
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
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

Other popular topics Top

New
Harrisonl
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
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
komlanvi
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
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 127089 1222
New
PeterCarter
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
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement