Pardon the newbie question, but I've found plenty of examples of guarding function plugs like so:
plug :assign_welcome_message, "Hi!" when action in [:index, :show]
But I have found no examples of how to do this with module plugs:
plug Guardian.Plug.EnsurePermissions,
handler: Mp.Api.AuthController,
admin: [:dashboard] when action in [:protected_action]
Everywhere I seem to move when action in [:protected_action]
either gives me a syntax error or an undefined function when/2
. I know I'm doing something stupid but I can't see what!
Help!
phoenix 1.1.4
Not stupid! Just a result of some syntactic sugar.
Plugs take two arguments, the second is an argument for options. In your example, you want to pass a keyword list as that options argument.
However, the syntactic sugar that lets your drop the square brackets only works if the keyword list is the last argument in the function.
Instead of
plug Guardian.Plug.EnsurePermissions,
handler: Mp.Api.AuthController,
admin: [:dashboard] when action in [:protected_action]
Try the explicit keyword list syntax:
plug Guardian.Plug.EnsurePermissions,
[handler: Mp.Api.AuthController,
admin: [:dashboard]] when action in [:protected_action]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With