Elixir/Phoenix 框架 authentication/authorization

Elixir/Phoenix Framework authentication/authorization

我正在为 elixir / phoenix 框架做一个项目。有一个关于授权的问题。比如我有一条路线:

get "/dashboard", DashboardController, :index

我希望只有登录的用户才能走这条路。正如我介绍的这个过程:用户沿着这条路线前进,它会检查用户是否已登录。如果是,则调用处理这条路线的控制器函数,如果没有,则会重定向到登录页面。请告诉我,如何在 phoenix 框架中正确实现它?可以有很多类似的路由,我想为此设置 1 个处理程序。

我正在使用 pow 进行身份验证。我有以下管道:

 pipeline :protected do
    plug Pow.Plug.RequireAuthenticated,
      error_handler: Pow.Phoenix.PlugErrorHandler
  end

然后,我只需要通过正确的管道传递我的范围:

 scope "/dashboard", MyAppWeb do
    pipe_through [:browser, :protected]
    get "/", PageController, :dashboard
  end

所有需要身份验证的路径都会到那里。如果您想要其他 library/implementation,方法应该类似。您可以看到使用 Guardian in here 进行身份验证的示例,其中范围的使用方式相同。