微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法创建提供会话 cookie 的经过身份验证的 Servant 客户端

如何解决无法创建提供会话 cookie 的经过身份验证的 Servant 客户端

我正在尝试为具有会话/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 举报,一经查实,本站将立刻删除。