Reputation: 353
I am doing a project for an elixir / phoenix framework. There was a question on authorization. For example, I have a route:
get "/dashboard", DashboardController, :index
I want only logged-in users to be able to go this route. As I present this process: the user goes along this route, it checks that the user is logged in. If yes, then the controller function that processes this route is called, if not, then a redirect to the login page occurs. Tell me, please, how to correctly implement this in phoenix framework? There can be many similar routes, I would like to have 1 handler for this.
Upvotes: 1
Views: 714
Reputation: 2089
I am using pow for authentication. I have the following pipeline:
pipeline :protected do
plug Pow.Plug.RequireAuthenticated,
error_handler: Pow.Phoenix.PlugErrorHandler
end
Then, I just need to pass my scope through the right pipe:
scope "/dashboard", MyAppWeb do
pipe_through [:browser, :protected]
get "/", PageController, :dashboard
end
All the paths that require authentication would go there. If you want it for some other library/implementation, the approach should be similar. You can see an example of authentication using Guardian
in here, where the scope is used in the same way.
Upvotes: 1