How to destroy session in CouchBaseLite in Swift


I want to destroy session when user logout from application. I want to destroy session using API


Can you clarify what you mean by “session” here? Are you referring to the TCP connection to the Sync Gateway that’s opened during replication or something else.


Thanks to reply

When I login with CouchBaseLite, first I call session API “http://localhost:4984/buildercycle/_session” with parameter

but When I logout with CouchBaseLite I call API “http://localhost:4984/buildercycle/_session” with Header
[“cookie”: Session.cookie!]
But I got response
“reason” : “Login required”,
“error” : “Unauthorized”

Please help me
how to destroy session on sync gateway?


Hmm…are you sure the cookie is valid (and not expired).

But is there a reason you are doing session based auth instead of basic auth ? Are you doing some custom authentication on the server side?


I am using both authentication Server side and basic auth

and also find valid cookie

if let res = response.response?.allHeaderFields[“Set-Cookie”] {
let cookie = String(describing:res).components(separatedBy:";")[0].components(separatedBy: “=”)[1]
print(“cookie -------” + cookie)

If you are using basic Auth, why aren’t you just sending the auth credentials as part of the Auth header in request (using the Authenticator ). Or is there some special handling going on on the server side?
A little more info on your login flow setup will help us determine the best option …

So what does your print statement output? Basically make sure that you DELETE the session when authorized to do so.
A better way - Please monitor replication change events and see if you are seeing an unauthorized error. That would be a sign that the session cookie has expired…