Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bajtos opened this issue Nov 14, 2014 · 5 comments
Closed

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

bajtos opened this issue Nov 14, 2014 · 5 comments
Assignees
Labels

Comments

@bajtos
Copy link
Member

bajtos commented Nov 14, 2014

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

See

And the solution:

@jannyHou
Copy link
Contributor

jannyHou commented Dec 3, 2015

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.

@bajtos
Copy link
Member Author

bajtos commented Dec 4, 2015

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

👍

@jankcat
Copy link

jankcat commented Nov 2, 2016

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...");
  }

@jannyHou
Copy link
Contributor

jannyHou commented Nov 2, 2016

@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....

@jankcat
Copy link

jankcat commented Nov 2, 2016

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
Labels
Projects
None yet
Development

No branches or pull requests

4 participants