How can i create a custom login mechanism?


#1

Hi everyone. I’m studying Couchbase Lite (3rd day) and it seems to me very powerful.

Actually there is one particular thing that i can’t understand: login.

I’ve seen Facebook login, custom login with cookies and strange things but… i need to implement a normal login
mechanism. The user opens the app, types in name and password and if such user exists, then he enters in the
MainActivity.

I didn’t find any tutorial or practical example about that.

I want to create a simple application in which a user can be a Student or a Professor. A professor can
create/update/delete notes about his courses. A student can create/update notes about the courses
he is registered to. These rules can be defined inside the sync functions, is it right?

From what i understand, in this context a course can be seen as a channel.

Data model:

  • Documents for professors (id, type, name, array of his courses (ids))
  • Documents for students (id, type, name, array of courses he is registered to (ids))
    ?? Documents for notes (id, type, course it belongs to, text) ?? --> probably it is not necessary
  • Documents for courses (name(id), type, array of notes with title and text)

To make a login mechanism as i’ve said, is it enough to put also the password in the documents of students and professors?
Then when the user put the data and clicks login, i would execute a query against the database that looks for
a student or professor with that combination of name/password.

Note: it should not be a REAL application, it’s just an example.

Note 2: i’m using the default database walrus.

Note 3: i’m reading about Admin REST API, but i didn’t understand from where should i send HTTP requests…

Note 4: Ok i’ve to use “curl” to execute http requests. It’s a bit complicated…!

Update:

Ok i can define users in the serviceconfig.json but…

As i said, my users (students or professors) haven’t just “name” and “password”, so i need documents, and
not just their definition in the config file… i’m very confused.

Note 5: for example, once the user is logged with basic authentication, i need to know if he/she is a professor or a studient to load the correct layout for the Activity.


#2

you can create a user by Admin REST API with “name” and “password”;
then you can use this name and password to login.
at the same time,you can create Documents for professors (id, type, name, array of his courses (ids))
Documents for students (id, type, name, array of courses he is registered to (ids)) which id should reference name.
for example,you can create a students user by:

curl -X PUT localhost:4985/walrus/_user/students01 --data '{"name":"students01","password":"password"}'

note: you should run the command on the Sync Gateway server only.

then you can create a students documents by:

curl -X PUT -H "Content-Type:application/json" http://students01:password@127.0.0.1:4984/walrus/STUDENT:students01 --data '{"type":"STUDENT","name":"students01's name","ids":["course01","course02"]}'

then you can deal with ACL in Sync Function by split userID(students01) from id(STUDENT:students01) of student document.

You can get document’s specific properties by View to know if he/she is a professor or a student.


#3

Thank you very much, this will be useful!

I didn’t understand this part:

“then you can deal with ACL in Sync Function by split userID(students01) from id(STUDENT:students01) of student document.”

Update:

If i try to execute those commands, i get: {“error”:“Bad Request”,“reason”:“Invalid JSON: “invalid character ‘\’’ looking for beginning of value””}


#4

FYI.

change example to:

curl -X PUT -H "Content-Type:application/json" http://students01:password@127.0.0.1:4984/walrus/STUDENT:students01 --data '{"type":"STUDENT","name":"students01_name","ids":["course01","course02"]}'

#5

I get the error when i try to create a new user. I could not try to create a documen yet.


#6

my env works fine.
can you post your all input and output info?


#7

C:\Program Files (x86)\Couchbase>curl -X PUT localhost:4985/university-notes/_user/students01 --data ‘{“name”:“students01”,“password”:“password”}’
{“error”:“Bad Request”,“reason”:“Invalid JSON: “invalid character ‘\’’ looking for beginning of value””}

Looking at the couch db wiki, i tried to put:

C:\Program Files (x86)\Couchbase>curl -H Content-Type:application/json -vX PUT localhost:4985/university-notes/_user/students01 -d ‘{“name”:“name”,“password”:“password”}’

  • Trying ::1…
  • Trying 127.0.0.1…
  • Connected to localhost (127.0.0.1) port 4985 (#0)

PUT /university-notes/_user/students01 HTTP/1.1
Host: localhost:4985
User-Agent: curl/7.49.1
Accept: /
Content-Type:application/json
Content-Length: 31

  • upload completely sent off: 31 out of 31 bytes
    < HTTP/1.1 400 Bad Request
    < Content-Type: application/json
    < Server: Couchbase Sync Gateway/1.2.1
    < Date: Sun, 19 Jun 2016 13:20:01 GMT
    < Content-Length: 107
    <
    {“error”:“Bad Request”,“reason”:“Invalid JSON: “invalid character ‘\’’ looking for beginning of value””}* Connection #0 to host localhost left intact

#8

the doc’id should be the same as name property.


#9

But i’m still trying to create a user. From what you said, it must have a name and a password, for the login.

I would like to create a document but i need the user first…


#10

try this

curl -H Content-Type:application/json -vX PUT localhost:4985/university-notes/_user/students01 -d '{"name":"students01","password":"password"}'

the value of “name” should be students01,not name


#11

C:\Program Files (x86)\Couchbase>curl -H Content-Type:application/json -vX PUT localhost:4985/university-notes/_user/students01 -d ‘{“name”:“students01”,“password”:“password”}’

  • Trying ::1…
  • Trying 127.0.0.1…
  • Connected to localhost (127.0.0.1) port 4985 (#0)

PUT /university-notes/_user/students01 HTTP/1.1
Host: localhost:4985
User-Agent: curl/7.49.1
Accept: /
Content-Type:application/json
Content-Length: 37

  • upload completely sent off: 37 out of 37 bytes
    < HTTP/1.1 400 Bad Request
    < Content-Type: application/json
    < Server: Couchbase Sync Gateway/1.2.1
    < Date: Sun, 19 Jun 2016 13:54:25 GMT
    < Content-Length: 107
    <
    {“error”:“Bad Request”,“reason”:“Invalid JSON: “invalid character ‘\’’ looking for beginning of value””}* Connection #0 to host localhost left intact

#12

Ok i almost did it.

It works if i write this:

curl -H Content-Type:application/json -vX PUT localhost:4985/university-notes/_user/students01 -d {\"name\":\"students01\",\"password\":\"password\"}
  1. Is it normal that the users that i add don’t appear in the serviceconfig.json?

  2. From the application, if i do:

    database = manager.getExistingDatabase(DATABASE_NAME);

with DATABASE_NAME = “university-notes”, it does not exist…


#13

yes, user in serviceconfig.json is for test purpose.