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

feat: addition of current user authentication in the request to creat… #71

Conversation

austin047
Copy link

…e a new order

Updated the create order method that handles post request to create a user order to authenticate
against the current user before an order can be created, the client will have to pass the
authentication token in the request header which will be authenticated before the order is created

BREAKING CHANGE: For an order to be created the client must pass the authentication token in the
request header, otherwise a 401 Unauthorized error will be generated

feat #43

@austin047 austin047 force-pushed the user-order-authentication-with-jwt branch from 32bfd3d to 71b5f5f Compare March 30, 2019 02:49
@requestBody() order: Order,
): Promise<Order> {
if (currentUser.id !== userId) {
throw new HttpErrors.BadRequest(
`User id does not match looged in user: ${userId} !== ${
Copy link
Contributor

Choose a reason for hiding this comment

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

User id does not match looged in user:

should be

User id does not match logged in user:

Copy link
Author

Choose a reason for hiding this comment

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

I have done the changes.

austin047 and others added 3 commits April 29, 2019 15:17
…e a new order

Updated the create order method that handles post request to create a user order to authenticate
against the current user before an order can be created, the client will have to pass the
authentication token in the request header which will be authenticated before the order is created

BREAKING CHANGE: For an order to be created the client must pass the authentication token in the
request header, otherwise a 401 Unauthorized error will be generated

feat loopbackio#43

Signed-off-by: austin047 <[email protected]>
Signed-off-by: Raymond Feng <[email protected]>
@jannyHou jannyHou force-pushed the user-order-authentication-with-jwt branch from 7da2091 to 6903c1c Compare April 29, 2019 19:27
Signed-off-by: jannyHou <[email protected]>
Copy link
Contributor

@jannyHou jannyHou left a comment

Choose a reason for hiding this comment

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

@austin047 Thank you so much for adding the authentication for creating the orders of the logged in user.

I left a few comments and rebased your branch(including some trivial change in the last commit to get tests pass). Let me know if you have any questions.

And would you like to rewrite endpoint GET /orders (returns all orders of the current user) as well?
It's ok to leave it out of the scope of this PR, depends on you 🙂

@requestBody() order: Order,
): Promise<Order> {
if (currentUser.id !== order.userId) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The rewrite here is different from the expected behaviour:

The expected requirement mentioned here is don't take in userId from request:

@authenticate('jwt')
async createOrder(
  @inject('authentication.currentUser') currentUser: UserProfile,
  @requestBody() order: Order,
) {
  if (!currentUser.id) {
    throw new HttpErrors.BadRequest(`No user logged in.`);
  }
  // The rest of code that creates orders for the logged in user.
}

But your logic which compares the logged in user id and the id from query also makes sense to me.

If aligning with the original requirements costs too much effort I am ok with the approach here.

Any thought @strongloop/loopback-next ?

Choose a reason for hiding this comment

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

+1. Agree. No user id in request.

Copy link
Member

Choose a reason for hiding this comment

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

Yes please, we should really remove userId argument from this controller method.

In the implementation proposed by this pull request, there are too many places where user id can be specified:

  • userId as a path parameter
  • order.userId from request body
  • currentUser.id from the authentication layer

it('creates an order for a user with a given orderId', async () => {
const newUser = await userRepo.create(user);
const userId = newUser.id.toString();
const order = givenAOrder({userId: userId, orderId: '1'});
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: givenAnOrder

password: plainPassword,
});

const order = givenAOrder({userId: userId});
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason not provide the orderId here?

Copy link
Member

Choose a reason for hiding this comment

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

Ideally, I'd like the server to fill userId so that the clients making HTTP requests to create new orders don't have to. I.e. this test should pass with const order = givenAnOrder() too.

@dhmlau
Copy link
Member

dhmlau commented Jun 10, 2019

@austin047, there are a few review comments from the maintainers. Are you interested to continue to work on this PR? Thanks!

@austin047
Copy link
Author

Yes, I will work on it as soon as possible.

@austin047 austin047 force-pushed the user-order-authentication-with-jwt branch 5 times, most recently from da2c61e to 4020dc2 Compare June 19, 2019 14:37
Client can create an order without passing the userId in the body of the
request, the serve collects the userId from the url and compares against
the authenticated user(Current User) and the creates the order for that
user.

✅ Closes: loopbackio#43

refactor: 💡 remove debug console logs

Remove console logs created for debug purposes

Signed-off-by: Fuh Austin <[email protected]>
@austin047 austin047 force-pushed the user-order-authentication-with-jwt branch from 4020dc2 to 55854d1 Compare June 19, 2019 20:56
@@ -35,13 +37,29 @@ export class UserOrderController {
},
},
})
@authenticate('jwt')
async createOrder(
@param.path.string('userId') userId: string,
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove the userId argument please, we should take the id from currentUser.

@dougal83
Copy link
Contributor

Hey @austin047 Are you interested in continuing work on this PR? I'll happily complete the work if you would prefer. Thanks.

@dougal83
Copy link
Contributor

@jannyHou @hacksparrow should this PR be closed as inactive?

@jannyHou
Copy link
Contributor

@dougal83 Thank you for checking it, yes I think after refactoring the shopping app, all orders endpoints are secured in https://github.com/strongloop/loopback4-example-shopping/blob/master/packages/shopping/src/controllers/user-order.controller.ts

To continue(or open a new PR), are you planning to remove the /{userId} from all paths?

And really appreciate @austin047 's effort on updating it after auth was first added in this example. There are several breaking changes made since then.

@dougal83
Copy link
Contributor

@jannyHou Probably easier to close this PR and start afresh from the current master. If you could create the story/issue with help wanted tag I'll pick it up when I have a moment unless someone beats me to it. Thank you.

@jannyHou
Copy link
Contributor

jannyHou commented Apr 2, 2020

@dougal83 Thank you created story in #629
Based on the chat I am going to close this PR. Let's keep discussion in #629.
And apologize to @austin047 again for unable to merge his code 😞 we appreciate your effort spending on kicking off this refactor 💪

@jannyHou jannyHou closed this Apr 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants