Auth for particular function inside controller

Now i have some functions inside a controller.

function-1
function-2
function-3

In that i need to make some authorization check to only function-2. For that i have created one Plug. Now how can i call that plug only to function-2 inside the controller??

AFAIR you can write plug :auth when action in [function-2]

2 Likes

Thank you :slight_smile:

@NobbZ is right.

I have this working example with Guardian.

  plug Guardian.Plug.EnsureAuthenticated, handler: AppWeb.Auth.UeberauthController
  plug Guardian.Plug.EnsurePermissions, [handler: AppWeb.Auth.UeberauthController, one_of: [%{group: [:read]}, %{mgmt: [:read]}]] when action in ~w(index)a
  plug Guardian.Plug.EnsurePermissions, [handler: AppWeb.Auth.UeberauthController, one_of: [%{group: [:write]}, %{mgmt: [:write]}]] when action in ~w(new create edit update change delete)a  
  plug Plugs.AuthenticateUser
  plug :scrub_params, "company" when action in ~w(create update)a