kelvinst

kelvinst

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: phx_gen_auth/priv/templates/phx.gen.auth/auth.ex at de9596102bf59a3333d78330a0ab07f8a9ec8f4d · aaronrenner/phx_gen_auth · GitHub

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.

Marked As Solved

andrewb

andrewb

Also Liked

kelvinst

kelvinst

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.

redrapids

redrapids

Author of Adopting Elixir

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!

slouchpie

slouchpie

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.

Last Post!

ani

ani

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.

Where Next?

Popular in Questions Top

ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement