我正在尝试使用this documentation对couchdb进行身份验证
当我这样做
# first request
const url = 'http://localhost:5984/_session'
fetch(url, {
method: 'POST',
headers: {
"Content-Type": "application/json",
},
body : JSON.stringify({
"name": "my_username",
"password": "my_password"
}),
}).then( data => {
console.log(data)
}).catch(e => {
console.log('Error', e)
})
如果my_username或my_password中的一个不正确,我将得到:
body: (...)
bodyUsed: false
headers: Headers {}
ok: false
redirected: false
status: 401
statusText: "Unauthorized"
type: "cors"
url: "http://localhost:5984/_session"
这很好。
但是,如果my_username或my_password中的一个正确,则会得到:
body: ReadableStream
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "cors"
url: "http://localhost:5984/_session"
代替
HTTP/1.1 200 OK
Cache-Control: must-revalidate
Content-Length: 43
Content-Type: application/json
Date: Mon, 03 Dec 2012 01:23:14 GMT
Server: CouchDB (Erlang/OTP)
Set-Cookie: AuthSession=cm9vdDo1MEJCRkYwMjq0LO0ylOIwShrgt8y-UkhI-c6BGw; Version=1; Path=/;
HttpOnly
{"ok":true,"name":"root","roles":["_admin"]} // <-- i expect that
也没有饼干。
我也试过卷发,它很管用:
> curl http://localhost:5984/_session
{"ok":true,"userCtx":{"name":null,"roles":[]},"info":{"authentication_handlers":
["cookie","default"]}}
> curl -X POST http://localhost:5984/_session -H "Content-Type: application/json" -d '{"name":"my_username","password":"my_password"}'
{"ok":true,"name":"my_username","roles":["_admin"]}
但我需要它在一个react应用程序中工作,来自http:localhost:3000
Maybe it's CORS related?我在CouchDB设置中启用了CORS。
我如何修改第一个请求,以获得用户对象的名称/密码提供?
1条答案
按热度按时间bogh5gae1#
我认为认证集-Cookie是由后端服务器而不是前端服务器产生的。所以你需要像Node.js这样的后端服务器来获取Cookie,然后再从前端服务器获取。