How can I create reference document using sync gateway REST API?

Firstly, I am new to Couchbase development. I am creating an android application which requires user’s sign in and sign up process. There are multiple options for sign in like email, google and facebook. So I need to reference all this options to signle document. So I found this article about reference document here:

So When a user sign in with Google or Facebook or Email i’ll pass the something like g::ID, fb::ID or email::email_id which can refer to single user profile document if matches(through reference documents as mentioned in the link). If it matches sync gateway should return the document with the details.

Else if any of those doesn’t match with any user profile document it should return null so that it takes a sign up process automatically and create a new user profile document. This thing should happen through sync gateway REST API so that sync gateway can have the newly created document in its memory.

So, for this newly created document I need to add reference documents through sync gateway’s REST API so that it can point to that user profile for next sign in. Unfortunately, I am not able to find anything on how can I create reference documents using Couchbase Lite? Or Is there any other way to handle this type of situation in any other way?

Thank you.

EDIT: As I understood, There is no document type like reference document. It is a simple new document with different keys containing same user id. So the value which is stored in it is an integer (or can be string). There is no key value pair in the document. So how can I create a document using Couchbase Lite for Android with only a value and not a key value pair?

That article about ‘reference documents’ is from a release of Couchbase Server that predates N1QL. The technique it’s talking about is useful when there’s no form of indexing or querying available, which was the case then; but with the current server you’d probably find it easier to do a N1QL query to look up user accounts based on properties inside the profile doc.

I’m not sure why you’re asking about how to do this in Couchbase Lite. Authentication has to be done on the server. To do custom auth you’d implement an HTTP handler in your app server and look up the user account in a bucket you create for storing credentials. If the credentials are correct, your app server would send Sync Gateway a REST request to generate a session cookie, then send that back to the client. (The Sync Gateway docs have a page about custom auth.)

Yes, I understood the concept now. When user signs in to the server I can easily verify it by sending a GET request to sync gateway than querying on the bucket. If sync gateway returns the document than user is already signed up, If not I am responsible to create new user with google/facebook options from the web server. So for that I need to use sync gateway’s REST API to create this kind of documents. So more specifically, can I create documents with a simple value using sync gateway’s REST API ? So that I can establish this mechanism which work flawlessly with user logins/account creation. This is concept is also mentioned in this Couchbase data modeling video.

It’s a lot safer not to store those account/credential documents in the Sync Gateway bucket. If you put them there, they are by default readable and writeable by mobile clients, which of course breaks your security completely. You can configure the sync function to fix that, but you’re still vulnerable if there’s a mistake in the sync function.

For that reason, and since there’s probably no need to replicate those credential documents to clients, it’s better to just set up a separate Couchbase bucket for them.

@jens Yes, That’s true ! The mistake in sync function can break that! But this actually does not contain any critical information, I can simply disable this type of docs by type in sync function.

Though it looks like querying will be a better approach to check the if user is already signed up or not! Thank you for your valuable feedback.