smon

smon

Let’s me start out with a new project based on generators:

Using mix phx.gen.live gives me form_component.ex, index.ex and show.ex for my schema. The latter two use the form_component.ex as a modal:

<.modal :if={@live_action in [:new, :edit]} id="project-modal" show on_cancel={JS.patch(~p"/projects")}>
  <.live_component
    module={DemoWeb.User.FormComponent}
    id={@user.id || :new}
    title={@page_title}
    action={@live_action}
    project={@user}
    patch={~p"/userss"}
  />
</.modal>

or

<.modal :if={@live_action == :edit} id="project-modal" show on_cancel={JS.patch(~p"/projects/#{@project}")}>
  <.live_component
    module={DemoWeb.ProjectLive.FormComponent}
    id={@project.id}
    title={@page_title}
    action={@live_action}
    project={@project}
    patch={~p"/projects/#{@project}"}
  />
</.modal>

Now I am having a look at mix phx.gen.auth and was wondering how I could restrict only the modal usage (both :new and :edit) to registered users.

Ideas so far:

  • Create a standalone live view form.ex and replace form_component.ex. But then I can not use it inside a modal (?). This way I could set authorization in my router.ex.
  • Add a check in the relevant apply_action/3 in index.ex and use the same pattern in show.ex?
  • Add checks to both existing live views: .modal :if={@live_action in [:new, :edit] and @current_user} in the heex?
  • Pass the :current_user to the live component and let the live component handle the check?

I would prefer to handle this in the router.ex, just because then all restriction related logic would be defined at one point.

Any other ideas?

Showing Posts 1 to 6

LostKobrakai

LostKobrakai

The modals in the default boilerplate have separate routes. Add authorization to those.

smon

smon OP

How would you do that from the router?

The generated authentication logic seems only to check “on_mount” for live views and the initial “dead view” requests. If a guest user is on the :index route (i.e. successfully mounts the view) and then switches to the edit route which is supposed to be restricted, they still use the same live view process, there is no mount call anymore and there is no second evaluation happening.

LostKobrakai

LostKobrakai

You could switch the patch to be a proper redirect. More expensive on resoure loading, but giving you another mount.

smon

smon OP

Ah, right - the patching in my links was what confused me.

Instead of switching all patches to redirect I would simply render those links only if current_user is set? I am still struggling to evaluate where I might run into security issues with the LiveView Javascript magic.

codeanpeace

codeanpeace

Definitely ensure that the handle_event callback for the form submission/save event checks for authorization – in your case, that the user is registered.

Every time the user performs an action on your system, you should verify if the user is authorized to do so, regardless if you are using LiveViews or not. For example, imagine a user can see all projects in a web application, but they may not have permission to delete any of them. At the UI level, you handle this accordingly by not showing the delete button in the projects listing, but a savvy user can directly talk to the server and request a deletion anyway. For this reason, you must always verify permissions on the server.

In LiveView, most actions are handled by the handle_event callback. Therefore, you typically authorize the user within those callbacks. In the scenario just described, one might implement this:

Events Consideration | LiveView Security considerations

krasenyp

krasenyp

I’d hide all the actions which the current user can’t perform.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews