Share logic across `Plugs` and `LiveView` `on_mount` callbacks

Hi :wave:

Phoenix recommends having the authorization logic in both plugs that are executed before the LiveView session is mounted and in LiveView on_mount callbacks that are executed when a user navigates from one view to another. This suggestion made me think about the potential inconsistencies that might arise between the logic that runs for the standard requests vs. LiveView sessions.

Is this a known challenge already solved by any of Phoenix’s building blocks or an external library? I was looking for a solution that allows defining the authorization logic that’s transport-agnostic and that via macros can generate the Plug and LiveView-specific logic.

Thanks!

No need for macros. Just define a function and reuse it. Elixir is a functional language.

2 Likes