在脚本中创建的couchdb服务器管理员无法在同一脚本中创建数据库

问题描述 投票:0回答:1

在同一个脚本中,我可以在/ _config / admins /上成功创建服务器管理员,然后在_session端点上以该管理员身份登录。回应是:

{"ok":true,"name":"cor","roles":["_admin"]}

但是,我无法创建数据库:

{"error":"unauthorized","reason":"You are not a server admin."}

也就是说,直到我关闭会话并启动另一个会话,以完全相同的方式验证和创建数据库。

我试着等待(2000毫秒)但这不是解决方案。

我运行Couchdb 2.1.1。该脚本是Javascript,由Purescript编译。

这是Purescript来源:

    module Test.Couchdb where

    import Control.Monad.Aff (Aff)
    import Data.Argonaut (fromString)
    import Data.Argonaut.Core (fromObject)
    import Data.Foreign.Generic (defaultOptions, genericDecode)
    import Data.Foreign.NullOrUndefined (NullOrUndefined)
    import Data.Generic.Rep (class Generic)
    import Data.Maybe (Maybe(..))
    import Data.Newtype (class Newtype)
    import Data.StrMap (fromFoldable) as StrMap
    import Data.Tuple (Tuple(..))
    import Network.HTTP.Affjax (AJAX, post, put, AffjaxResponse)
    import Network.HTTP.Affjax.Response (class Respondable, ResponseType(..))
    import Prelude (Unit, bind, pure, unit, ($), (<>))

    -- Run this function on a couchdb 2.1.1 instance with CORS set up or from the localhost.
    -- Puts a new admin with the given user name and password.
    -- Then creates a session.
    -- Finally tries to create a database.
    -- No analysis on what is returned from the server for clarities sake.
    -- This code fails, because the database cannot be created because: "You are not a server admin.". 
    test :: forall e. String -> String -> String -> Aff (ajax :: AJAX | e) Unit
    test usr pwd dbName = do
      (_ :: AffjaxResponse String) <- put ("http://127.0.0.1:5984/_node/couchdb@localhost/_config/admins/" <> usr) (fromString pwd)
      (_ :: AffjaxResponse PostCouchdb_session) <- post "http://127.0.0.1:5984/_session"
        (fromObject (StrMap.fromFoldable [Tuple "name" (fromString usr), Tuple "password" (fromString pwd)]))
      -- the argument to post is in essence {"name": <usr>, "password": <pwd>}
      (_ :: AffjaxResponse String) <-put ("http://127.0.0.1:5984/" <> dbName) ""
      pure unit

    -- Just a definition for the return value of post.
    newtype PostCouchdb_session = PostCouchdb_session
      { ok :: Boolean
      , name :: NullOrUndefined String
      , roles :: Array String
      }
    derive instance genericPostCouchdb_session :: Generic PostCouchdb_session _
    derive instance newtypePostCouchdb_session :: Newtype PostCouchdb_session _
    instance respondablePostCouchdb_session :: Respondable PostCouchdb_session where
      responseType = Tuple Nothing JSONResponse
      fromResponse = genericDecode $ defaultOptions {unwrapSingleConstructors = true}
purescript couchdb-2.0
1个回答
1
投票

Affjax库便捷函数(例如put和get)依赖于将“withCredentials”设置为false的defaultRequest对象。这可以防止浏览器在跨域请求中保存服务器发送的cookie。因此,即使在使用Couchdb成功进行身份验证之后,也不会存储会话cookie,因此下次尝试在服务器上执行操作将失败。

© www.soinside.com 2019 - 2024. All rights reserved.