lehoff

lehoff

Phoenix LiveView cookie without authentication

Hi,

I am trying the help a local restaurant by putting up a simple website for ordering take-away.

I want to leverage LiveView to show the current order items on the right hand side.

I need to have a cookie based solution that preserves the order items when clicking on links (sub-menus for starters, mains, desserts, etc).
So far I have not been able to make it work and I have not been able to find any solutions/hints online.
I have tried using the _csrf_token, but it is not always present in the session.

I want to avoid forcing the customer to create a login - just have them state a mail/cell number so that the restaurant can tell when their order is ready. Everything is paid for at pick-up, no online payment (I’m just doing this as a favour to the local restaurant, so not going to open that box of goodies).
I have found several authentication related solutions for LiveView, but that is not the use case I’m after.

As far as I can tell it would be possible to control this better using straight Phoenix as the problem is related to the use of websockets in LiveView. I just fancy the idea of a live update of the current order in the style of LiveView. If it is impossible I will just revert to “plain” Phoenix, but that feels like an unnecessary defeat.

I am not an expert in Phoenix (LiveView) so please forgive me for my ignorance.
I hope that I have explained the problem well enough.

Cheers,
Torben

Most Liked

mindok

mindok

Hi Torben,

There’s a discussion here that may give you a few ideas:

Or, you could use javascript hooks as described here: Persistent session data in Phoenix LiveView – The Pug Automatic - it’s a bit more roundabout, particularly if you want to share session state between liveviews.

dnsbty

dnsbty

Because the LiveView isn’t reloading the page, it isn’t really possible to set cookies server side.

Your best bet would be to either use JavaScript to set the cookie client side or to use AJAX to make it work similar to the solution outlined here:

lehoff

lehoff

@wmnnd

We got it to work with PhoenixLiveViewSession and managed to cut it down to the bare minimals:

  • Add the dependency.
  • Simple do-nothing handling of {:live_session_updated, session} in handle_info:
def handle_info({:live_session_updated, session}, socket) do
    {:noreply, socket}
end
  • Use PhoenLiveSession.maybe_subscribe/2 in mount/3:
socket = assign(
  socket,
  dishes: DatabaseLogic.get_dishes
)
|> PhoenixLiveSession.maybe_subscribe(session)

We have used for a long time now and it just works™.

Last Post!

wmnnd

wmnnd

Awesome :grinning:

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement