medusa
medusa copied to clipboard
Can't access loggedInUser inside PayPalProviderService
Bug report
Describe the bug
I'm trying to implement marketplace, each vendor has it's own PayPal
account for this purpose I saved PayPal config (clientId, secret etc)
in database and want to get on PayPalProviderService
for this purpose I need to know which user is currently logged in. Here is article which show how to create a middleware to access loggedInUser
Example - Access loggedInUser
middleware.ts
import { MiddlewaresConfig } from "@medusajs/medusa";
import type {
MedusaNextFunction,
MedusaRequest,
MedusaResponse,
} from "@medusajs/medusa";
const registerLoggedInUser = async (
req: MedusaRequest,
res: MedusaResponse,
next: MedusaNextFunction
) => {
req.scope.register({
loggedInUser: {
resolve: () => req.user?.userId || "",
},
});
next();
};
export const config: MiddlewaresConfig = {
routes: [
{
matcher: /\/admin\/[^(auth)].*/,
middlewares: [registerLoggedInUser],
},
],
};
product.ts (works fine successfully able to get the loggedInUser id)
import { Lifetime } from "awilix";
import { ProductService as MedusaProductService } from "@medusajs/medusa";
// extend core product service
class ProductService extends MedusaProductService {
// The default life time for a core service is SINGLETON
static LIFE_TIME = Lifetime.SCOPED;
constructor(container, options) {
// @ts-ignore
super(...arguments);
try {
console.log("=====>Product Logged in user id:", container.loggedInUser);
} catch (e) {
console.log(e);
}
}
}
export default ProductService;
I also want to get the loggedInUser
on PayPalProviderService
paypal-provider.ts (Unable to get loggedInUser
it fail to resolve)
class PayPalProviderService extends AbstractPaymentProcessor {
static LIFE_TIME = Lifetime.SCOPED;
constructor(container, options) {
// @ts-ignore
super(...arguments);
try {
console.log(
"=====>Paypal Provider Logged in user id:",
container.loggedInUser
);
} catch (e) {
console.log(e);
}
}
// Other methods...
}
export default PayPalProviderService;
Every time I get this error.
AwilixResolutionError: Could not resolve 'loggedInUser'. Resolution path: pp_paypal -> paypalProviderService -> loggedInUser
Steps to reproduce this error
- Install new medusa backend
- Create loggedInUser middleware
- Create plugin for payment
- Try to access loggedInUser inside payment provider service (in this case PaypalProviderService)
did you solve it ?
No
I think it's related to this issue: https://github.com/medusajs/medusa/issues/6585
Please check my comment: https://github.com/medusajs/medusa/issues/5964#issuecomment-2100352398