Skip to content

How to access the current user from a model method #816

Closed

Description

@bajtos

We should document the recommended way for accessing the current user from model methods.

See

And the solution:

Activity

  1. added and removed on Nov 26, 2015
  2. jannyHou commented on Dec 3, 2015

    @jannyHou
    Contributor

    hey @bajtos , I confirmed with Rand and this solution is already explained perfectly in doc:
    https://docs.strongloop.com/display/public/LB/Using+current+context#Usingcurrentcontext-Usecurrentauthenticateduserinremotemethods

    The same example snippet as you post in comment is applied.

    Since it's done, I will close it here. Thanks.

  3. bajtos commented on Dec 4, 2015

    @bajtos
    MemberAuthor

    Since it's done, I will close it here.

    👍

  4. jankcat commented on Nov 2, 2016

    @jankcat

    For those looking for a solution to finding out the authenticated user in a remote method (now that currentContext is deprecated), the groundwork is laid out at the following link: http://loopback.io/doc/en/lb2/Remote-methods.html#http-mapping-of-input-arguments

    Here is my example snippet:

      MyObject.remoteMethod(
        'myMethod',
        {
          http: {path: '/myMethod', verb: 'get'},
          accepts: [
            {arg: 'someArgument', type: 'string'},
            {
              arg: 'accessToken',
              type: 'object',
              http: function(ctx) {
                return ctx.req.accessToken;
              }
            }
            ],
          returns: {arg: 'someResponse', type: 'string'}
        }
      );
      MyObject.myMethod = function(someArgument, accessToken, cb) {
        // ...
        // accessToken.userId
        // ...
        cb(null, "Response blah blah...");
      }
  5. jannyHou commented on Nov 2, 2016

    @jannyHou
    Contributor

    @jankcat Thank you for looking into context issue and providing the sample code.

    While the link you post is from our old doc site. Now it's migrated to new site and please check latest doc of proper usage of context:
    http://loopback.io/doc/en/lb2/Using-current-context.html
    It elaborates some warning, solution, ect....

  6. jankcat commented on Nov 2, 2016

    @jankcat

    Thanks, I updated my comment to the new documentation location.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions