iOS swift couchbase sync gateway - session expiration

In relation with using this curl command (Ios swift couchbase login using curl) to login, this request returns session (SyncGatewaySession) from the header.

I plan to use the returned session into Sync gateway replication, through pull or push setCookieNamed().

What happens if the session is already expired?
Does the setCookieNamed return status that the session is already expired?

Can anyone give me the best way to handle session expiration in iOS Swift 3 - CB Sync Gateway?

Thanks a lot in advance!

I see two different ways to do this:

  • Track the time to live of the session cookie on the client. The default TTL of a session cookie is 24 hours. However, the session cookie may have been deleted on the server side (if the user logs out on another device for example). So that’s perhaps not the best approach.
  • Monitor the replication change event for a 401 Unauthorized error which means that the cookie has expired. In this case you can request a new session cookie from your app server. Ideally at this point the user shouldn’t have to login again but I don’t know how this can be done with 3rd party auth providers.

Are you using session cookies in the context of user authentication with a 3rd party provider? Can you provide a bit more detail about the login flow you wish to implement?

James

What happens if the session is already expired?

Sync Gateway will return a 401 status for requests that include that cookie.

Does the setCookieNamed return status that the session is already expired?

No, setCookieNamed doesn’t access the network at all so it can’t tell the status of the cookie.

Sync Gateway will return a 401 status for requests that include that cookie.

@jens, where is it exactly returned? can you show me how to handle this response? I tried printing self.pull replication but I don’t see any result.
Thanks in advance.

You can catch the 401 Unauthorized in the replication change event (see 2nd link in 2nd comment).