【问题标题】:Guardian - exclude routes from needing authenticationGuardian - 排除需要身份验证的路由
【发布时间】:2021-07-27 06:28:36
【问题描述】:

目前我已使用以下配置设置监护人身份验证:

pipeline :api do
  plug :accepts, ["json"]
  plug MyApp.AuthAccessPipeline
end
defmodule MyApp.AuthAccessPipeline do
  use Guardian.Plug.Pipeline, otp_app: :my_app

  plug Guardian.Plug.VerifySession, claims: %{"typ" => "access"}
  plug Guardian.Plug.VerifyHeader, claims: %{"typ" => "access"}
  plug Guardian.Plug.EnsureAuthenticated
  plug Guardian.Plug.LoadResource, allow_blank: true
end

我的路线是这样设置的:

scope "/api/v1", MyAppWeb do
    pipe_through :api

  resources "/users", UserController, except: [:new, :edit]

  post "/auth/sign_up", UserController, :sign_up
  post "/auth/sign_in", UserController, :sign_in
  post "/auth/forgot_password", UserController, :forgot_password
end

如何设置此管道,以便无需身份验证即可访问 /auth/* 路由?

【问题讨论】:

    标签: elixir phoenix-framework phoenix guardian


    【解决方案1】:

    我可以通过这样设置我的路线来解决这个问题:

    pipeline :anonymous do
      plug :accepts, ["json"]
    end
    
    pipeline :protected do
      plug :accepts, ["json"]
      plug MyApp.AuthAccessPipeline
    end
    
    scope "/api/v1/auth", MyAppWeb do
      pipe_through :anonymous
    
      post "/sign_up", UserController, :sign_up
      post "/sign_in", UserController, :sign_in
      post "/forgot_password", UserController, :forgot_password
    end
    
    scope "/api/v1", MyAppWeb do
      pipe_through :protected
    
      resources "/users", UserController, except: [:new, :edit]
    end
    

    【讨论】:

      猜你喜欢
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      相关资源
      最近更新 更多