问题描述
我正在尝试为具有会话/cookie 身份验证的基本 API 创建一个 Servant 客户端;但是,我收到了关于缺少 BearerAuthNotEnabled
实例的类型检查错误。
这是我的 API:
import RIO
import qualified Data.Aeson as J
import Servant ( (:>),(:<|>) )
import qualified Servant as SV
import qualified Servant.Auth.Server as AS
import qualified Servant.Client as SC
-- Contents of the session cookie
data User = User
{ username :: !Text,email :: !Text
} deriving (Eq,Show,Generic)
instance J.ToJSON User
instance J.FromJSON User
instance AS.ToJWT User
instance AS.FromJWT User
-- An endpoint that requires authentication.
type ProtectedEndpoint = "protected" :> SV.Get '[SV.JSON] Text
-- The API with authentication wrapped around the protected endpoint.
type Api = (AS.Auth '[AS.Cookie] User :> ProtectedEndpoint)
这是我创建客户端的尝试:
apiProxy :: Proxy Api
apiProxy = Proxy
-- Let Servant generate HTTP clients.
protectedClient :: AC.Token -> SC.ClientM Text
protectedClient = SC.client apiProxy
在编译时,我得到以下实例错误,我无法理解:
• No instance for Servant.Auth.Client.Internal.BearerAuthNotEnabled
arising from a use of ‘SC.client’
• In the expression: SC.client apiProxy
In an equation for ‘protectedClient’:
protectedClient = SC.client apiProxy
我是否正确配置了 Servant 客户端以传递会话 cookie?
更新: 向混合中添加令牌身份验证 (JWT) 解决了该问题。即,如果我将 API 类型声明为
type Api = (AS.Auth '[AS.Cookie,AS.JWT] User :> ProtectedEndpoint)
-- instead of
-- type Api = (AS.Auth '[AS.Cookie] User :> ProtectedEndpoint)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)