I want to create Plug for user Authorization,so i would to know what kind plug to use, Functioal or Module?
It depends…
If your plug needs some intialisation, go for a module-plug, if not use a function-plug.
I need make query to database with user_id and dept_id , so i can do that query on init , from that then i pass the result to call, so in this scenario, module plug best approach right?
Where are user_id and dept_id comming from? Are you able to write plug YourAuth, user_id: user_id, dept_id: dept_id in the code?
I always thought that function plugs can only be used in the module in which they are defined. Therefore I always use module plugs when I need to reuse them in other modules.
Is there any way to reuse function plugs defined in another module?
I am using guardian as auth framewrok so i can get logged in user id by using Guardian.Plug.current_resource(conn) and dept_id will come from url , for example http://localhost:4000/department/22
Is this information available in conn? If you can answer this with yes, you probably won’t need a module plug.
An approach I often use, is to start with a module-plug, and just don’t do anything in its init/1 function. This way I have better separation of concerns when I have multiple plugs and can extend easily without having to rename the plug if I really need the init-stuff.
thanks man, its better to start create module plug without doing anything in init/1, so that we can modify plug definitiion in one place
I believe imported functions can be used with the plug :atom syntax also.
Eg Phoenix.Controller — Phoenix v1.8.8 is imported from Phoenix.Controller when use Phoenix.Router, making it available to use in a Router pipeline.






















