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

Extend model object with request context #462

Merged
merged 2 commits into from
Feb 13, 2018

Conversation

basimhennawi
Copy link

Problem:

There are many cases where you want to pass some parameters and access them in the model methods but you're only limited to the method signature params usually some attribute inside the request object. After going through some similar-issues (#254, #336) and PRs (#320) found out there are a lot of usecases needed for different people implementing this lib.

Example:

In saveToken method I need to pass and save consentId attribute in the token table from the request body, as follow:

const saveToken = (token, client, user) => {
    const { consentId } = this.request.body;
    ...
}

Suggested solution (Quick-win):

Extend model object with request context in the handlers (authorize, authenticate, token) so that it can be accessible in any of the model methods, also it won't break the current tests.

Note:

Right now, I forked the lib and did this minimal change as a quick-win but it'd be better if this issue is fixed on the lib so I still can refer to the original lib no my forked one.

Any feedback or change requests are welcome! Thanks!

Copy link
Contributor

@mjsalinger mjsalinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just two things:

a) Pleasechange this PR to a merge request to the dev branch instead of `master.
b) Please add some tests with regards to setting and accessing the request context from model methods.

@basimhennawi basimhennawi changed the base branch from master to dev January 24, 2018 13:04
@mjsalinger
Copy link
Contributor

Great, if you add some tests I think we can merge this.

@basimhennawi
Copy link
Author

Unit and integration tests are added! Thanks @mjsalinger for reviewing!

Copy link
Contributor

@mjsalinger mjsalinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mjsalinger mjsalinger merged commit 3d4fa77 into oauthjs:dev Feb 13, 2018
mjsalinger added a commit to mjsalinger/node-oauth2-server that referenced this pull request Aug 27, 2018
…-request

Extend model object with request context
@aidinrs aidinrs mentioned this pull request Jul 23, 2019
thomseddon pushed a commit that referenced this pull request Jun 11, 2020
@thomseddon
Copy link
Member

I don't think this works as expected as the model functions can be executed asynchronously, the request as set here could be overwritten during model execution.

For example:

const model = {
  request: false,
  saveToken:  function (num) {
    console.log(`saveToken for request ${num} start - model.request = ${model.request}`)
    setTimeout(() => {
      console.log(`saveToken for request ${num} end - model.request = ${model.request}`)
    }, 2000);
  }
}

// 1st request received 1 second after start
setTimeout(() => {
  model.request = 1
  model.saveToken(1);
}, 1000);

// 2nd request received 2 seconds after start
setTimeout(() => {
  model.request = 2
  model.saveToken(2);
}, 2000);

console.log('Starting - All requests queued');

Prints:

Starting - All requests queued
saveToken for request 1 start - model.request = 1
saveToken for request 2 start - model.request = 2
saveToken for request 1 end - model.request = 2
saveToken for request 2 end - model.request = 2

This was also pointed out in #564 (review).

This will be removed from v3.1 and discussion can continue in #254

@thomseddon thomseddon mentioned this pull request Jun 11, 2020
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants