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

Get user ID from token in querymen #33

Closed
chemitaxis opened this issue Mar 15, 2017 · 2 comments
Closed

Get user ID from token in querymen #33

chemitaxis opened this issue Mar 15, 2017 · 2 comments
Labels

Comments

@chemitaxis
Copy link
Collaborator

Hi Diego, I love your boilerplate (Yeoman Generator Rest), but I have a "question" that I can't solve...

I need to customize a Schema, something like this:

const customSchema = new QuerymenSchema({
  sort: '-LastModified', // this change the default order
  IdUser: '5819c860072c1d156cfec382' // this is the field I need to change
}, {
  page: false,
  limit: false
})

The purpose of that, if get the resource of an endpoint but JUST of the owner user... How can I solve this with querymen?

This is the output of Moongose:
myCollection.find({ IdUser: '5819c860072c1d156cfec382' }, { sort: { LastModified: -1 }, fields: {} })

Everything is ok, but I need to change the IdUser for the current user logged. I think this is a typical approach ;)

Thanks for your work, is excellent!

@diegohaz
Copy link
Owner

diegohaz commented Mar 15, 2017

On the rest project you can use the token({ required: true }) middleware in the express middleware chain.

On the controller, just grab req.user.id and pass it to the mongoose query:

const controller = ({ user, querymen: { query } }, res) =>
  myCollection.find({ IdUser: user.id, ...query })

Didn't test the above code, but you should be able to do something like that.

Tell me if it helps you.

@chemitaxis
Copy link
Collaborator Author

Thanks, this is correct way it works ;)

export const index = ({ user, querymen: { query, select, cursor } }, res, next) => 
  myCollection.find({ IdUser: user.id, ...query }, select, cursor)
    .then((geniallys) => geniallys.map((genially) => genially.view()))
    .then(success(res))
    .catch(next)

On this way, I can use if I want the cursor, and the select!

You can close the question!

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

2 participants