jerrygolero

jerrygolero

Hi everyone!

I’m working on an authentication system for my app using Phoenix LiveView and considering implementing token-based authentication (e.g., with Phoenix.Token or JWT). I’d love to hear how others approach this, especially when it comes to safely handling reconnections after the WebSocket connection drops.

Here’s the workflow I’ve been ideating:

  1. A user submits their email and password for authentication.
  2. If the system successfully authenticates the user, it generates a token (using Phoenix.Token, JWT, or something similar).
  3. The system attaches the token to the LiveView socket (or session—still deciding).
  4. If the user’s WebSocket connection drops (e.g., due to network issues), the browser attempts to reconnect.

Here’s where I’m stuck:

  • How can I ensure the connection safely reuses the already-generated token during the LiveView reconnection process?
  • Should I rely on the LiveView session or some other mechanism to persist and validate the token securely for subsequent reconnect attempts?
  • Are there any gotchas I should watch out for when integrating token-based authentication into a LiveView app?

Any advice, insights, or examples from your own implementations would be greatly appreciated!

Thanks in advance!

Showing Posts 1 to 2

D4no0

D4no0

Each time a new re-connection happens it still begins with a http upgrade connection request/response, so all security considerations that go for http requests like CSRF are valid concerns here too.

IMO a generic phoenix project provides great defaults for security, add to that all security hardening that goes for https and you are good to go with the default setup.

hubertlepicki

hubertlepicki

You should probably use mix phx.gen.auth even on a side project just to follow the code if you want to implement it from scratch. They’re using Phoenix.Token and save it in the session.

There should be nothing you have to do for session re-connects.

Also: you can use Phoenix.Token or JWT but you do not have to.

What phx.gen.auth is doing is actually different - they do not use either. They just generate a simple random token value using :crypto.strong_rand_bytes and then insert that token along with user_id to a database table.

After that’s done, the value of the token is set in the session.

Session is signed, so in theory they could have put user_id there, but their approach is smarter as they store the token in the session, but the mapping to user_id on server, which allows them to invalidate the session wherever they want by just deleting the token on the server.

This is the approach I have adopted across my apps, not just LiveView, and this has saved me a lot of trouble.

Phoenix.Token or JWT could be used instead of Phoenix sessions. You usually use them with APIs. They’re not suitable for session/cookie based authentication because of their size, which can be big and your session storage is probably in cookie, meaning that it has a hard limit that is not too big at all.

So that’s my gotcha at the end. I do advise you use mix phx.gen.auth and spend an hour or so with generated code, it will be very educational!

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews