Liam_or

Liam_or

Hello : )

would anyone be able to tell me how to protect certain static routes? Why? - plug.static does so much under the hood, I would like to utilise its goodness such as caching and concurrent reads to serve js files from protected routes.

Liam.

Showing Posts 1 to 5

rhcarvalho

rhcarvalho

Depends on what kind of “protection” you want.

Does your app have any existing protected routes?

You could have HTTP Basic Auth, phx.gen.auth email+password accounts, integration with external services for auth, …

ruslandoga

ruslandoga

:wave:

You can add a custom plug in your pipeline that would wrap Plug.Static and implement custom protection logic there.

# assuming a Phoenix project
defmodule MyAppWeb.Endpoint do
  # ...
  # private static files
  plug :private_static, Plug.Static.init(at: "/private", from: :my_app)
  # public static files
  plug Plug.Static, at: "/", from: :my_app, only: MyAppWeb.static_paths()
  # ...

  defp private_static(%Plug.Conn{path_info: ["private" | _]} = conn, plug_static_opts) do
    if private_static_allowed?(conn) do
      # NOTE: if it doesn't match any files, then it would pass-through to router
      Plug.Static.call(conn, plug_static_opts)
    else
      conn |> send_resp(403, "forbidden or whatev") |> halt()
    end
  end

  # pass-through non /private/** routes
  defp private_static(conn, _opts), do: conn

  defp private_static_allowed?(conn) do
    # your custom logic
    true
  end
end

And you can also do it in the router. It might be even easier that way.

Liam_or

Liam_or OP

I would be using phx Auth :blush:

Liam_or

Liam_or OP

Thanks for your example!! :blush:

rhcarvalho

rhcarvalho

In that case combine the plugs generated by phx.gen.auth (mix phx.gen.auth — Phoenix v1.8.8) with the example from @ruslandoga to gate access to Plug.Static.

His example is great, as it leaves space for your own custom logic.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

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
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews