Laravel/Lumen Cashier-Paddle Authentication Header mixing up with Passport Authentication Header

18 Views Asked by At

I have a user micro-service that runs on lumen framework and I am using passport for api authentication and cashier-paddle for billing, however when I make a request to an authenticated url(subscriptions) to retrieve all subscriptions for user. here is the controller

<?php

    namespace App\Http\Controllers;

    use App\Models\Plan;
    use Exception;
    use Illuminate\Http\JsonResponse;
    use Log;
    use PDOException;

class SubscriptionsController extends Controller
{
     public function __construct()
   {
       $this->middleware('auth:api', ["except" => "index"]);
   }

    public function index(): JsonResponse
   {
      try {
          /**
           * This part assumes that the current billable instance is the authorised user.
           */
          if (!is_null(auth('api')->user())) {
              $billable = auth('api')->user();

              // Our list of available plans, note the name could be anything here for the plan model.
              $plans = Plan::whereIsAvailable(true)
                  ->get(['id', 'paddle_id', 'title', 'name', 'is_available']);

              Log::info('Plans are : ' . count($plans));

              $subscriptions = $plans->map(function ($plan) use ($billable) {

                  if ($currentSubscription = $billable->subscribed($plan->name)) {
                      /** For this example you can just giving them the option to cancel the plan*/
                      $payLink = $billable->subscription($plan->name)->cancelUrl();
                  } else {
                      $payLink = $billable->checkout($plan->name)
                       ->returnTo('https://pb2f0ybgfb.sharedwithexpose.com/dashboard');
                }

                return [
                    'title' => $plan->title,
                    'name' => $plan->name,
                    'payLink' => $payLink,
                    'current' => $currentSubscription,
                ];
            });
        } else {
            // Our list of available plans, note the name could be anything here for the plan model.
            $plans = Plan::whereIsAvailable(true)
                ->get(['id', 'paddle_id', 'title', 'name', 'is_available']);

            Log::info('guest Plans are : ' . count($plans));

            $subscriptions = $plans->map(function ($plan) {
                return [
                    'title' => $plan->title,
                    'name' => $plan->name,
                ];
            });
        }

        return new JsonResponse([
            'data' => $subscriptions,
        ], 200);
    } catch (PDOException $e) {
        Log::error($e->getMessage());

        return new JsonResponse(['error' => 'failed to list subscriptions'], 400);
    } catch (\Laravel\Paddle\Exceptions\PaddleException $e) {
        Log::error($e->getMessage());

        return new JsonResponse(['error' => 'could not list subscriptions'], 400);
    } catch (Exception $e) {
        Log::error($e->getMessage());

        return new JsonResponse(['error' => 'could not list subscriptions'], 400);
    }
}

}

However when I visit this protected route passing the Authorization header for passport, I get this error

Authentication header included, but incorrectly formatted

I am guessing that the call made to paddle's api is appending Authorization header for passport and thus messing with the authorization, I have not been able to confirm this.

NB: This particular error is showing on this route only.

0

There are 0 best solutions below