How to use phx gen auth in a live application?

So, I am starting a new phoenix project, and of course I’m going to implement it all using live views. So I have one question: does phx_gen_auth work well with live views? I know that the generated code does generate only dead views, and I don’t care too much about that, I will probably replace them with live views in the future.

But there is this line that got me thinking:

It looks like there is actually a right way to put phx_gen_auth and live views together, but I couldn’t find it on the docs and neither found any tutorial explaining that specifically. Do I have to do something else on my live views? Besides manually setting the user to the socket assigns, and authorization, which I already noticed I have to.


Alright, I actually think I discovered, the live_socket_id key on session is used by LiveView under the hood to identify connections, so if the value changes and the connection gets down, it will fail reconnecting cause the session is not the same anymore. So no extra work needed here, only the work I already expected with the assigns and authorization.


Hi … check this out


Oh! Thank you so much! That makes it a lot clearer :smiley:

1 Like

And thanks @redrapids for the video :smiley: I already was interested on, now that I had a little taste of it I’m keen to sign up for it.

1 Like

Many thanks! Allow me to say a few words about our editorial process. We consider three things: MY interest in a topic (passion comes through in teaching); content PERFORMANCE, and the overarching Groxio mission: teach programmers interesting concepts in a fun and engaging way on a regular basis.

We were in the air about doing another Elixir series, because the market is pretty saturated compared to the language size. But the LiveView one was popular enough that we decided to continue for one more year, adding sessions for nerves, Ecto, and property-based testing.

So for those who like it, the ways you can advocate for more are:

  • subscribe to our channel (we’re currently short about 100 subscribers to start monetizing that content)

  • tell your friends on twitter to follow us and be noisy (we’re @groxioLearning and @redrapids and our marketing is grass roots social media),

  • get a paid subscription. The way we measure the effectiveness of content is how many yearly subscribers we get while a module is active.

In short, we love Elixir and want to keep producing content, but it has to put food on the table first!

An offer for those on the board. Subscribe annually, and we’ll let you keep the Elixir content we’ve released so far:Elixir/OTP/LiveView (and our subscription model means you will also get to keep the Nerves, Ecto, and prop test modules we will build later this year. Just tweet that you’ve subscribed, and at me: @redrapids.

Thanks again for your kind words!


Thanks so much!

1 Like

For posterity, the accepted answer (the youtube video) does not actually:

Instead it uses the autogenerated HTML and controllers from phx_gen_auth and shows how to integrate that with a LiveView page. It does not solve the problem of having, e.g. registration form in a live view.

I briefly mentioned my own approach to a similar problem here: Phx_gen_auth using live view ONLY
If anyone wants more details on it, let me know.


That is nice thanks. But I see in the video he’s doing an additional query to fetch the user with the token, but fetch_current_user already does that query, so i wonder how to assign the existing fetched user to the LiveView socket (it is already attached to the conn)

You can do that by using assign_new. There is a guide on how to use it with an on_mount hook in the LiveView docs here.

1 Like