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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
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
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

Other popular topics Top

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 130579 1222
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement