Get user ID from token in querymen
chemitaxis opened this issue · 2 comments
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!
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.
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!