-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Undefined User in middlewares #5964
Comments
@adrien2p has helped me to find the solution. First, the This should be in the documentation. |
Does applying custom middlewares override all default ones? |
No it doesn't, but applying them on core route will apply the custom ones first |
I'm trying to follow your blog on extending medusa to Marketplace https://medusajs.com/blog/extending-medusa-usecase-marketplace/ And I have the same issue with the middleware : What is the recommended way to apply authentication middleware before the custom ones ? |
you need to apply the authenticate middleware first |
I am also experiencing a similar issue. I extended three services: Product, Store and User. For both the Product and Store I am able to pull the loggedInUser just fine however for the User service, I am getting a null value when I pull the loggedInUser from the container. This is what I have for the MiddlewaresConfig where I apply the authenticate middleware first as you have advised to do: |
Ok I got it to work with the help of other members in the discord. Ultimately one had a solution to make the service TRANSIENT and not SCOPED. This was the only service that differs from the other services I have extended (PRODUCT, ORDERS and STORE) whicha are all made to be SCOPED. Extremely confused as to why this is the case but at least it's working now and I hope this helps others when running across this issue. IMO this issue should not be marked as closed as per the docs (https://docs.medusajs.com/development/services/extend-service) we are encouraged to make the extended service SCOPED. |
Can confirm that marking user service as |
Anyone else encountering a CORS issue when adding a product with the following middleware config:
Adding |
facing the same issue |
has anyone fixed this?? |
This is not a bug from Medusa, but from the multi-stage dependencies.
So the So please check your UserService()'s dependencies. My solution is create a custom service which does not has any dependent services and use it inside of LoggedInUserMiddleware, instead of using UserService.
and in loggedInUser middleware
|
Did you fixed the issue? im facing the same problem... |
Here is workaround for that issue. |
Bug report
Describe the bug
According to the example on how to create a middleware, the
req.user
property should show the currently logged in user. However, when applying a middleware to existing routes, thereq.user
isundefined
. Applying to custom routes will correctly attach theuser
record to theMedusaRequest
.System information
Medusa version (including plugins): v1.19.0
Node.js version: v18 or v20
Database: Postgres (duh)
Operating system: Linux
Browser (if relevant):
Steps to reproduce the behavior
Step 1: Create a new app using the
medusa new
command and the default starter template.Step 2: Create a middleware to
console.log
the current logged in user at pathsrc/api/middlewares.ts
:Step 3: Create a custom API route at
src/api/admin/hello/route.ts
:Step 4: Run the dev server using
npm run dev
.Expected behavior
When accessing an existing route, e.g.
/admin/products
using Postman, the console log forreq.user
would beundefined
. But for the custom route/admin/hello
, the app correctly log the current user (or should I say{userId: '<string>'}
).Update 1:
I tried:
but the program gets caught in an infinite loop.
The text was updated successfully, but these errors were encountered: