         conn
         |> put_flash(:info, "Invitation completed.")
         |> render("show.html", invitation: invitation)
-      nil ->
-        conn
-        |> put_flash(:error, "Invalid invitation.")
-        |> redirect(to: page_path(conn, :index))
+      nil -> handle_error(conn, "Invalid invitation.")
     end
   end
 

    case invitation_res do
      %Invitation{} = invitation ->
        user = create_user(invitation)
        if has_membership?(event, invitation, user) do
          conn
          |> put_flash(:error, "Already accepted the invitation.")
          |> redirect(to: page_path(conn, :index))
        else
          membership = create_membership(invitation, user)
          invitation_changeset = Invitation.completion_changeset(invitation, %{


          redirect(conn, to: invitation_path(conn, :show, invitation.event.slug, invitation.invitation_token))
        end
      nil -> handle_error(conn, "Invalid invitation.")
    end
  end

  # Private methods
  defp load_event(conn) do
    Repo.get_by!(Event, slug: conn.params["event_slug"])
  end

  defp handle_error(conn, flash_message) do
    conn
    |> put_flash(:error, flash_message)
    |> redirect(to: page_path(conn, :index))
  end

  defp create_user(%Invitation{} = invitation) do
