凤凰路由器范围问题
Phoenix router scope issue
我该怎么做,我向 api/events 发送了一个 GET 请求,我只收到了 public 个事件,但如果我发送了一个授权令牌,那么我就收到了用户的事件?
基本上使用相同的路线,但如果有用户登录则返回不同的东西。
我相信它必须对路由器做一些事情,如果我只发送到 /api 范围,它永远不会有用户,但如果我发送到 [:api, :jwt_authenticated] 没有令牌那么它显然说我没有登录。
如果未登录,我尝试单独定义返回,但结果相同
scope "/api", DotooAppWeb do
pipe_through(:api)
get("/users", UserController, :index_not_logged)
post("/users", UserController, :create)
get("/events", EventController, :index_not_logged)
scope "/auth" do
post("/identity/callback", AuthenticationController, :identity_callback)
end
pipe_through(:jwt_authenticated)
resources("/users", UserController, only: [:index, :create, :show, :update, :delete])
resources("/events", EventController, only: [:index, :create, :show, :update, :delete])
end
有办法解决这个问题吗?
我关注这篇文章主要是为了设置我的身份验证:http://blog.nathansplace.co.uk/2018/ueberauth-and-guardian
我认为这不应该发生在路由器中,尽管从技术上讲你可以让它工作。
如果您的动机是将这两种情况的逻辑分开,您可以让 UserController.index
函数只检查用户是否已登录,然后委托给获取 public / 用户的函数-特定事件。它还具有非常透明的优点,因为确定端点 returns 的所有逻辑都在一个地方。
希望对您有所帮助!
我该怎么做,我向 api/events 发送了一个 GET 请求,我只收到了 public 个事件,但如果我发送了一个授权令牌,那么我就收到了用户的事件? 基本上使用相同的路线,但如果有用户登录则返回不同的东西。 我相信它必须对路由器做一些事情,如果我只发送到 /api 范围,它永远不会有用户,但如果我发送到 [:api, :jwt_authenticated] 没有令牌那么它显然说我没有登录。 如果未登录,我尝试单独定义返回,但结果相同
scope "/api", DotooAppWeb do
pipe_through(:api)
get("/users", UserController, :index_not_logged)
post("/users", UserController, :create)
get("/events", EventController, :index_not_logged)
scope "/auth" do
post("/identity/callback", AuthenticationController, :identity_callback)
end
pipe_through(:jwt_authenticated)
resources("/users", UserController, only: [:index, :create, :show, :update, :delete])
resources("/events", EventController, only: [:index, :create, :show, :update, :delete])
end
有办法解决这个问题吗? 我关注这篇文章主要是为了设置我的身份验证:http://blog.nathansplace.co.uk/2018/ueberauth-and-guardian
我认为这不应该发生在路由器中,尽管从技术上讲你可以让它工作。
如果您的动机是将这两种情况的逻辑分开,您可以让 UserController.index
函数只检查用户是否已登录,然后委托给获取 public / 用户的函数-特定事件。它还具有非常透明的优点,因为确定端点 returns 的所有逻辑都在一个地方。
希望对您有所帮助!