egze

egze

I was moving my personal site to Phoenix LiveView and noticed the there is always a session cookie present.

I’m wondering if it’s possible to run a LiveView application without cookies. There is this EU cookie law that there needs to be a warning if the site is using cookies and I would like to just have no cookies at all, to skip this cookie popup.

So the questions is:

  1. How to run a Phoenix site without sessions/cookies?
  2. How to run a LiveView enabled Phoenix site without sessions/cookies?

Showing Posts 1 to 10

lucaong

lucaong

Not a lawyer, but the EU cookie law does not disallow all cookies. It explicitly allows those “strictly necessary for the delivery of a service requested by the user”, with one such example being a cookie used to implement a shopping cart in an e-commerce application. As long as you don’t use the session cookie for other tracking purposes not necessary for the functioning of your app, you should be fine.

The session cookie is in most cases “strictly necessary”, as your app cannot really work without, if it has to maintain any state. Conversely, third party cookies and tracking pixels are not strictly necessary, and would definitely fall into the ones you should be able to opt-out from. In between, there is a large gray area.

Here’s Wikipedia on that: ePrivacy Directive - Wikipedia

egze

egze OP

That’s the thing - I don’t want to get some letter from a lawyer and then explain to him the situation. If it is possible I just want to disable the source of the problem - cookies. Then I just don’t care.

Please let’s not debate the law and how to interpret it.

LostKobrakai

LostKobrakai

The phoenix part is quite simple: remove the :fetch_session plug and all plugs depending on it. Also do not manually set cookies / use the session.

Same should be possible for live_view I guess, but again you cannot use any stuff depending on it, which by now should only be flash messages.

egze

egze OP

If I do that, the LiveView keeps crashing:

[debug] LiveView session was misconfigured or the user token is outdated.

1) Ensure your session configuration in your endpoint is in a module attribute:

    @session_options [
      ...
    ]

2) Change the `plug Plug.Session` to use said attribute:

    plug Plug.Session, @session_options

3) Also pass the `@session_options` to your LiveView socket:

    socket "/live", Phoenix.LiveView.Socket,
      websocket: [connect_info: [session: @session_options]]

4) Define the CSRF meta tag inside the `<head>` tag in your layout:

    <%= csrf_meta_tag() %>

5) Pass it forward in your app.js:

    let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content");
    let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}});

I’m having doubts that it can be used without session.

LostKobrakai

LostKobrakai

This should only pop-up if you supply a :session key for the connect_info options:

egze

egze OP

Yepp. If I change

socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]

to

socket "/live", Phoenix.LiveView.Socket

Then it works and no cookies are created. Sweet.

egze

egze OP

Actually, the issue is not solved yet :frowning: In dev I see no cookies. but…

There are 2 problems:

  1. In production there is still a cookie being created. Could not figure out yet, what is causing it.
  2. Tests for live views don’t work anymore. I think they are always trying to load something from the session. It is strange, because in development the live_view works perfectly fine and no cookies are created.
1) test shows privacy policy (AppWeb.PageLiveTest)
     test/byteflip_web/live/page_live_test.exs:23
     ** (ArgumentError) session not fetched, call fetch_session/2
     code: {:ok, live_view, _disconnected_html} = live(conn, "/")
     stacktrace:
       (plug 1.10.0) lib/plug/conn.ex:1557: Plug.Conn.get_session/1
       (phoenix_live_view 0.12.1) lib/phoenix_live_view/test/live_view_test.ex:343: Phoenix.LiveViewTest.do_connect/7
       test/app_web/live/page_live_test.exs:24: (test)

What is more strange. In production if I disable cookies in my browser - the page keeps reloading.

egze

egze OP

Fixed the problem 1. Was a stupid mistake on my end. Was untar-ing the release in the wrong folder. Oops :slight_smile:

Additionally what I did: in app.js removed the csrf_token thing. and in the layout removed also the csrf token tag.

Still don’t know what to do with tests.

egze

egze OP

And it’s fixed in master. Got the hearts from José himself. Feeling pretty good about myself :smiley:

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
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
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
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews