|
|
|
|
|
by FinalDestiny
1043 days ago
|
|
As a new Phoenix convert I agree with a lot of these points from my experience too. The functional components make more sense in the majority of cases (and I think the docs point this out) and having the duplicate auth logic with on_mount feels less than ideal. Hopefully the team will see this and make it even better! |
|
All your auth logic (and any other bits you need to set while starting up request state) should be a function in a module somewhere that returns expected values. Then, you just care to set these values to Conn and Socket assigns appropriately, keeping in mind that Conns and Sockets aren’t the same—when you have one, you don’t have the other. Thus, on_mount to the rescue:
1. Use Plug pipeline(s) to call your module function to compute and add bits to Conn.assigns (and you can add those values to the session to avoid computing again, if you’d like).
2. Use on_mount to assign_new:
2a. assign_new pulls from Conn.assigns by key for the dead render while you have a Conn.
2b. The fallback function gets that value again when you no longer have a Conn (recompute or pull from session if already there).
Your actual logic should only exist in one place. And there are easy strategies you can use to avoid expensive computations if necessary.
[Edit]: Not suggesting you don’t understand it, just to be clear. I’ve just experienced a lot of coworkers fighting against, abusing, and misunderstanding Conns, Sockets, and assigns.