我使用的是Guardian 1和Phoenix 1.3。我正在尝试制作一个使用JWT的API。我现在可以对路由进行身份验证了。例如,如果报头中没有有效的令牌,则无法访问get api/users/。
我有一个管道,看起来像这样:
defmodule PhxAuthApi.Auth.AuthPipeline do
use Guardian.Plug.Pipeline, otp_app: :phx_auth_api,
module: PhxAuthApi.Auth.Guardian,
error_handler: PhxAuthApi.Auth.AuthErrorHandler
plug Guardian.Plug.VerifyHeader, claims: %{"typ" => "access"}, realm: :none
plug Guardian.Plug.EnsureAuthenticated
plug Guardian.Plug.LoadResource, ensure: true
end我想要实现的是,如果调用put api/users/1的用户在令牌资源中具有相应的:id,则该用户只能访问该路由。我知道我可以通过调用
resource = Guardian.Plug.current_resource(conn)但是我该怎么做呢?做另一条管道?
我找不到任何关于实现这一点的文档,那会是什么样子呢?
我是Elixir和Phoenix的新手,这是我打算发布的第一个项目。
发布于 2017-11-19 21:03:34
最简单的方法是在Guardian.Plug.LoadResource之后创建另一个要包含在同一管道中的Plug。
此时,资源已经加载,您需要实现call回调来拒绝访问,除非用户在其令牌资源中具有相应的:id。
https://stackoverflow.com/questions/47376401
复制相似问题