Of course! So, as I understand, you have two LiveViews, EventIndex
and EventIndexAuth
, which both use the ListEventsComponent
to show a list of all events.
Do I understand it correctly that you handle the events from the ListEventsComponent
in your LiveViews, so in EventIndex
and EventIndexAuth
? If yes, that’s smart since you keep the ListEventsComponent
generic and let its parent handle the save&remove events.
As I see it, you will have some duplication inevitably since you try to offer the same feature to two user groups (authenticated
and unauthenticated
). However, let’s just brainstorm on some solutions so that you can pick the most suitable one for your use-case.
-
You let the ListEventComponent
handle authenticated vs. unauthenticated behavior. So, whenever a save
or remove
event comes in, the LiveComponent checks whether it has a user_id
and either executes the action or redirects to the login-page.
Pro: Everything is in one place and it’s easy to understand the checking-logic. You can reuse the component everywhere without having to worry about whether a user is logged in. You can use a single LiveView and don’t have code duplication.
Con: The LiveComponent receives the responsibility of authentication management, which you’d preferably move as much up the chain as possibly (i.e. into the router). Imagine you’d have many of these components, then you need to check this in every single component.
-
You keep your current solution with two LiveViews which handle the authentication.
Pro: You can customise the authenticated and unauthenticated parts of your website better. Maybe you want to have a helper text underneath each event saying: “You’ve gotta login to use all the features”. You only have to handle the authentication in the mount
-function of each LiveView and don’t have to worry about it in the other functions or in the LiveComponent. You keep your LiveComponent generic, but have to handle its events always in its implementing LiveView. You cleanly separate the authenticated from the unauthenticated part of your website.
Con: Duplication of the event-handler, possibly of the templates and views. If the authenticated LiveView does not differ much from the unauthenticated LiveView, this feels redundant and you will forget why you’ve done this down the road.
-
You move the authentication management further down into e.g. the Context
. So, you always call the context and let it check whether the user is logged in (if you somehow have a user_id
even if the user isn’t logged in) and possibly has the permissions to execute the action. If not, then you return an error and the LiveView has to handle that error by e.g. redirecting to the Login-page with an error flash message.
Pro: By checking the authentication in your context you will make sure that the user will always have the permission to execute this action. If you do this “further up” e.g. in your LiveView, then you might forget to check for it, creating a security hole.
Con: You probably won’t have a user_id
if the user isn’t logged in, so this solution can only check for permissions but not for authenticated vs unauthenticated state.
So, again this is a judgement call. I’d prefer solution 1 (checking the auth-status inside the LiveComponent) if you don’t expect to have many components where the behaviour differs between the authenticated vs. unauthenticated user group. So, if it’s only this single component, then only have a single LiveView with this LiveComponent, and check inside the component whether to execute the action or redirect to the login-page.
If, however, you expect to have many components of your event list where the behavior depends on the logged-in status of the user, then I’d prefer solution 2 (have two LiveViews use the same LiveComponent). This way you can easily always redirect to the login-page from inside your unauthenticated LiveView, which prevents you from creating “security holes” (i.e. features where unauthenticated users can execute actions they aren’t supposed to). Also, you can then focus on implementing these actions in your authenticated LiveView without having to worry about checking the logged-in status in every event handler.
What do you think? Does this help you further? 
Edit: I assumed that you use a LiveView for this, but if you use a Phoenix Controller for handling these events, there might be an easier solution by defining an action_fallback-Controller, which redirects to your login-page if a user tries to execute an action without being authenticated. So, you’d simply match against a user_id
in your controller event handler. If the user isn’t logged in, they won’t pass in a user_id
parameter, so no event handler matches and the request goes to the action_callback
controller. This is like solution 2, but you don’t have to write too much duplicate code in order to achieve it 