ggerico

ggerico

Phoenix guide, Context: call a plug function in a Router scope

I try to reproduce the “Context” example in the phoenix guide. But instead of defining authenticate_user in the router.ex file, I did it in another module called SessionAuth under the web directory which imports only Plug.Conn with a few aliases MyApp.Router.Helpers, Phoenix.Controller and MyApp.Accounts.

in the router:

scope "/cms", MyAppWeb.CMS, as: :cms do
pipe_through [:browser, &MyApp.SessionAuth.authenticate_user/2]
resources "/pages", PageController
end

Then I get

= Compilation error in file lib/my_app_web/router.ex ==
** (ArgumentError) argument error
    :erlang.atom_to_list(&MyAppWeb.SessionAuth.authenticate_user/2)
    (plug) lib/plug/builder.ex:186: Plug.Builder.init_plug/1
    (plug) lib/plug/builder.ex:181: anonymous fn/4 in Plug.Builder.compile/3
    (elixir) lib/enum.ex:1811: Enum."-reduce/3-lists^foldl/2-0-"/3
    (plug) lib/plug/builder.ex:181: Plug.Builder.compile/3

when authenticate_user is defined and called from the router, there is no error.

when

import MyAppWeb.SessionAuth, only: [authenticate_user: 2]
pipe_through [:browser, :authenticate_user]

there is no error

Most Liked

idi527

idi527

I think

pipe_through [:browser, &MyApp.SessionAuth.authenticate_user/2]

is the same as

pipe_through [:browser, fn conn, opts -> MyApp.SessionAuth.authenticate_user(conn, opts) end]

and pipe_through macro expects a list of atoms to call something like apply/3 on, not functions.

I usually call these plugs App.Web.Session or App.Web.Auth.

kylethebaker

kylethebaker

I usually do it using module plugs:

pipeline :auth do
    plug MyApp.Plugs.AuthUser
end

Then add :auth to your pipeline.

In this case Plugs.AuthUser is a module plug that contains the required init and call functions: Plug — Phoenix v1.8.8

There is possibly a way to it how you’ve described though. I’m not sure what the recommended way would be, but I prefer putting the plug it it’s own module.

I think the issue you’re having is that pipe_through accepts a list of pipelines, so you aren’t able to pass it a function directly.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement