loopbackio / loopback-connector-elastic-search

Strongloop Loopback connector for Elasticsearch
MIT License
79 stars 56 forks source link

Login does not work #3

Closed pulkitsinghal closed 9 years ago

pulkitsinghal commented 9 years ago

When I use the memory connector, the accessToken looks like:

req.accessToken: {
  "id": "qGYGzUMsGsSRuG2Xwy28rDQSQOanp8dqDbYgdKdKqqqlqQtETzSNeyrdhdGlpvUA",
  "ttl": 1209600,
  "created": "2015-04-01T16:12:29.025Z",
  "userId": 2
}

But when I use the elasticsearch connector, it looks like:

req.accessToken: { 
  "id": "SEFEuxTa8XOjCtywWhAXNvKK1fVukSqawcNHBT9bHIy4DuUMBeeIEw9vIOp7iQWR", 
  "ttl": 1209600, 
  "created": "2015-04-01T16:06:03.489Z" 
}

Notice how the userId is missing for req.accessToken when elasticsearch connector is involved?

pulkitsinghal commented 9 years ago

I went ahead and attempted to sanity test this in a boot script:

// login w/ merchant1 
UserModel.login( 
  {realm: 'portal', username: retailUser.username, password: 'xxx'}, 
  function(err, accessToken) { 
    if (err) return debug('%j', err); 
    debug(accessToken); 
    debug('logged in w/ ' + retailUser.username + ' and token ' + accessToken.id); 
});

And the result was:

boot:create-model-instances {
  ttl: 1209600,
  userId: undefined,
  created: Wed Apr 01 2015 11:05:49 GMT-0500 (CDT),
  id: '1GcFtx1APKWBgLRejaYbv7dxW1U1vl2bCIGTPiNw7lGiupkbpzWLEzKLRagvWbIK'
} +342ms 
boot:create-model-instances logged in w/ merchant1@shoppinpal.com and token 1GcFtx1APKWBgLRejaYbv7dxW1U1vl2bCIGTPiNw7lGiupkbpzWLEzKLRagvWbIK +0ms

So userId isn't being set is obvious ... I wonder, where in the core loopback code should I dig for the login() method?

raymondfeng commented 9 years ago

Do you use the ES connector to back the user model?

pulkitsinghal commented 9 years ago

Yes, UserModel extends the built in user model

pulkitsinghal commented 9 years ago

@raymondfeng So when the UserModel is backed by memory connector, there's no problem but when it is backed by ES, this issue crops up.

pulkitsinghal commented 9 years ago

@raymondfeng - can you point me to a test that I can copy/paste and use to explore what might be wrong with my connector?

superkhau commented 9 years ago

@sumitha No, there is no example for that specific situation. I suggest digging through the source code of other connectors, most likely https://github.com/strongloop/loopback-connector-mongodb in this case since ES is a document-oriented database.

superkhau commented 9 years ago

Digging through the User.login method may help also.

superkhau commented 9 years ago

can you point me to a test that I can copy/paste and use to explore what might be wrong with my connector?

https://github.com/strongloop/loopback/blob/master/test/user.test.js

pulkitsinghal commented 9 years ago

@superkhau - in user.test.js I don't see app variable getting initialized anywhere, does this test have a parent that calls or requires it? https://github.com/strongloop/loopback/blob/master/test/user.test.js#L34

bajtos commented 9 years ago

in user.test.js I don't see app variable getting initialized anywhere, does this test have a parent that calls or requires it?

See https://github.com/strongloop/loopback/blob/bd973def6308a771bdd7212d9a1d7021f3772491/test/support.js#L19-L33

I would like to get rid of that support file because it makes the test difficult to understand.

pulkitsinghal commented 9 years ago

In the all() method it makes sense to return the id with the content! Its a given on other datasources but for a datasource like elasticsearch, we need to make sure and map "_id" into the content, just in case its an auto-generated one.

Also need to make sure that the model property marked as id: true (if it is configured) is the one that gets the value.

Simple tests have been added to prevent regression.