medusa icon indicating copy to clipboard operation
medusa copied to clipboard

Can't access loggedInUser inside PayPalProviderService

Open AxeemHaider opened this issue 1 year ago • 4 comments

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)

AxeemHaider avatar Feb 16 '24 06:02 AxeemHaider

did you solve it ?

agmyomyat avatar Mar 02 '24 06:03 agmyomyat

No

AxeemHaider avatar Mar 04 '24 02:03 AxeemHaider

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

marcopadillo avatar Mar 08 '24 00:03 marcopadillo

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

kim-hanho avatar May 08 '24 11:05 kim-hanho