medusajs / medusa

Building blocks for digital commerce
https://medusajs.com
MIT License
24.65k stars 2.43k forks source link

Can't access loggedInUser inside PayPalProviderService #6414

Open AxeemHaider opened 7 months ago

AxeemHaider commented 7 months ago

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

  1. Install new medusa backend
  2. Create loggedInUser middleware
  3. Create plugin for payment
  4. Try to access loggedInUser inside payment provider service (in this case PaypalProviderService)
agmyomyat commented 6 months ago

did you solve it ?

AxeemHaider commented 6 months ago

No

marcopadillo commented 6 months ago

I think it's related to this issue: https://github.com/medusajs/medusa/issues/6585

kim-hanho commented 4 months ago

Please check my comment: https://github.com/medusajs/medusa/issues/5964#issuecomment-2100352398