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

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
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
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement