Skip to content

Login should allow a full fledged include filter #1477

Closed

Description

@pulkitsinghal

Developers should be able to capture a complete picture of a User model upon login from angular like so:

        UserModel.login({
          realm: 'myRealm',
          username: username,
          password: password,
          include: {user: ['roles', ..., 'otherModels']}
        });

What are your thoughts on this enhancement request?

Also not sure what this test teaches us:

.post('/test-users/login?include=USER&include=Post')

Activity

  1. pulkitsinghal commented on Jun 23, 2015

    @pulkitsinghal
    Author

    The workaround (it may be a lapse in my understanding) seems a bit wasteful/slow when retrieving user's related models via promise chaining after login.

    For example:

    1. If I chained UserModel.login() with UserModel.findById() ... I can't use the include filter to get the related models as the include filter isn't supported for findById
    2. If I chain login with findOne which support include filter, it will fail due to authorization reasons ... something about loopback not being able to apply ACLs unless an id is known in the REST url.
    3. So I'm left with one call per related model being chained like so:
    UserModel.login()
      > UserModel.storeModels({id:xxx})
        > UserModel.prototype$__get__roles({id:xxx})
    

    silly no?

  2. doublemarked commented on Jun 23, 2015

    @doublemarked

    You should be using approach 2, login chained with findOne w/ include. I'm not sure why it's failing. Show us how you were doing it?

  3. pulkitsinghal commented on Jun 23, 2015

    @pulkitsinghal
    Author

    @doublemarked - sure, here's what I tried for approach 2:

    This works but does not have roles:
    UserModel.findOne({id: $sessionStorage.currentUser.userId})

    It doesn't work when I try to include roles:

                    UserModel.findOne({
                      filter: {
                        where: {
                          id: $sessionStorage.currentUser.userId
                        },
                        include: [
                          'roles'
                        ]
                      }
                    })
    

    I think @beeman uses it here: https://github.com/beeman/loopback-angular-playground/blob/498f39b9c48c4984cedf5c574ce692c7a114247f/client/js/services/auth.js#L7-L10

    I get an error:

    {
       "error":{
          "name":"Error",
          "status":403,
          "message":"Access Denied",
          "statusCode":403,
          "code":"ACCESS_DENIED",
          "stack":"Error: Access Denied
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/lib/application.js:348:21
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/lib/model.js:313:7
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/common/models/acl.js:465:23
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:251:17
        at done (/Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:132:19)
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:32:16
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:248:21
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:572:34
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/common/models/acl.js:447:17
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/common/models/role.js:319:23
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:368:13
        at done (/Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:132:19)
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:32:16
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:365:17
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/common/models/role.js:310:15
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback-datasource-juggler/lib/dao.js:1442:62
        at allCb (/Users/pulkitsinghal/dev/w2/node_modules/loopback-datasource-juggler/lib/dao.js:1374:7)
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback-datasource-juggler/lib/connectors/memory.js:371:7
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/continuation-local-storage/node_modules/async-listener/glue.js:188:31
        at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/continuation-local-storage/node_modules/async-listener/glue.js:188:31
        at process._tickDomainCallback (node.js:492:13)
        at process.<anonymous> (/Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/continuation-local-storage/node_modules/async-listener/index.js:19:15)"
       }
    }
    
  4. faridnsh commented on Aug 19, 2015

    @faridnsh
    Contributor

    @pulkitsinghal To get include: 'roles working you need to define the relationship: User.hasMany(Role, {through: RoleMapping, foreignKey: 'principalId'});

  5. pulkitsinghal commented on Aug 19, 2015

    @pulkitsinghal
    Author

    @alFReD-NSH - Thanks! I've had that for a while: https://github.com/ShoppinPal/warehouse/blob/develop/common/models/user-model.json#L43-L48

    I think it is just the way login is implemented, it doesn't do certain things. So I'd like it enhanced :)

  6. stringbeans commented on Aug 26, 2015

    @stringbeans

    +1, would like to see the ability to include more than just the "user". it is strange that it is limited ONLY to the user and no sub relations

  7. mamartins commented on Feb 8, 2016

    @mamartins

    +1

  8. lee-40square commented on Jul 25, 2016

    @lee-40square

    +1
    I should be able to log in and have related objects returned (using include) in a single REST call. As I understand it, I can only get the user object returned in a single call.

  9. dongmai commented on Aug 12, 2016

    @dongmai

    +1

  10. dongmai commented on Aug 31, 2016

    @dongmai

    +1

  11. acabreragnz commented on Sep 7, 2016

    @acabreragnz

    +1

  12. tatianacmh commented on Sep 7, 2016

    @tatianacmh

    +1

  13. mradzinski commented on Sep 7, 2016

    @mradzinski

    +1

  14. rkmax commented on Sep 7, 2016

    @rkmax

    +1

  15. 12 remaining items

  16. stale commented on Sep 6, 2017

    @stale

    This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

  17. bajtos commented on Oct 13, 2017

    @bajtos
    Member

    Reopening based on the discussion in #3641. Please note that this issue is not a priority to us, we are not going to work on it anytime soon. Please let us know if you can contribute this improvement yourself, we are happy to help you along the way.

  18. reopened this on Oct 13, 2017
  19. phortonssf commented on Oct 17, 2017

    @phortonssf

    Appuser.js returns user along with relational data. Needs node ^7.6.0

    Appuser.afterRemote('login', async (ctx, user, next) => {
            if(user){
                let userData = await Appuser.find({
                        fields:{password: false, username: false, realm: false, '_id': 0},
                        include:{
                            relation: 'cities',
                            scope: {
                                fields: ['name', 'temp_f']
                            },
                        },
                        where: {
                        id: user.userId
                    }
                })
                user.userData = userData[0]
            }
        })
    
  20. ahmetcetin commented on Nov 17, 2017

    @ahmetcetin

    in api documentation (http://apidocs.loopback.io/loopback/#user-login) it looks like this feature is there but i couldn't make it working. is it already implemented? is there any sample code?

  21. bajtos commented on Dec 14, 2017

    @bajtos
    Member

    @ahmetcetin AFAIK, it's not possible to include arbitrary related models in User.login request yet. Would you like to contribute this feature yourself?

  22. ahmetcetin commented on Dec 14, 2017

    @ahmetcetin

    @bajtos i'll give it a try during Christmas break, till then quite busy.

  23. phortonssf commented on Dec 14, 2017

    @phortonssf

    @ahmetcetin below code is working for relational data on login. You can remove scope if you want every field from the relational data. Let me know if you have a different solution or idea.
    The App-user.js returns user along with relational data. Needs node ^7.6.0

    
    Appuser.afterRemote('login', async (ctx, user, next) => {
            if(user){
                let userData = await Appuser.find({
                        fields:{password: false, username: false, realm: false, '_id': 0},
                        include:{
                           relation: <relation>,
                            scope: {
                                fields: [<fields to include>]
                            },
                        },
                        where: {
                        id: user.userId
                    }
                })
                user.userData = userData[0]
            }
        })
    
  24. stale commented on Feb 12, 2018

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  25. stale commented on Feb 26, 2018

    @stale

    This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions