Arsenalist

Arsenalist

How to set value in on_mount which can be read in LiveView?

I have the following live session:

  scope "/live", AmplifyWeb do

    live_session :default,
      layout: {AmplifyWeb.Layouts, :admin},
      on_mount: AmplifyWeb.Live.TokenValidator do

      live "/mailing-lists/:mailing_list_id/import-subscribers", ImportSubscribers
      live "/events/performance", EventPerformance

    end

  end

I want to validate a token from the query string in TokenValidator.on_mount and store the user_id from it in session so that it is available to all LiveViews within the live_session.

However, when I set anything on socket or session in on_mount, it is not available in the LiveView. I understand that’s because they are different instances of socket which makes sense. But how do I go about reading an initial value from query string and keeping it around in session or somewhere else where it can persist across LiveViews in the same live_session (and persist it across refreshes)?

Marked As Solved

sodapopcan

sodapopcan

No prob, no matter how easy frameworks make stuff, web programming is the worst (even though I like it) :sweat_smile:

It’s actually best to do it in a plug since you can’t write session data over web sockets (this is a limitation of web sockets, not LiveView). I would make a new project and run mix phx.gen.auth Accounts User users and look at the UserAuth module. The short answer is that you want, Plug.Conn.put_session though. Basically move that whole initial validation logic to a plug and then your on_mount can always read from the session.

Sorry I can’t point you to anything that implements actual API auth.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement