roganjoshua

roganjoshua

The problem I am trying to solve is this:

There is no authenticated or session user per se, just the browser.
When someone lands on the page I ask them to select a train station as their home train station. I want to store this somewhere, cookie?, so that when they return, that station will be their home station, until they change it.

It isn’t really a session thing, since their is no authentication, I am trying to make it as effortless for the user as possible.

Or is it better to just store it in session anyway?

Showing Posts 1 to 5

BartOtten

BartOtten

Sessions: are gone when the browser is closed

Cookie: Great for permanent data like a location. However, you can’t access cookies from the socket.

To safe-guard your application, Phoenix limits and validates the connection information your socket can access. This means your application is safe from these attacks, but you can’t access cookies and other headers in your socket.

The solution when using LiveView? Read the cookie in a conn plug, set it in the session and pick it up in the LiveView again.

LiveView access to cookies

Local storage: this is not send to the server but you might be able to preselect the right location with a Javascript hook. This saves you the hassle of a cookie read, session set and session get.

roganjoshua

roganjoshua OP

Hi Bart

Thanks for the response but I don’t think I need to use liveview on the landing page right?

So when the user hits the site for the first time, I ask for permission to location, then I find the nearest station, I suggest that station might be their home station and if it is or they choose one I set the cookie accordingly.

None of that needs to be liveview but ordinary Phoenix?

Once I have the cookie I can start using Liveview ?

BartOtten

BartOtten

Not sure I understand. LiveView is never a mandatory technique, but when used is might brings benefits over ‘dead views’ in scenario’s with a certain ration of interaction.

Assuming the server is the one finding the nearest location and you don’t mind an old-school full page ‘refresh’ after submitting the location to the server, dead views can do just fine (and simple with cookie). You have a hidden formfield, put the detected location in, submit the form and just handle the form as we have done for decades.

Once you want to use LiveViews in combination with the location, you need to somehow transfer the data in the cookie to the live view process (which can be on an different machine). So usually this is done by reading it in a dead view, setting it in the session and read the session once the live view is mounted.

garrison

garrison

Session has two meanings here:

First, there is the Plug.Session which is a k/v store (often called a Session in webdev) which stores data either in a cookie on the client (Plug.Session.COOKIE) or in an :ets table (Plug.Session.ETS).

Then, there is what is termed a “session cookie”, which is a cookie with no expiry set. When you set a cookie with no expiry (or max-age), the browser will delete that cookie “when it feels like it”, which is usually when the window or browser is closed (but not always).

If you use Plug.Session out of the box, you are probably using the Plug.Session.COOKIE store with the default settings. The default max-age for the Plug.Session cookie (which is configurable) is nil, meaning the cookie will have no expiry. This leads to the following unfortunate statement:

The “Session Cookie” is stored in a “session cookie”.

But it does not have to be a “session cookie” in which you store your “Session Cookie”, because you can set the :max_age option for Plug.Session, and then your Session will survive the browser being closed :slight_smile:

But yeah, in practice you can just set a cookie and then load it into the session instead of doing that. That’s what phx.gen.auth does.

garrison

garrison

BTW, to actually answer the OP:

It is fine to store your flag in a cookie.

If you decide you want to use LiveView, you won’t be able to access the cookie directly, but you can copy it from the cookie to the session and then to the assigns. If you want to see an example of this, run phx.gen.auth on an empty project and have a look at user_auth.ex, particularly ensure_user_token/1 and on_mount/4.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews