How to protect and hide a route with 404 page?


I would like to protect and hide an admin area of my site. For example, I would like domain/admin area to be available only for admins and if everyone else tries to access it, I would like to show 404 error to them. I built a function which loads 404 page for non-admins and it works fine but in browser network tab, it shows 200 status instead of 404.

I use this code to get 404 page

|> put_root_layout(html: false)
|> put_layout(html: false)
|> put_view(TestApp.ErrorView)
|> render("404.html")
|> halt()

Is there a way to create a 404 status or is there some better approach to accomplish this?

You can set the HTTP status to be sent with the response using Plug.Conn.put_status

1 Like