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:
- A user submits their email and password for authentication.
- If the system successfully authenticates the user, it generates a token (using
Phoenix.Token, JWT, or something similar). - The system attaches the token to the LiveView socket (or session—still deciding).
- 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!
Trending in Questions
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











First Post!- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.
Most Liked
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_bytesand 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.authand spend an hour or so with generated code, it will be very educational!